Actually on server side inside the configuration file you can use:
# server vpn interface is up
up "/script/server_up.sh"
# server vpn interface is going down
down "/script/server_down.sh"
# client connected to VPN server
client-connect "script/client_connect.sh"
# client disconnected from VPN server
client-disconnect "script/client_disconnect.sh"
On client side you will use:
# Client connected to VPN server
up "script/connected.sh"
# Client disconnected from VPN server
down "script/disconnected.sh"
OpenVPN will pass a lot of environmental variables to your shell script that you can use for whatever you want.
I have in the past had a script called server_up.sh
that setup a IPv6 tunnel to Hurricane Electric.
Assume the ip address 2001:db8::1 is Hurricane Electric ipv6 standard gateway and the subnet 2001:db8:cafe::/48 is the IPv6 subnet that is routed to me.
Then the content of script/server_up.dh
would be a bit like this:
#!/bin/bash
ip tunnel add he-ipv6 mode sit remote TUNNELBROKER.IPV4.IP.ADDRESS local MY.IPV4.IP.ADDRESS ttl 255
ip link set he-ipv6 up
ip -6 route add default via 2001:db8::1 dev he-ipv6 table openvpn
# Reset ALL ipv6 routes
ip -6 rule flush
# Reinitialise the main IPv6 routing table (inbound traffic) because of reset above
ip -6 rule add priority 32766 from all table main
# Reset OpenVPN routing table (outbound traffic)
ip -6 route flush table openvpn
# Add default unreachable route for any ipv6 subnet not in use.
ip -6 route add unreachable 2001:db8:cafe::/48 table main
ip -6 route add unreachable 2001:db8:cafe::/48 table openvpn
# Add rule to lookup openvpn table if traffic originates from our subnet
ip -6 route add priority 32000 from 2001:db8:cafe::/48 table openvpn
The content of script/server_down.sh
would tear down everything again in reverse order.