LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Blogs > Musings on technology, philosophy, and life in the corporate world
User Name
Password

Notices

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.

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
Posted in Uncategorized
Views 9594 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  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 08:56 AM.

Main Menu
Advertisement

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