2014年8月5日 星期二

DD-WRT PPTP client redirect gateway

administration --> commands --> commands text area --> save startup

sleep 90
OLDGW=$(nvram get wan_gateway)
VPNSRV=$(nvram get pptpd_client_srvip)
VPNSRVSUB=$(nvram get pptpd_client_srvsub)
PPTPDEV=$(route -n | grep ^${VPNSRVSUB%.[0-9]*} | awk '{print $NF}' | head -n 1)
VPNGW=$(ifconfig $PPTPDEV | grep -Eo "P-t-P:([0-9.]+)" | cut -d: -f2)
route add -host $VPNSRV gw $OLDGW
route del default gw $OLDGW
route add default gw $VPNGW

routing table
root@DD-WRT:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.240.1   0.0.0.0         UG    0      0        0 ppp0
127.0.0.0       *               255.0.0.0       U     0      0        0 lo
169.254.0.0     *               255.255.0.0     U     0      0        0 br0
192.168.0.0     *               255.255.0.0     U     0      0        0 vlan1
192.168.1.0     *               255.255.255.0   U     0      0        0 br0
192.168.240.0   *               255.255.255.0   U     0      0        0 ppp0
192.168.240.1   *               255.255.255.255 UH    0      0        0 ppp0
220.135.10.98   192.168.179.1   255.255.255.255 UGH   0      0        0 vlan1

enhanced script
#!/bin/sh
OLDGW=$(nvram get wan_gateway)
VPNSRV=$(nvram get pptpd_client_srvip)
VPNSRVSUB=$(nvram get pptpd_client_srvsub)
PPTPDEV=$(route -n | grep ^${VPNSRVSUB%.[0-9]*} | awk '{print $NF}' | head -n 1)
while [ ${#PPTPDEV} -eq 0 ]; do
  PPTPDEV=$(route -n | grep ^${VPNSRVSUB%.[0-9]*} | awk '{print $NF}' | head -n 1)
  echo "Retry"
  sleep 5
done
VPNGW=$(ifconfig $PPTPDEV | grep -Eo "P-t-P:([0-9.]+)" | cut -d: -f2)
route add -host $VPNSRV gw $OLDGW
route del default gw $OLDGW
route add default gw $VPNGW

ref: http://junan.name/1544.html