Hi. I'm a Unix Administrator, mathematics enthusiast, and amateur philosopher. This is where I rant about that which upsets me, laugh about that which amuses me, and jabber about that which holds my interest most: Unix.
vpnc auto-reconnect script
A native Cisco VPN client doesn't exist for OpenBSD, so I wrote this little script to make vpnc more useful. It detects when the vpn tunnel fails and re-establishes it.
Code:
#!/bin/ksh
# This script was developed on OpenBSD with ksh as the primary shell.
# set INT_SERVER to a list of 5 ping-able machines behind your vpn gateway
# ip addresses can be repeated if you have less than five machines.
set -A INT_SERVER 192.168.1.16 192.168.1.201 192.168.1.205 192.168.1.230 192.168.1.129
# this script expects vpnc to be properly configured beforehand with a working profile.
# this script only handles detecting a dropped tunnel and re-establishing the tunnel.
VPNC=/usr/local/sbin/vpnc
DISC=/usr/local/sbin/vpnc-disconnect
TUN=tun0
debug=0 # automatically tuned if vpnc fails...
while [ 1 -eq 1 ]; do # main loop
echo "Connecting to vpn gateway at `date`...";
[ $debug -gt 3 ] && debug=3
$VPNC --debug $debug
[ $? -gt 0 ] && { echo "connection failed!"; debug=`expr $debug + 1`; $DISC; ifconfig $TUN destroy; sleep 1; continue; } || echo "Connected!"
sleep 1; # let the VPN "settle" before testing connectivity
checks=0
while [ $checks -lt 5 ]; do
server=${INT_SERVER[$checks]}
checks=`expr $checks + 1`
success=0
while ping -c 1 -w $checks -i $checks $server >/dev/null 2>&1; do
sleep $checks
success=`expr $success + 1`
[ $success -gt 60 -a $checks -gt 4 ] && { checks=0; echo "Resetting failure count."; }
done
echo "Failed check #$checks - $server"
done
echo "Tunnel dropped at `date`...resetting!"
$DISC 2>/dev/null;
ifconfig $TUN destroy;
sleep 1;
done
Total Comments 2
Comments
-
This was extremely helpful, I had to tweak it a little bit to match my installation but it worked as a charm.
Thanks for sharing it!Posted 11-13-2011 at 11:16 AM by paulocr
-
No problem, paulocr! Glad it helped!Posted 11-17-2011 at 03:13 PM by rocket357



