feature/ss-instead-of-netstat #2

Merged
manuel merged 8 commits from feature/ss-instead-of-netstat into master 2020-09-18 14:24:42 +02:00
Showing only changes of commit 2f58a65f7b - Show all commits

View file

@ -38,7 +38,7 @@
MY_IP="94.199.214.20"
# After this point, no editing is required.
start=$(date +%s)
start="$(date +%s)"
# Dependencies of this script. Simple array with the following structure:
# (command package [...])
@ -53,8 +53,8 @@ dependencies=(
"tr" "sys-apps/coreutils"
"uniq" "sys-apps/coreutils"
"grep" "sys-apps/grep"
"sponge" "sys-apps/moreutils"
"ss" "sys-apps/iproute2"
"sponge" "sys-apps/moreutils"
"getopt" "sys-apps/util-linux"
)
@ -85,8 +85,8 @@ function is_installed() {
}
function print_missing_dependency() {
local command="$1"
local package="$2"
local command="${1}"
local package="${2}"
echo "${red}Command ${bold}${command}${reset}${red} not found.${reset} Please install package ${blue}${package}${reset}." >&2
}
@ -104,7 +104,7 @@ function check_dependencies() {
package="${dependencies[$i + 1]}"
is_installed "${command}" "${package}"
res=$?
if [[ $res -ne 0 ]] ; then
if [[ ${res} -ne 0 ]]; then
print_missing_dependency "${command}" "${package}"
all_installed=1
fi
@ -176,11 +176,11 @@ function exec_as_root() {
function filter() {
# list of current connections
file="$1"
file="${1}"
# subnet extension, e.g. ".0.0"
ext="$2"
ext="${2}"
# subnet suffix, e.g. "/16"
suffix="$3"
suffix="${3}"
rm -f "${filtered}"
touch "${filtered}"
@ -208,14 +208,14 @@ function parse_command_line_args() {
unset TEMP
while true; do
case "$1" in
case "${1}" in
'-a' | '--auto')
case "$2" in
case "${2}" in
'')
autopilot=1
;;
*[!0-9]*)
echo "Invalid argument for parameter 'auto': '$2'. Invoke with --help for help." >&2
echo "Invalid argument for parameter 'auto': '${2}'. Invoke with --help for help." >&2
exit 1
;;
*)
@ -225,15 +225,15 @@ function parse_command_line_args() {
shift
;;
'-c' | '--country')
IFS=',' read -ra bancountries <<< "$2"
if [[ -z ${bancountries[@]// } ]] ; then
echo "Invalid argument for parameter 'country': '$2'. Invoke with --help for help." >&2
IFS=',' read -ra bancountries <<<"${2}"
if [[ -z ${bancountries[@]// /} ]]; then
echo "Invalid argument for parameter 'country': '${2}'. Invoke with --help for help." >&2
exit 1
fi
shift
;;
'-d' | '--database')
database="$2"
database="${2}"
shift
;;
'-e' | '--dependencies')
@ -241,11 +241,11 @@ function parse_command_line_args() {
exit $?
;;
'-j' | '--jail')
jail="$2"
jail="${2}"
shift
;;
'-n' | '--netmask')
case "$2" in
case "${2}" in
'1' | '8')
netmask=8
;;
@ -259,14 +259,14 @@ function parse_command_line_args() {
netmask=32
;;
*)
echo "Invalid argument for parameter 'netmask': '$2'. Invoke with --help for help." >&2
echo "Invalid argument for parameter 'netmask': '${2}'. Invoke with --help for help." >&2
exit 1
;;
esac
shift
;;
'-p' | '--port')
port="$2"
port="${2}"
shift
;;
'-h' | '--help')
@ -278,7 +278,7 @@ function parse_command_line_args() {
break
;;
*)
echo "Unknown error on command line argument '$1'. Terminating." >&2
echo "Unknown error on command line argument '${1}'. Terminating." >&2
exit 1
;;
esac
@ -303,13 +303,13 @@ function parse_command_line_args() {
# color to the next happen at different values.
################################################################################
function set_highlight_color() {
local count=$1
local count=${1}
case "${choice}" in
"1")
# /32: 0 <= green < 3 <= yellow < 5 <= red
if [ $count -ge 5 ] ; then
if [ ${count} -ge 5 ]; then
hilite="${red}"
elif [ $count -ge 3 ] ; then
elif [ ${count} -ge 3 ]; then
hilite="${yellow}"
else
hilite="${green}"
@ -317,9 +317,9 @@ function set_highlight_color() {
;;
"2")
# /24: 0 <= green < 7 <= yellow < 13 <= red
if [ $count -ge 13 ] ; then
if [ ${count} -ge 13 ]; then
hilite="${red}"
elif [ $count -ge 7 ] ; then
elif [ ${count} -ge 7 ]; then
hilite="${yellow}"
else
hilite="${green}"
@ -327,9 +327,9 @@ function set_highlight_color() {
;;
"3")
# /16: 0 <= green < 13 <= yellow < 25 <= red
if [ $count -ge 25 ] ; then
if [ ${count} -ge 25 ]; then
hilite="${red}"
elif [ $count -ge 13 ] ; then
elif [ ${count} -ge 13 ]; then
hilite="${yellow}"
else
hilite="${green}"
@ -337,9 +337,9 @@ function set_highlight_color() {
;;
"4")
# /8: 0 <= green < 21 <= yellow < 49 <= red
if [ $count -ge 49 ] ; then
if [ ${count} -ge 49 ]; then
hilite="${red}"
elif [ $count -ge 21 ] ; then
elif [ ${count} -ge 21 ]; then
hilite="${yellow}"
else
hilite="${green}"
@ -378,7 +378,7 @@ function process_file () {
addrwithsuffix="${addronly}${suffix}"
set_highlight_color "${count}"
country="$("${curdir}/geoip-lookup.py" -f "${database}" "${addronly}")"
if [[ autopilot -eq 0 ]] ; then
if [[ ${autopilot} -eq 0 ]]; then
echo "Country: '${yellow}${country}${reset}'"
fi
echo -n "Address ${bold}$((nline++)) of ${nlines}${reset}: \
@ -389,7 +389,7 @@ Found '${blue}${addrwithsuffix}${reset}' ${hilite}${count}${reset} times."
read banaction
else
if [[ " ${bancountries[@]} " =~ " ${country} " ]]; then
if [[ $count -ge $autopilot ]] ; then
if [[ ${count} -ge ${autopilot} ]]; then
echo -en "\n${red}Autopilot active. ${reset}"
banaction=y
else
@ -397,7 +397,7 @@ Found '${blue}${addrwithsuffix}${reset}' ${hilite}${count}${reset} times."
return
fi
else
if [[ $count -ge $autopilot ]] ; then
if [[ ${count} -ge ${autopilot} ]]; then
echo -en "\n${green}Autopilot active. ${reset}"
banaction=n
else
@ -429,7 +429,7 @@ banlist (country=${yellow}${country}${reset})."
# Create a temp directory, chdir into it and create the (initially empty)
# banlist file.
tmpdir=$(mktemp -d)
tmpdir="$(mktemp -d)"
# Set up all file paths
curdir="$(dirname "$0")"
@ -563,6 +563,6 @@ while read -r addrwithsuffix ; do
exec_as_root fail2ban-client set "${jail}" banip "${addrwithsuffix}"
done <"${banlist}"
end=$(date +%s)
end="$(date +%s)"
echo "${green}All done in $((end - start)) seconds!${reset}"