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
| #!/bin/bash
# get two parameters : ip and port, try to connect to ssl vpn. if success return 0, else return 1
if [ "x$1" == "x" ];
then
echo "Usage: vpn.sh connect ip port ";
echo " vpn.sh disconnect ";
exit 1;
fi
if [ "x$1" == "xdisconnect" ];
then
# delete old sslvpn and collect original ip address for route delete
echo $(cat vpndel.cmd | vpncmd | grep "^VPN Server Hostname" | cut -d "|" -f 2 | cut -d " " -f 1 | cut -d ":" -f 1 ) | xargs sudo ./route.sh del;
exit 0;
fi
if [ "x$1" == "xconnect" ];
then
if [ "x$2" == "x" ] || [ "x$3" == "x" ];
then
echo "Usage: vpn.sh connect ip port ";
exit 1;
fi
fi
# delete old sslvpn and collect original ip address for route delete
echo $(cat vpndel.cmd | vpncmd | grep "^VPN Server Hostname" | cut -d "|" -f 2 | cut -d " " -f 1 | cut -d ":" -f 1 ) | xargs sudo ./route.sh del;
# ensure ip/port is open via nc(netcat).
alive=$(nc -z --timeout=1 $2 $3 )
if [ "x$alive" == "x" ]; then
echo "nc $2:$3 succeeds.";
else
echo "nc $2:$3 fails.";
exit 1;
fi
# generate ssl.vpn
./vpndef.sh $2 $3
# delete old sslvpn and collect original ip address for route delete
echo $(cat vpndel.cmd | /usr/local/vpnclient/vpncmd | grep "^VPN Server Hostname" | cut -d "|" -f 2 | cut -d " " -f 1 | cut -d ":" -f 1 ) | xargs sudo ./route.sh del
# import new vpn definition
cat vpnimport.cmd | /usr/local/vpnclient/vpncmd
cat vpnconnect.cmd | /usr/local/vpnclient/vpncmd
sleep 3
isConnected=$(cat vpnlist.cmd | /usr/local/vpnclient/vpncmd | grep Status | cut -d "|" -f 2)
echo "vpn status is $isConnected"
if [ $isConnected != "Connected" ];
then
#disconnect the old vpn first
./vpn.sh disconnect
exit 1;
fi;
# add new route
echo $(cat vpnlist.cmd | /usr/local/vpnclient/vpncmd | grep "^VPN Server Hostname" | cut -d "|" -f 2 | cut -d " " -f 1 | cut -d ":" -f 1 ) | xargs sudo ./route.sh add
# test if vpnconnection is up
connected=$(nc -z --timeout=1 www.youtube.com 80 )
if [ "x$connected" == "x" ]; then
echo "vpn connection to $2:$3 succeeds.";
exit 0;
else
echo "vpn connection to $2:$3 fails.";
exit 1;
fi |