forked from enjoydiy/ttautovpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpndown.sh
executable file
·96 lines (75 loc) · 2.26 KB
/
vpndown.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
#
# E-mail:[email protected]
# http://bbs.enjoydiy.com
#
set -x
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
LOG='/tmp/autoddvpn.log'
LOCK='/tmp/autoddvpn.lock'
PID=$$
INFO="[INFO#${PID}]"
DEBUG="[DEBUG#${PID}]"
ERROR="[ERROR#${PID}]"
CHINART='/jffs/down.sh'
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") vpndown.sh started" >> $LOG
for i in 1 2 3 4 5 6
do
if [ -f $LOCK ]; then
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") got $LOCK , sleep 10 secs. #$i/6" >> $LOG
sleep 10
else
break
fi
done
if [ -f $LOCK ]; then
echo "$ERROR $(date "+%d/%b/%Y:%H:%M:%S") still got $LOCK , I'm aborted. Fix me." >> $LOG
exit 0
#else
# echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") $LOCK was released, let's continue." >> $LOG
fi
# create the lock
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") vpnup" >> $LOCK
OLDGW=$(nvram get wan_gateway)
case $1 in
"pptp")
case "$(nvram get router_name)" in
"tomato")
#VPNSRV=$(nvram get pptpd_client_srvip)
#VPNSRVSUB=$(nvram get pptpd_client_srvsub)
#PPTPDEV=$(nvram get pptp_client_iface)
VPNGW=$(nvram get pptp_client_gateway)
VPNUPCUSTOM='/jffs/pptp/vpnup_custom'
;;
"DD-WRT")
PPTPSRV=$(nvram get pptpd_client_srvip)
VPNUPCUSTOM='/jffs/pptp/vpnup_custom'
VPNGW=$(nvram get pptp_gw)
;;
esac
;;
"openvpn")
OPENVPNSRV=$(nvram get openvpncl_remoteip)
OPENVPNDEV='tun0'
VPNGW=$(ifconfig $OPENVPNDEV | grep -Eo "P-t-P:([0-9.]+)" | cut -d: -f2)
VPNUPCUSTOM='/jffs/openvpn/vpnup_custom'
;;
*)
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") unknown vpndown.sh parameter, quit." >> $LOCK
exit 1
;;
esac
echo "[INFO] removing the static routes"
##### begin batch route #####
grep ^route $CHINART | /bin/sh -x
if [ -f $VPNUPCUSTOM ]; then
grep ^route $VPNUPCUSTOM | sed -e 's/add/del/' | sed -e 's/ gw $OLDGW//' | sed -e 's/ gw $VPNGW//' | /bin/sh -x
fi
##### end batch route #####
#route del -host $PPTPSRV
route del default gw $VPNGW
echo "$INFO add $OLDGW back as the default gw"
route add default gw $OLDGW
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") vpndown.sh ended" >> $LOG
# release the lock
rm -f $LOCK