Networking – Run Script Automatically on tun0 Interface Up/Down Events

ifconfiginterfaceinternetnetworking

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 ?.

Best Answer

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 is

#!/bin/sh
# filename: tun-up

if [ "$IFACE" = tun0 ]; then
  echo "tun0 up" >> /var/log/tun-up.log
fi

make it executable

sudo chmod +x /etc/network/if-up.d/tun-up

then see if the up events are recorded in /var/log/tun-up.log