Skip to content

Instantly share code, notes, and snippets.

@lowstz
Forked from xream/MacVPN.sh
Created February 8, 2012 15:22
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save lowstz/1770403 to your computer and use it in GitHub Desktop.
Save lowstz/1770403 to your computer and use it in GitHub Desktop.
Linux openvpn auto reconnect script
#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
### Thanks Xream's Work XD
# if you don't have several vpn servers to select, you can comment following line
# and use your openvpn config file name to replace "${host}.ovpn" in while loop.
read -p "Select the host: " host
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 your_sudo_password | sudo -S openvpn --config /home/user/openvpn/${host}.ovpn
sleep 6
fi
sleep 6
done
@sveip
Copy link

sveip commented Nov 18, 2015

You can achieve the same by adding keepalive 10 120 to your conf, as stated in the docs.

@sajjadintel
Copy link

where the username should be enterd?

@szewczyk-it
Copy link

@SajadAbasi, in *.ovpn file you can add entry
auth-user-pass up.txt
and in file up.txt:

your_username
your_password

@damien990
Copy link

damien990 commented May 24, 2018

Can anybody state out the procedure how to do it?

I have this error when execute the script on my/path/directory,

utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::11b6:ff42:907:ed7e%utun0 prefixlen 64 scopeid 0xb
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::11b6:ff42:907:ed7e%utun0 prefixlen 64 scopeid 0xb
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::11b6:ff42:907:ed7e%utun0 prefixlen 64 scopeid 0xb

And other issue is not clear of the scripte
#Replace your_sudo_password to your real user sudo password.

Thanks

@damien990
Copy link

Finally it is fixed!
This is because of the getStatus command not work on my MBP. I comment it out then everything work.
Add the script directory /path/to/my/myscript.sh into the /usr/home~ .profile and .bash_profile and than logout & login again.

@pingaan
Copy link

pingaan commented Feb 25, 2019

Hey!
I keep getting the error message:
$/home/pi/.VPN/auto-ovpn2.sh: 6: /home/pi/.VPN/auto-ovpn2.sh: Syntax error: "(" unexpected

The script seems to output an error already on the 11th row. What could I be doing wrong? When selecting host on the 9th row, what should I pick btw? Simply "host"?

@pingaan
Copy link

pingaan commented Feb 26, 2019

I worked it out.

For some reason running the script with "sh" ended up in this error while ./ worked just fine. Didn't know there were any differences.

Cheers for a neat script!

@pingaan
Copy link

pingaan commented Mar 2, 2019

I have another question though... Is it possible to skip the step on the 9th line? "Select the host: "?
I'd like for the script to run at boot up. Perhaps there is a nother way to launch it at boot up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment