Created
March 28, 2015 09:37
-
-
Save jerry74/6ab9b7060eca5f85a77c to your computer and use it in GitHub Desktop.
Revisions
-
jerry74 created this gist
Mar 28, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ #!/bin/bash #wget -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /etc/chnroute.txt netcalc(){ local IFS='.' ip i local -a oct msk read -ra oct <<<"$1" read -ra msk <<<"$2" for i in ${!oct[@]}; do ip+=( "$(( oct[i] & msk[i] ))" ) done NET="${ip[*]}" } cidr2mask () { # Number of args to shift, 255..255, first non-255 byte, zeroes set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 [ $1 -gt 1 ] && shift $1 || shift MASK="${1-0}.${2-0}.${3-0}.${4-0}" } cidrsplit () { local IFS='/' local -a ipcidr read -ra ipcidr <<<"$1" IP=${ipcidr[0]} CIDR=${ipcidr[1]} } while read line do cidrsplit $line cidr2mask $CIDR netcalc $IP $MASK echo no-route = $IP/$MASK #echo MASK: $MASK #echo NETWORK: $NET done < chnroute.txt