¿Cómo puedo asociar un script a OpenVPN para que se ejecute cuando la VPN se conecte correctamente?
Mejor respuesta
network-manager-openvpn no proporciona dicha funcionalidad, debe usar openvpn directamente.
Pase --script-security 2 --up /path/to/your/script a él cuando se conecta. Si está utilizando un archivo de configuración ubicado en /etc/openvpn/ , agregue las siguientes líneas a su archivo de configuración:
script-security 2
# run /etc/openvpn/up.sh when the connection is set up
up /etc/openvpn/up.sh
Desde la página de manual de OpenVPN:
--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.
Hay más eventos para la ejecución de secuencias de comandos, que se pueden encontrar en la página del manual.
Crear /etc/openvpn/up.sh y darle permisos de ejecución (por ejemplo, 755 o 700). Contenido de ejemplo para agregar una dirección y ruta IPv6 (se muestra con fines educativos, no lo copie directamente):
#!/bin/sh
# add an IPv6 address to device $dev (environment variable)
ip -6 addr add 2001:db8::1:2/112 dev $dev
# and the IPv6 route for this net using gateway 2001:db8::1
ip -6 route add 2001:db8::1:0/112 via 2001:db8::1 dev $dev
Tenga en cuenta que este up el script se ejecuta como root. Si no ha especificado un User y Group configuración, OpenVPN ejecutará scripts como down como root también.