在 ChinaUnix 找到的,利用 whois -A 命令来提取电信/网通信息进而获得相应的网段
http://bbs.chinaunix.net/viewthread.php?tid=577601
http://bbs.chinaunix.net/viewthread.php?tid=577598
- #!/bin/sh
- #filename cn_isp.sh;auto get the IP of CHINANET and CNC;
- rm delegated-apnic-latest
- rm cnnet
- rm IP_CHINANET
- rm IP_UNICOM
- rm IP_CNC
- fetch http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
- grep 'CN|ipv4' delegated-apnic-latest | cut -f 4,5 -d '|' | tr '|' ' ' >> cnnet
- cat cnnet | while read ip cnt
- do
- mask=$(bc < <END | tail -1
- pow=32;
- define log2(x) {
- if (x<=1) return (pow);
- pow--;
- return(log2(x/2));
- }
- log2($cnt);
- END
- )
- resultext=`whois -A $ip | grep -e ^netname -e ^descr -e ^role | cut -f 2 -d ':' | sed 's/ *//'`
- echo '................Search for '$ip/$mask' .........................'
- if echo $resultext | grep -i -e 'chinanet' -e 'chinatel' -e 'china telecom'
- then echo $ip/$mask >> IP_CHINANET
- fi
- if echo $resultext | grep -i -e 'unicom'
- then echo $ip/$mask >> IP_UNICOM
- fi
- if echo $resultext | grep -i -e 'cncgroup'
- then echo $ip/$mask >> IP_CNC
- fi
- echo '----------------------------------------------'
- echo ''
- done
再利用 ccpp0@DRL 写的执行脚本,就可以把网通/电信等IP列表写出路由表中
- #!/bin/sh
- #write By ccpp0
- DEFAULTGW=`/usr/bin/grep defaultrouter /etc/rc.conf | /usr/bin/sed "s/[^0-9\.]//g"`
- #CNCGW=60.28.32.49
- #CNCIP=/root/cn_isp/IP_CNC
- CHINANETGW=221.239.1.193
- CHINANETIP=/root/cn_isp/IP_CHINANET
- /sbin/route flush
- /sbin/route add default ${DEFAULTGW}
- #for RL in `/bin/cat ${CNCIP}`; do
- # /sbin/route add -net ${RL} ${CNCGW}
- #done
- for RL in `/bin/cat ${CHINANETIP}`; do
- /sbin/route add -net ${RL} ${CHINANETGW}
- done
Update:
稍微修正一下,没有必要把所有的网通/电信网段都加进路由表