Advertisement
GoodiesHQ

asn.sh

Jan 1st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.00 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ $# -eq 0 ]; then
  4.     echo "You must provide at least one ASN (multiple should be separated by space)."
  5.     exit 0
  6. fi
  7. result=""
  8. dir="/etc/nginx/custom/"
  9. if [ ! -d "$dir" ]; then
  10.     read -p "'$dir' does not exist. Create it? (y/n): " -n 1 -r
  11.     echo
  12.     if [[ $REPLY =~ ^[Yy]$ ]]; then
  13.         mkdir -p $dir 2>/dev/null || { echo "Could not create directory. Exiting."; exit -1; }
  14.     fi
  15. fi
  16. re='^[0-9]+$'   # simple regex to make sure that each ASN is a number
  17. for arg in "$@"; do
  18.     arg=$(sed 's/[AaSs]//g' <<< $arg)
  19.     if [[ $arg =~ $re ]]; then
  20.         result=$(whois -h whois.radb.net -- "-i origin AS${arg}" | grep -Eo "([0-9.]+){4}/[0-9]+")
  21.         #result="$result $temp"
  22.         read -p "Output this ASN to Nginx Blockfile? (y/N): " -n 1 -r
  23.         echo
  24.         if [[ $REPLY =~ ^[Yy]$ ]]; then
  25.             for res in $result; do
  26.                 echo -e "  deny $res;" >> ${dir}AS${arg}.asn 2>/dev/null || { echo "Could not write to file '${dir}AS${arg}.asn'. Exiting."; exit -1; }
  27.             done
  28.         fi
  29.         for res in $result; do
  30.             echo $res
  31.         done
  32.     fi
  33. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement