Advertisement
mark2011

Untitled

Apr 7th, 2019
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.34 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #A script which blacklists an IP or unblocks an IP
  4. #Parameters:
  5. #
  6. #-i <string>
  7. #-a <list|delist>
  8. #-d <number>
  9.  
  10. basename=`basename $0`
  11. NO_ARGS=0
  12.    
  13. help()
  14. {
  15.     echo "This script helps to block IP addresses. You can work both with IPv4 and IPv6. These IPs you can block and unblock. When necessary, use the blocking feature from file"
  16.     echo "Usage:"
  17.     echo "";
  18.     echo "-i | --ip-address IP address which is to be blocked or unblocked"
  19.     echo "-a | --action action, which is to be taken (list, delist, block, unblock). List/delist is for single IPs whereas block/unblock is for file operations"
  20.     echo "-f | --block-from-file full path to the file name which contains IPs to block"
  21.     echo "-c | --count just count IPs that were already blocked"
  22.     echo "-v | --version Echoes the version of this script"
  23.     echo "-h | --help Prints this help message"
  24.     exit 1
  25. }
  26.    
  27. valid_ip_v4()
  28. {
  29.         local  ip=$1
  30.         local  stat=1
  31.        
  32.             if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/{,1}[0-9]{,2}$ ]]; then
  33.                 OIFS=$IFS
  34.                 IFS='.'
  35.                 ip=($ip)
  36.                 IFS=$OIFS
  37.                 [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  38.                 stat=$?
  39.             fi
  40.             return $stat
  41. }
  42.  
  43.  
  44. valid_ip_v6()
  45. {
  46.     file="$(mktemp /tmp/IPLogic.XXXXXX)"
  47.     ipcalc -c "$1" 2>$file
  48.    
  49.     size=`du -b $file | awk ' {print $1}'`
  50.  
  51.     if [[ $size > 0 ]]
  52.     then
  53.     echo "0"
  54.     else
  55.     echo "1"
  56.     fi
  57.     unlink $file
  58. }
  59.    
  60. usage()
  61. {
  62.     echo "Usage: $basename [-i <string>] [-a <list|delist>] [-f FILE_NAME]" 1>&2; exit 1;
  63. }
  64.  
  65. block_ip_v4()
  66. {
  67.     if [ -z "$1" ]
  68.     then
  69.         exit 1
  70.     fi
  71.    
  72.    
  73.     if valid_ip_v4 $1;
  74.     then
  75.     stat="good"
  76.     else
  77.     stat="bad"
  78.     fi
  79.     if [[ $stat = "bad" ]]
  80.     then
  81.     echo "This is not a valid IP address. Exiting..."
  82.     exit 1
  83.     fi
  84.    
  85.     str=`ip route | grep $1 | tr -d "blackhole"`
  86.     strlength=`echo -n $str | wc -c`
  87.  
  88.     if [ $str = "$1" 2>/dev/null ]
  89.     then
  90.     echo "This IP has been already blocked. No need to block again. Try to unblock instead"
  91.     exit 1
  92.     fi
  93.     ip route add blackhole $1
  94.  
  95.     str=`ip route | grep "$1" | tr -d "blackhole"`
  96.     strlength=`echo -n $str | wc -c`
  97.     if [ $strlength > 0 ] && [ $str = "$1" ];
  98.     then
  99.     echo "IP $IP has been successfully blocked"
  100.     exit 0
  101.     else
  102.     echo "There was a problem while blocking IP $IP"
  103.     fi
  104. }
  105.  
  106.  
  107. block_ip_v6()
  108. {
  109.     if [ -z "$1" ]
  110.     then
  111.     exit 1
  112.     fi
  113.     if [[ $(valid_ip_v6 $1) = "1" ]]
  114.     then
  115.     ip -6 route add blackhole $1
  116.     str=`ip -6 route | grep "$1" | tr -d "blackhole"`
  117.     strlength=`echo -n $str | wc -c`
  118.     if [[ $strlength > 0 ]];
  119.     then
  120.         echo "IP $1 has been successfully blocked"
  121.         exit 0
  122.     else
  123.         echo "There was a problem blocking IP $1"
  124.     fi
  125.     else
  126.     echo "IP $1 doen't seem to be a valid IPv6 address"
  127.     fi
  128. }
  129.  
  130. unblock_ip_v4()
  131. {
  132.     if [ -z "$1" ]
  133.     then
  134.     exit 1
  135.     fi
  136.    
  137.     str=`ip route | grep $1`
  138.     strlength=`echo -n $str | wc -c`
  139.    
  140.     if [ $strlength = 0 ]
  141.     then
  142.     echo "IP $1 is not in blackhole. Nothing to delist"
  143.     exit 0
  144.     else
  145.     ip route del blackhole $1
  146.     str=`ip route | grep $1`
  147.     strlength=`echo -n $str | wc -c`
  148.     if [ $strlength = "0" ]
  149.     then
  150.         echo "IP $1 has been successfully delisted from the blackhole"
  151.         exit 0
  152.     else
  153.         echo "There was an error while delisting IP $1"
  154.     fi
  155.     fi
  156.    
  157. }
  158.  
  159.  
  160. block_from_file()
  161. {
  162.     if [ -z "$1" ]
  163.     then
  164.     exit 1
  165.     fi
  166.    
  167.     if [ ! -f "$1" ]
  168.     then
  169.     echo "File $1 does not exist"
  170.     exit 1
  171.     else
  172.     input="$1"
  173.     for ip in `cat $input`
  174.         do
  175.         if [[ $ACTION = "block" ]]
  176.         then
  177.             if valid_ip_v4 $ip;
  178.             then
  179.             ip route add blackhole $ip 2>/dev/null
  180.             str=`ip route | grep $ip | tr -d "blackhole"`
  181.             strlength=`echo -n $str | wc -c`
  182.                 if [ $str = "$ip" ]
  183.                 then
  184.                 echo "IP $ip has been successfully blocked"
  185.                 else
  186.                 echo "There was a problem blocking IP $ip"
  187.                 fi
  188.             else
  189.             echo "IP $ip is not a valid IP address. Exiting..."
  190.             fi
  191.            
  192.         elif [[ $ACTION = "unblock" ]]
  193.         then
  194.         if valid_ip_v4 $ip;
  195.         then
  196.             ip route del blackhole "$ip"
  197.             str=`ip route | grep $ip | tr -d "blackhole"`
  198.             strlength=`echo -n $str | wc -c`
  199.             if [[ $strlength = 0 ]]
  200.             then
  201.                 echo "IP $ip has been successfully unblocked"
  202.             else
  203.                 echo "There was a problem unblocking IP $ip"
  204.             fi
  205.         else
  206.             echo "IP $ip doesn't seem to be a valid IP address. No need to unblock"
  207.         fi
  208.         fi
  209.     done
  210.     fi
  211. }
  212.  
  213. unblock_ip_v6()
  214. {
  215.     if [ -z "$1" ]
  216.     then
  217.     exit 1
  218.     fi
  219.    
  220.    
  221.     str=`ip -6 route | grep $1`
  222.     strlength=`echo -n $str | wc -c`
  223.    
  224.     if [ $strlength = "0" ]
  225.     then
  226.     echo "IP $1 is not in blackhole. Nothing to delist"
  227.     exit 0
  228.     else
  229.     ip -6 route del blackhole $1
  230.     str=`ip route | grep $1`
  231.     strlength=`echo -n $str | wc -c`
  232.     if [ $strlength = "0" ]
  233.     then
  234.         echo "IP $1 has been successfully delisted from the blackhole"
  235.         exit 0
  236.     else
  237.         echo "There was an error while delisting IP $1"
  238.     fi
  239.     fi
  240. }
  241.    
  242. count()
  243. {
  244.     echo "IPv4 blocked: " `ip route | wc -l`
  245.     echo "IPv6 blocked: " `ip -6 route | wc -l`
  246.     return 0
  247. }
  248.  
  249. if [ $# = $NO_ARGS ]
  250. then
  251.     usage
  252. fi
  253.  
  254.  
  255.  
  256. while getopts "i:a:dcf:vh" arg; do
  257.     case $arg in
  258.     i)  IP=${OPTARG}
  259.         ;;
  260.  
  261.     a)  ACTION=${OPTARG}
  262.         ;;
  263.  
  264.     d)  IPVERSION=${OPTARG}
  265.         if [[ $IPVERSION != 4 && $IPVERSION != 6 ]]
  266.         then
  267.             usage
  268.         fi
  269.         ;;
  270.        
  271.     c)  count
  272.         exit 0
  273.         ;;
  274.  
  275.     f)  block_from_file ${OPTARG}
  276.         exit 0
  277.         ;;
  278.        
  279.     v)  echo "IP blocking script; version 0.1"
  280.         exit 0
  281.         ;;
  282.        
  283.     h)  help
  284.         ;;
  285.        
  286.     *)  usage
  287.         ;;
  288.     esac
  289. done
  290.  
  291.  
  292. if [[ $ACTION != "list" && $ACTION != "delist" ]]
  293. then
  294.     usage
  295. fi
  296.  
  297. if [[ -z $IPVERSION ]]
  298. then
  299.     IP_VERSION=4
  300. else
  301.     IP_VERSION=$IPVERSION
  302. fi
  303.  
  304. case $ACTION in
  305.     list)
  306.     if [[ $IP_VERSION = 4 ]]
  307.     then
  308.         block_ip_v4 $IP
  309.         exit 0
  310.     else
  311.         block_ip_v6 $IP
  312.         exit 0
  313.     fi
  314.     exit 0
  315.     ;;
  316.    
  317.     delist) if [[ $IP_VERSION = 4 ]]
  318.     then
  319.         unblock_ip_v4 $IP
  320.     else
  321.         unblock_ip_v6 $IP
  322.     fi
  323.     exit 0
  324.     ;;
  325.    
  326.     count)  count
  327.     ;;
  328.    
  329.     block)  block="1"
  330.     ;;
  331.    
  332.     unblock)    block="0"
  333.     ;;
  334.    
  335.     *)  usage
  336.     exit 1
  337. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement