Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[建议]增加一个generate_ddwrt模式 #3

Open
sagan opened this issue Aug 28, 2012 · 2 comments
Open

[建议]增加一个generate_ddwrt模式 #3

sagan opened this issue Aug 28, 2012 · 2 comments

Comments

@sagan
Copy link

sagan commented Aug 28, 2012

  1. dd wrt只有 /bin/sh,没有 /bin/bash
  2. 目前dd wrt不支持 ip -batch
  3. 目前dd wrt无法用ip route show 0/0 | head -n1 | grep 'via' | grep -Po '\d+.\d+.\d+.\d+' 获取默认网关, 需要用nvram get wan_gateway
def generate_ddwrt(metric):
    results = fetch_ip_data()

    upscript_header = """\
#!/bin/sh

export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin:/mmc/usr/sbin:/mmc/usr/bin:/opt/sbin:/opt/bin:/opt/usr/sbin:/opt/usr/bin"

OLDGW=$(nvram get wan_gateway)
if [ $OLDGW == '' ]
then
    exit 1
fi

"""
    downscript_header = """\
#!/bin/sh

export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin:/mmc/usr/sbin:/mmc/usr/bin:/opt/sbin:/opt/bin:/opt/usr/sbin:/opt/usr/bin"

"""

    upfile = open('vpn-up.sh', 'w')
    downfile = open('vpn-down.sh', 'w')

    upfile.write(upscript_header)
    downfile.write(downscript_header)

    for ip, mask, _ in results:
        upfile.write('route add -net %s netmask %s gw $OLDGW metric %d\n' % (ip, mask, metric))
        downfile.write('route del -net %s netmask %s metric %d\n' % (ip, mask, metric))

    upfile.write('\n')
    downfile.write('\n')

    upfile.close()
    downfile.close()

    os.chmod('vpn-up.sh', 00755)
    os.chmod('vpn-down.sh', 00755)
@jimmyxu
Copy link
Owner

jimmyxu commented Sep 2, 2012

PATH略长啊…手头没DD-WRT设备,而且觉得这种没iproute2的还是用mac版稍微改改吧…

@wzyboy
Copy link

wzyboy commented Sep 18, 2012

Android 里 busybox 的那个 grep 也不带 -P,并且 ip 也不能用 -batch,所以我在 Android 上用的时候是这样的:

#!/system/xbin/bash -
OLDGW=$(ip route show 0/0 | head -n1 | grep 'via' | awk '{print $3}')
ip route add 1.0.1.0/24 via $OLDGW
ip route add 1.0.2.0/23 via $OLDGW
ip route add 1.0.8.0/21 via $OLDGW
....

执行时间倒也不是特别长,三千多条记录,在 vision 上大概 30s 也就执行完毕了,比 route add 好多了……

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants