Register a domain and help support LQ
Go Back > Blogs > Musings on technology, philosophy, and life in the corporate world
User Name


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.
Rate this Entry

vpnc auto-reconnect script

Posted 07-14-2011 at 02:05 AM by rocket357
Updated 07-15-2011 at 01:38 AM by rocket357

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.


# 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.

# 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.

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

        while [ $checks -lt 5 ]; do

                checks=`expr $checks + 1`

                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."; }


                echo "Failed check #$checks - $server"

        echo "Tunnel dropped at `date`...resetting!"
        $DISC 2>/dev/null;
        ifconfig $TUN destroy;
        sleep 1;
Posted in Uncategorized
Views 10039 Comments 2
« Prev     Main     Next »
Total Comments 2


  1. Old Comment
    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 paulocr is offline
  2. Old Comment
    No problem, paulocr! Glad it helped!
    Posted 11-17-2011 at 03:13 PM by rocket357 rocket357 is offline


All times are GMT -5. The time now is 09:02 AM.

Main Menu

Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration