Created
March 28, 2015 09:37
-
-
Save jerry74/6ab9b7060eca5f85a77c to your computer and use it in GitHub Desktop.
chnroute CID2Mast
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 characters
| #!/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment