blockable: Allow using alternate sources to lookup ASNs
Build and provide rpm of demus shell scripts / rpmbuild (release) Successful in 38s
Build and provide rpm of demus shell scripts / rpmbuild (release) Successful in 38s
Improve robustness and user-friendliness of script
This commit is contained in:
+87
-14
@@ -1,10 +1,84 @@
|
||||
#!/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
|
||||
@@ -13,35 +87,34 @@ if [[ ! "$TMP_DIR" || ! -d "$TMP_DIR" ]]; then
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# deletes the temp directory
|
||||
function cleanup {
|
||||
rm -rf "$TMP_DIR"
|
||||
echo "Deleted temp working directory $TMP_DIR"
|
||||
}
|
||||
|
||||
# register the cleanup function to be called on the EXIT signal
|
||||
trap cleanup EXIT
|
||||
|
||||
pushd $TMP_DIR
|
||||
asnip -c -t $1
|
||||
pushd $TMP_DIR 1>/dev/null
|
||||
if [ "$USE_ASNIP" == "1" ]; then
|
||||
asnip -c -t $1
|
||||
else
|
||||
curl -s -A "blockable.sh script <daniel@demus.dk>" https://asntool.com/$1 | tail -n +3 | awk '{$1=$1};1' > cidrs.txt
|
||||
fi
|
||||
|
||||
echo ----------
|
||||
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 ----------
|
||||
|
||||
echo -n Copy the above to edgeos
|
||||
|
||||
if [ -n "$singleblock" ]; then
|
||||
echo " or for the specific netblock"
|
||||
echo ----------
|
||||
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
|
||||
popd 1>/dev/null
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Name: demus-bash-scripts
|
||||
Version: 0.1.1
|
||||
Version: 0.1.2
|
||||
Release: %autorelease
|
||||
Summary: Useful shell scripts
|
||||
|
||||
|
||||
Reference in New Issue
Block a user