Skip to content

Instantly share code, notes, and snippets.

@greenqy
Forked from Hexcles/vpn-dorm.sh
Last active September 3, 2015 15:08
Show Gist options
  • Save greenqy/ac1395f8fa09dec883ed to your computer and use it in GitHub Desktop.
Save greenqy/ac1395f8fa09dec883ed to your computer and use it in GitHub Desktop.
ZJU Zijingang Campus dormitory VPN dial up script (xl2tpd + systemd, for Archlinux)
#!/bin/bash
PPP_TIMEOUT=30
L2TPD_TIMEOUT=5
L2TPD_CONTROL_FILE=/var/run/xl2tpd/l2tp-control
VPN_ACCOUNT=zjuvpn
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
xl2tpd-control disconnect $VPN_ACCOUNT
systemctl stop xl2tpd
fi
if ! start_xl2tpd; then
echo "Failed starting xl2tpd"
return 1
fi
GW=`ip route show 0/0 | cut -d " " -f 3`
if [ ! -z $GW ]; then
#Attention here! Intranet 10.0.0.0/8(except the DNS) must be connected through VPN in dorm.
ip route add 10.5.1.0/24 via $GW
ip route add 10.10.0.0/24 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