1

In Ubuntu (the latest distro is fine), I want to reboot a router and inside a bash script I'd like to have a command that waits for the network link to be up again and, when it detects that, it has to start a bash command.

I could implement this with some kind of polling loop, but the ideal solution would be to have a bash command that, when executed, waits for the link to be up and automatically executes a bash command that I gave to it.

I read something about dbus (and dbus seems the way to go) but it also seems that it takes too much time to fully understand how to use it properly. I was suggested to check if a tool like ethtool was able to do that kind of "wait and execute" but in the man pages I didn't find anything about it.

Note: I forgot to say that I'd like the command to check if the PHYSICAL layer of the link is up. So solutions working at upper layers are not accepted. Moreover, solutions involving putting scripts inside directories (such as/etc/network/if-up.d) are not accepted too.


Any ideas?

Thank you

condorwasabi
  • 616
  • 6
  • 19

3 Answers3

1

The event listener I suggested:

inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'

When you plug or unplug network cable, it will trigger echo 'Change detected', of course it could trigger just about anything.

And this will run as one off, but I take you know how to make a daemon out of it, if not it will be a good exercise to learn :)

Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
  • The only thing I've got to test (right now I can't) is if the value in /sys/class/net/eth0/carrier changes even if the cable is plugged in but the router has been rebooted – condorwasabi Jul 04 '14 at 12:25
  • Is the host plugged directly into the router? – Tiago Lopo Jul 04 '14 at 12:27
  • Yes it is. I've implemented some tests in bash and one of these tests requires that when the router is rebooted my bash script executes a command. – condorwasabi Jul 04 '14 at 12:34
  • 1
    I tried to use inotify but it seems not to detect when /sys/class/net/eth0/carrier is modified. I read that inotify can't detect changes in all the files in sysfs. This might be one of those cases. Just a question: had you tried your solution on your computer? – condorwasabi Jul 04 '14 at 17:11
  • 1
    @condorwasabi, One of the files that changes (opened or accessed) whenever the link is up is `/etc/network/if-up.d/avahi-daemon` file, on which you can use `inotifywait`, then you can check `/sys/class/net/eth0/carrier` file for its value ( 0 or 1 ). – lind Jul 04 '14 at 17:24
  • @lind, at the end your solution was the one that I used. Thanks. – condorwasabi Jul 25 '14 at 08:24
0

If you want a command to check if the link is up or down use ip :

ip addr show eth0 | grep -Po "(?<=state ).*?(?=\s)"
DOWN

ip addr show wlan0 | grep -Po "(?<=state ).*?(?=\s)"
UP
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
0

You can check link is up/down by /sys/class/net/eth0/carrier file

cat /sys/class/net/eth0/carrier

if output is 1 then ethernet cable is plugged in and link is up.

if output is 0 then ethernet cable is removed and link is down.

Note:if you have interface other then eth0 then replace interface like eth2/eth3 in place of eth0

Rahul R Dhobi
  • 5,668
  • 1
  • 29
  • 38
  • Both your solution and @Tiago 's one are good ones but they are of the "polling type". If we could find a command that doesn't require me to do explicit polling it'd be the ideal solution – condorwasabi Jul 03 '14 at 09:08
  • @condorwasabi you could use `inotify` as event listener in case a change happens on `/sys/class/net/eth0/carrier` – Tiago Lopo Jul 03 '14 at 20:13