Files
demus a71b621e04
Build and provide rpm of demus shell scripts / rpmbuild (release) Successful in 56s
blockable: Improve output of asntool lookups
2025-03-20 12:16:11 +01:00

125 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
function show_help() {
cat <<EOF
Usage: blockable [OPTIONS...] IP [NETWORKGROUP]
-a, --asnip Use the asnip app to look up cidrs
-t, --asntool Use an HTTP request to asntool.com to look up cidrs (default)
-h, --help Show this message and exit
IP The ip number to lookup
NETWORKGROUP The name of the network group that will be used in the edgeos commands
EOF
}
# validate that the value is a valid ip
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# deletes the temp directory
function cleanup {
rm -rf "$TMP_DIR"
echo "Deleted temp working directory $TMP_DIR"
}
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-a|--asnip)
USE_ASNIP=1
shift # past value
;;
-t|--asntool)
shift # past value
;;
-h|--help)
show_help
exit 0
;;
-*|--*)
echo "Unknown option $1"
echo
show_help
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
if [ -z "$1" ]; then
echo You must provide an IP to derive netblocks from
show_help
exit 1
fi
if ! (valid_ip $1); then
echo Invalid IP number $1
echo
show_help
exit 2
fi
TMP_DIR=$(mktemp -d)
# check if tmp dir was created
if [[ ! "$TMP_DIR" || ! -d "$TMP_DIR" ]]; then
echo "Could not create temp dir"
exit 2
fi
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
pushd $TMP_DIR 1>/dev/null
if [ "$USE_ASNIP" == "1" ]; then
asnip -c -t $1
else
CIDRS=$(curl -s -A "blockable.sh script <daniel@demus.dk>" https://asntool.com/$1 | awk '{if (NR<3) {} else {$1=$1}};1')
echo "$CIDRS" | head -2
echo
echo ----------
echo "$CIDRS" | tail -n +3 > cidrs.txt
fi
echo Copy the following to edgeos:
echo
for cidr in $(aggregate -q < cidrs.txt); do
echo set firewall group network-group ${2:-Infiltrators} network $cidr
if [ $(echo $1 | grepcidr -c -e $cidr) -ne 0 ]; then
singleblock=$cidr
fi
done
echo
echo ----------
if [ -n "$singleblock" ]; then
echo "Or the following to only block the specific netblock the ip is in:"
echo
echo set firewall group network-group ${2:-Infiltrators} network $singleblock
echo
echo ----------
fi
popd 1>/dev/null