How to detect VPN disconnection with vpnc?

The script /etc/vpnc/vpnc-script is called on various events, including connect and disconnect.

This sounds like you are using ubuntu or debian. If you are, the scripts

/etc/vpnc/vpnc-script-disconnect-action

and

/etc/vpnc/vpnc-script-post-disconnect-action

are called for the relevant events. You can create those files if they don't exist, and put whatever logic you want in there.

If you aren't using a debian-based distribution with these sub-scripts, you can modify /etc/vpnc/vpnc-script directly to add whatever logic you want.

If you are going to have logic to call vpnc-connect again, I recommend disconnecting that from the disconnect script somehow so you don't end up in an infinite loop.


I use a cron to take care of it so I can easily change it without having to remember what script I edited. The cron job is below.

*/15 * * * * [[ -d /sys/devices/virtual/net/tun0 ]] || /usr/sbin/vpnc /etc/vpnc/yourconf.conf


Typically the VPN connection should not break so frequently. In your case, you can first try to diagnose the real connectivity issue instead of writing scripts to automatically connect to the vpn when disconnected.