How to execute multiple scripts when openvpn establishes?

I am using ubuntu 12.04 to connect to an openvpn server.

Two scripts, update-resolv-conf and vpn-up.sh, in /etc/openvpn should be executed when the connection established.

The following commands are added to the configure file client.ovpn to make the two scripts run when openvpn connects and stop when openvpn disconnects:

script-security 2
up /etc/openvpn/update-resolv-conf
up /etc/openvpn/vpn-up.sh
down /etc/openvpn/vpn-down.sh
down /etc/openvpn/update-resolv-conf

However, during connection only the first script is executed. And every time I have to run the script vpn-up.sh individually by hand. So I want to know how to get them both work when openvpn tunnel is established?


Solution 1:

A quick hack would be to call the 2nd script from the end of the last one:

Just add the following to the end of the '/etc/openvpn/update-resolv-conf' script:

/etc/openvpn/vpn-up.sh

I'm sure people here will provide a more elegant way to do this.

Solution 2:

Just for reference. If you have a few scripts and timing isn't crucial you can also use another parameter for executing scripts.

Script Order of Execution

--up
Executed after TCP/UDP socket bind and TUN/TAP open.
--tls-verify
Executed when we have a still untrusted remote peer.
--ipchange
Executed after connection authentication, or remote IP address change.
--client-connect
Executed in --mode server mode immediately after client authentication.
--route-up
Executed after connection authentication, either immediately after, or some number of seconds after as defined by the --route-delay option.
--client-disconnect
Executed in --mode server mode on client instance shutdown.
--down
Executed after TCP/UDP and TUN/TAP close.
--learn-address
Executed in --mode server mode whenever an IPv4 address/route or MAC address is added to OpenVPN's internal routing table.
--auth-user-pass-verify
Executed in --mode server mode on new client connections, when the client is still untrusted.

https://openvpn.net/index.php/open-source/documentation/manuals/65-openvpn-20x-manpage.html

Solution 3:

up '/bin/bash -c "/etc/openvpn/update-resolv-conf; /etc/openvpn/vpn-up.sh"'