2

I'm working through a vpn which is under an unstable connection, so I developed the following script that allows a reconnection if any intermittency in connection with ovpn happens.

#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

function getStatus () {
    ifconfig | grep $1 && return 1
    return 0
}

while [[ 1 ]]; do
    getStatus tun0
    if [[ $? == 0 ]]; then
        echo "openvpn is not connected!"
        echo "Reconnecting!"
                #Replace your_sudo_password to your real user sudo password.
        echo my_sudo_password | sudo -S openvpn --config /home/user/openvpn/my_host_vpn.ovpn
        sleep 6
    fi
    sleep 6
done

The problem is that if I interrupt the connection, nothing happens. Simply displays:

Wed Oct 12 22:59:55 2016 Initialization Sequence Completed

My question is: how I can stop safely vpn connection so that the script can attempt reconnection. Openvpn simply does not release the tun0 interface, so the script believes the connection is still active, What should I do?

franvergara66
  • 10,524
  • 20
  • 59
  • 101
  • 1
    maybe try a different approach, try pinging a network resource that is available only when the VPN connection is active instead of checking tun0 – Omriko Oct 13 '16 at 12:17

2 Answers2

0

OpenVPN already tries to re-establish the connection all by itself, automatically:

If the connection is already established and the tunnel goes down, then openvpn will try to reconnect as long as things like 'keepalive' or 'ping-restart' are set, as well as 'persist-key' and authentication caching.

Ralf Hildebrandt
  • 543
  • 2
  • 16
0

You can use this at the reconnecting process:

echo my_sudo_password | sudo service openvpn@my_host_vpn restart

ghadeer darwesh
  • 185
  • 1
  • 3
  • 12