Tuesday, August 15, 2017

My Route Bash Script

This is my ver 1.0 routing bash script that makes use my other internet connection manually. My application here is route non-important sites like for example here Facebook site.


aroute.sh
#!/bin/bash
# version 1.0
#This script will list all the route and apply it.

#Change your gateway here
gateway="192.168.1.2"

#get the server IP list from the net and dump it to a file
#dig TXT +short _netblocks{,2,3}.google.com | tr ' ' '\n' | grep '^ip4:' > dump
whois -h whois.radb.net -- '-i origin AS32934' | grep  "route:" > fbdump

linenum=`wc -l fbdump`              #get file count
linenum=`echo ${linenum:0:2}`       #trim the output so that it will only leave the integer


while [ $linenum != 0 ]
       do
             str1=`cat fbdump | head -n$linenum | tail -n1`     #get the line content from the file
             str2=`echo ${str1:7}`         #Trim the text
             echo $str2
             nmaskpos=`echo $str2 | grep -aob '/' | grep -oE '[0-9]+'`  #get the position of netmask
             nmask=`echo ${str2:$nmaskpos+1}`   #get the netmask content
             str2=`echo ${str2:0:$nmaskpos}`    #remove the netmask from the network ID

             #get the netmask
             case "$nmask" in
                 16) nmask="255.255.0.0"
                 ;;
                 17) nmask="255.255.128.0"
                 ;;
                 18) nmask="255.255.192.0"
                 ;;
                 19) nmask="255.255.224.0"
                 ;;
                 20) nmask="255.255.240.0"
                 ;;
                 21) nmask="255.255.248.0"
                 ;;
                 22) nmask="255.255.252.0"
                 ;;
                 23) nmask="255.255.254.0"
                 ;;
                 24) nmask="255.255.255.0"
                 ;;
                 25) nmask="255.255.255.128"
                 ;;
                 26) nmask="255.255.255.192"
                 ;;
                 27) nmask="255.255.255.224"
                 ;;
                 28) nmask="255.255.255.240"
                 ;;
                 29) nmask="255.255.255.248"
                 ;;
                 30) nmask="255.255.255.252"
                 ;;
            esac

                #execute the route command
                route add -net $str2 netmask $nmask gw $gateway

             let linenum=$linenum-1
             echo linenum_loop=$linenum
       done

netstat -nr

echo Finished!


On the gateway variable is where you set your other router IP. For example your main gateway is 192.168.1.1 and the other is 192.168.1.2 so to route all facebook requests to 192.168.1.2 just set it on the gateway variable.

If you wish to make the script accept parameters instead of editing the gateway variable change the gateway="192.168.1.2"  to gateway="$1" then to execute the script just do it like this...
# ./aroute.sh 192.168.1.2

This will put the 192.168.1.2 to the variable gateway.
I also left a command to list all google IP in the script as reference to others who like to use it.