Skip to content

Instantly share code, notes, and snippets.

@Hexcles
Last active October 9, 2021 05:06
Show Gist options
  • Save Hexcles/5687169 to your computer and use it in GitHub Desktop.
Save Hexcles/5687169 to your computer and use it in GitHub Desktop.
ZJU VPN dial up script (xl2tpd + systemd, for Archlinux)
#!/bin/bash
PPP_TIMEOUT=15
L2TPD_TIMEOUT=5
L2TPD_CONTROL_FILE=/var/run/xl2tpd/l2tp-control
VPN_ACCOUNT=zjuvpn-a
function start_xl2tpd
{
systemctl start xl2tpd
for i in $(seq 0 $L2TPD_TIMEOUT)
do
if [ -e $L2TPD_CONTROL_FILE ]; then
return 0
fi
sleep 1
done
return 1
}
if [ ! -e $L2TPD_CONTROL_FILE ]; then
if ! start_xl2tpd; then
echo "Failed starting xl2tpd"
exit 1
fi
fi
GW=`ip route show 0/0 | cut -d " " -f 3`
if [ ! -z $GW ]; then
ip route add 10.0.0.0/8 via $GW
ip route add 58.196.192.0/19 via $GW
ip route add 58.196.224.0/20 via $GW
ip route add 58.200.100.0/24 via $GW
ip route add 210.32.0.0/20 via $GW
ip route add 210.32.128.0/19 via $GW
ip route add 210.32.160.0/21 via $GW
ip route add 210.32.168.0/22 via $GW
ip route add 210.32.172.0/23 via $GW
ip route add 210.32.176.0/20 via $GW
ip route add 222.205.0.0/17 via $GW
ip route del default
fi
xl2tpd-control connect $VPN_ACCOUNT
for i in $(seq 0 $PPP_TIMEOUT)
do
if ip addr show | grep 'inet.*ppp0' > /dev/null; then
#ip route flush root 10.0.0.0/8 dev ppp0
ip route add default dev ppp0
if ! grep '114' /etc/resolv.conf > /dev/null; then
echo "nameserver 114.114.114.114" >> /etc/resolv.conf
echo "nameserver 114.114.115.115" >> /etc/resolv.conf
fi
echo "Connected"
exit 0
fi
sleep 1
done
xl2tpd-control disconnect $VPN_ACCOUNT
systemctl stop xl2tpd
echo "Connection time out"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment