Skip to content

Instantly share code, notes, and snippets.

@ice-age
Created October 29, 2014 07:20
Show Gist options
  • Save ice-age/34071178cbcb727e1de4 to your computer and use it in GitHub Desktop.
Save ice-age/34071178cbcb727e1de4 to your computer and use it in GitHub Desktop.
生成路由添加批处理文件。国内路由直接走本地,不走vpn
#!/bin/bash
wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -O apnic-ip
echo "@echo off" >cn_route_via_local.bat
cat apnic-ip |grep "^apnic"|grep ipv4|grep CN|awk -F'|' '{print $4,$5}'|while read ip count
do
echo $ip,$count
count=`expr $count - 1`
count=`echo "obase=2;$count"|bc|awk '{print length($0)}'`
mask_count=`expr 32 - $count`
echo "route add $ip/$mask_count 172.16.70.1" >>cn_route_via_local.bat
done
echo "pause" >>cn_route_via_local.bat
unix2dos cn_route_via_local.bat
@ice-age
Copy link
Author

ice-age commented Oct 29, 2014

根据 ip 个数求掩码可以用 log2(count) 函数

  • python 32 - int(math.log(count,2))
  • awk 'BEGIN { fl=(log(count)/log(2)); print fl }'
  • bc echo 'l(count)/l(2)' | bc -l
  • bc 递归函数
    cat << EOF | bc |tail -1
    pow=32;
    define log2(x) {
    if (x<=1) return (pow);
    pow--;
    return(log2(x/2));
    }
    log2(256) 
    EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment