1

I have eth0 device and tun0 device (VPN) and I would like to automatically detect if any of these devices changes status, i.e. they are up or down.

In each case, I would like to automatically call a predefined script. So:

  • if eth0 goes down, I would like to call script 1
  • if tun0 goes down, script 2
  • if tun0 goes up, script 3

How to do that with NetworkManager?

For tun0, I can use /etc/network/interfaces:

auto tun0
iface tun0 inet manual
    up /bin/sh /home/matej/tun-up.sh
    down /bin/sh /home/matej/tun-down.sh

What about eth0, since network is managed by Network Manager?

1

1 Answer 1

1

Thanks a lot! This is what I did:

sudo nano /etc/NetworkManager/dispatcher.d/02-events

With the content:

#!/bin/sh -e

if [ "$1" = "eth0" ] && [ "$2" = "down" ] ; then
echo "eth0 is down!" >> /home/matej/dispatch.txt
fi

if [ "$1" = "tun0" ] && [ "$2" = "down" ] ; then
echo "VPN is down!" >> /home/matej/dispatch.txt
fi

if [ "$1" = "tun0" ] && [ "$2" = "up" ] ; then
echo "VPN is up!" >> /home/matej/dispatch.txt
fi

Make it executable:

sudo chmod +x /etc/NetworkManager/dispatcher.d/02-events

Restart NM:

sudo service network-manager restart

This works as expected, but when I unplug the ethernet cable, there is around 10 seconds timeout, before NetworkManager detects an event. Could this be shortened to let's say 5 seconds?

2
  • Restarting NetworkManager is seldom necessary nor correct.
    – thaller
    Mar 6, 2021 at 3:12
  • For the delay when unplugging the cable see device.carrier-wait-timeout in man NetworkManager.conf.
    – thaller
    Mar 6, 2021 at 3:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .