I use VPN client to connect to my corporate servers. It creates tun0 interface after starting the client. I've written script which install specific routes point to tun0 interface and rest to use normal wifi connection. So that, only my office related traffic goes via VPN and rest are goes via home internet connection. How do I make the script to run automatically when tun0 interface up/down events ?.
I am not sure about
tun0
, but I think the script in/etc/network/if-up.d/
and/etc/network/if-down.d/
are invoked when an interface goes up or down, respectively.Inside the script you can determine which interface is interested from the content of the variable
IFACE
.To be sure, add a simple script to
/etc/network/if-up.d/
which content ismake it executable
then see if the up events are recorded in
/var/log/tun-up.log
Add:
COMMAND
can be a command, likeip route add something...
or a script path with executable permissions (chmod +x
), eventually stored in/etc/network/if-up.d/
.Instead of
up
you may usepost-up
,down
,post-down
.Documentation:
I've used
systemd
to run a script afternetwork-online.target
. My script<path>/script.sh
.1.)
sudo systemctl edit --force --full my-script.service
:2.)
sudo systemctl enable my-script.service
3.)
sudo systemctl start my-script.service