randybai / autoddvpn

Automatically exported from code.google.com/p/autoddvpn
0 stars 0 forks source link

自制脚本: wget mode下开机启动+pptp断线自动修复重连 #164

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
#!/bin/sh

# variables
vpngateway='192.168.123.123'
checklog='/tmp/autoddvpn.log'
vpnoptionurl='http://www.abc.com/options.vpn'

# initialize in case of running for the first time
if [ ! -f $checklog ]
then
    echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] 1st time running, initialize" >> $checklog
    wget http://autoddvpn.googlecode.com/svn/trunk/pptp/wget/run.sh -O /tmp/run.sh && /bin/sh /tmp/run.sh
    rm /tmp/pptpd_client/options.vpn
    wget $vpnoptionurl -O /tmp/pptpd_client/options.vpn
    cp /tmp/pptpd_client/options.vpn /tmp

    echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] initialization complete" >> $checklog
    exit 1
fi

# regular checking of PPTP connection
ping -c 5 $vpngateway>>/dev/null
if [ $? -ne 0 ]
then
    echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] VPN connection dropped, kill process" >> $checklog
    rm /tmp/autoddvpn.lock
    /bin/sh /tmp/pptpd_client/vpn stop
    kill $(ps|grep pptp)

    # check if vpndown.sh has been properly executed
    tail -1 $checklog|grep "vpndown.sh ended"
    if [ $? -ne  0 ]
    then
        echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] vpndown unsuccessful, redo" >> $checklog
        rm /tmp/autoddvpn.lock
        /bin/sh /tmp/vpndown.sh pptp
    fi

    # re-modify options.vpn
    rm /tmp/pptpd_client/options.vpn
    cp /tmp/options.vpn /tmp/pptpd_client

    # re-modify ip-up if vpnup.sh not exist
    temp=$(tail -2 /tmp/pptpd_client/ip-up|head -1)
    if [ $temp = "esac" ]
    then
        sed -ie 's#exit 0#/tmp/vpnup.sh pptp\nexit 0#g' /tmp/pptpd_client/ip-up
        echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] re-modified ip-up" >> $checklog
    fi

    # re-modify ip-down if vpndown.sh not exist
    temp=$(tail -2 /tmp/pptpd_client/ip-down|head -1)
    if [ $temp = "esac" ]
    then
        sed -ie 's#exit 0#/tmp/vpndown.sh pptp\nexit 0#g' /tmp/pptpd_client/ip-down
        echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] re-modified ip-down" >> $checklog
    fi

    # re-connect VPN
    echo "[INFO#9999] $(date "+%d/%b/%Y:%H:%M:%S") [custom] restart VPN connection" >> $checklog
    /bin/sh /tmp/pptpd_client/vpn start
fi

针对autoddvpn的部分问题我写了以上脚本,适用于以下用户:
1. 路由没有jffs
2. dd-wrt固件是V24, options.vpn格式错误,pptp拨号有问题
3. 希望pptp断线后可以自动重连

使用方法:
1.把这段脚本复制到dd-wrt下的自制脚本那里(Administration->Command
s->Paste)
2.根据自己的设置修改前面三个variable
3.点下Save Custom Script
4.确保页面没有任何其他autoddvpn的脚本在运行
3.在cron设定* * * * * root /tmp/custom.sh或者其他interval

脚本还有很多改善空间,包括加入autoddvpn.lock的等待,大家帮
忙改善改善

Original issue reported on code.google.com by tony.lcl on 24 Aug 2012 at 5:54

GoogleCodeExporter commented 9 years ago
虽然我看不懂是什么东西,但你的做法还是值得尊敬的,无��
�的贡献,加油。。我愿意帮你试试!!

Original comment by jacky19...@gmail.com on 26 Aug 2012 at 4:14

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
干得不错。
现在的情况是这样,wget方式无法获取脚本了,因为被墙。
既然这种方法可以保存脚本在路由器上,为什么不直接把run.s
h vpnup.sh vpndown.sh都保存了呢?就像jffs一样。
一直不太明白为什么这三个脚本要保存到jffs中。

Original comment by freezi...@gmail.com on 19 Jun 2014 at 6:19

GoogleCodeExporter commented 9 years ago
要想重启后脚本还在,就只能保存在nvram,这个家伙的一般都
只有64k这样的大小。妄想把3个脚本都放进去是不可以的,客�
��。
所以jffs也好、wget也好,都是因为这个原因。折中的方法是写
一句短一点的脚本存在nvram里,在开机之后用这个脚本去get以
上3个脚本。
这是我的理解。

Original comment by river.do...@gmail.com on 10 Jul 2014 at 11:24