How can I associate a script to OpenVPN so that it runs when the VPN is connected successfully?
From the OpenVPN manpage:
--script-security level [method] This directive offers policy-level control over OpenVPN’s usage of external programs and scripts. Lower level values are more restrictive, higher values are more permissive. Settings for level: 0 -- Strictly no calling of external programs. 1 -- (Default) Only call built-in executables such as ifconfig, ip, route, or netsh. 2 -- Allow calling of built-in executables and user-defined scripts. 3 -- Allow passwords to be passed to scripts via environmental variables (potentially unsafe). --up cmd Shell command to run after successful TUN/TAP device open (pre --user UID change). The up script is useful for specifying route commands which route IP traffic destined for private subnets which exist at the other end of the VPN connection into the tunnel. Script Order of Execution --up Executed after TCP/UDP socket bind and TUN/TAP open. --down Executed after TCP/UDP and TUN/TAP close.
There are more events for script execution, those can be found on the manual page.
Note that this
this may be well known and obvious to many people, but I stuck with that for some time with no clue. Just to possibly save some time to others:
The script has to include the shell info e.g. "#!/bin/sh" to get successfully executed. Until this was there, openvpn kept complaining "script failed: could not execute external program" even though the script was there with all the executable flags and everything.