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?