Help with script to observe connection, check speed and change connection

How to customize and extend your OpenVPN installation.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Post Reply
docb
OpenVpn Newbie
Posts: 9
Joined: Fri Sep 29, 2017 4:03 pm

Help with script to observe connection, check speed and change connection

Post by docb » Fri Sep 29, 2017 4:31 pm

Hi all,
I am a bash-script newbie and allready quite proud that I manage to establish a openvpn connection via shell. I spent the whole day in searching ways to query if the openvpn connection is still up and to shut it down. I found several solutions, but I have no clue which one is the "gratefull" one. My goal is the following: I want a script that does the following:
  • 1. as soon as the openvpn connection shuts down (e.g. because provider closes connection), immediately shutdown any network traffic and establish a new connection to another server. I have 4 possible servers, they all use the same config, I only need to change the --remote server port and that is the part that I will be able to manage.
The problem with the methods I found to shut down the connection, the tun0 device is still shown with ifconfig and if I establish a connection to another server there will be a tun1 etc (openvpn config: --dev tun) device. When I do a --dev tunVPN I am not able to open a second connection as tunVPN is still busy. And I am not sure which of the found function really showes that all network traffic goes throu openvpn.
  • 2. I want to change the connection if the speed goes to low. But if I get some more advice to manage my first point, I am pretty sure, I will be able to do that on my own. I allready made a script for speedtesting.
Of course I will post my final script here if anyone else needs it.
Best regards
doc

TinCanTech
OpenVPN Protagonist
Posts: 11137
Joined: Fri Jun 03, 2016 1:17 pm

Re: Help with script to observe connection, check speed and change connection

Post by TinCanTech » Fri Sep 29, 2017 5:42 pm

docb wrote:
Fri Sep 29, 2017 4:31 pm
The problem with the methods I found to shut down the connection, the tun0 device is still shown with ifconfig and if I establish a connection to another server there will be a tun1 etc
Without your configs this is a non-authoritative answer .. but:
Do not drop privileges with --user+group
or make sure the user you assign has the privilege to manage (IE. delete) tun devices.

docb
OpenVpn Newbie
Posts: 9
Joined: Fri Sep 29, 2017 4:03 pm

Re: Help with script to observe connection, check speed and change connection

Post by docb » Fri Sep 29, 2017 5:55 pm

Thanks for the quick answer.
My config is as follows:
Start:

Code: Select all

sudo openvpn --config /networkfolder/ovn.cfg --remote vpn-server.com 1194
And the config file:

Code: Select all

client
dev tunVPN
proto udp
resolv-retry infinite
nobind
persist-key
persist-tun
persist-remote-ip

tls-client
remote-cert-tls server
auth-user-pass /networkfolder/auth.txt
comp-lzo
verb 3

status /networkfolder/openvpn-status.log
log-append /networkfolder/openvpn.log
writepid /networkfolder/openvpn.pid
management 127.0.0.1 1161

auth SHA256
cipher AES-256-CBC

<ca>
-----BEGIN CERTIFICATE-----
alotofletters;-)
-----END CERTIFICATE-----
</ca>


Post Reply