views:

138

answers:

4

I want to setup my computer to run certain commands when I join or leave certain networks (ie: Start up Synergy when at work; Start up ssh when on the lan at home so I can sync, etc...).

I found iwevent which outputs events to the wireless interface that I can watch. However I can't find an equivalent to watch when eth0 is connected or disconnected. I tried ip monitor but there's so much data outputted I can't make use of it sanely.

Is there any command I can use to monitor state changes to eth0? An API available to Python works too.

A: 

Use netstat -i -c which will run continuosly every second....

tommieb@darkstar:~$ netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth1       1500 0     22259      0      0 0         13558      0      0      0 BMRU
lo        16436 0         0      0      0 0             0      0      0      0 LRU
tommieb@darkstar:~$

Then it's a matter of grepping out for the columns under 'RX-OK' (recieve ok) and 'TX-OK' (transmit ok)...

tommieb75
Interesting, works moderately for eth0, though it's not quite optimal.I could probably get the same results by polling another command myself.For me wlan0 always reports ok even when disconnected.I could listen to iwevent and use another command to check eth0, but I'd prefer actual events if possible for eth0.
@daniel: hmmm....I would not be 100% confident in saying if you want portability, that it would work under another *nix variant....but would imagine netstat output to be the same across all linux-kind....
tommieb75
A: 
#!/bin/bash
interface="eth0"
val=0
sec=10 #seconds to sleep
while true
do
  val=$(netstat -I="$interface" | awk 'END{print $8}')
  if [ "$val" != "$p" ];then
     echo "Interface: $interface ok"
  else
     echo "Interface: $interface no activity for $sec seconds"
  fi
  sleep $sec
  p=$val    
done
ghostdog74
+1  A: 

I managed to solve the issue myself.

ip monitor link | grep --line-buffered '\(eth0\|wlan0\).\+state \+\(DOWN\|UP\)' - | awk '{print $2$9}'

That command prints out link state changes to interfaces, greps out the lines specific to eth0 or wlan0 which are about state and are DOWN or UP (wlan0 also outputs DORMANT states which I don't need) and then uses awk so that it only outputs the info on interface and state.

The command prints out lines like "eth0:DOWN" and "wlan0:UP".

I can just pipe that into a shell script, perhaps use sed to replace the : with a space and split up the info.

A: 

On Debian and Ubuntu at least, activating a network interface runs the scripts in /etc/network/if-up.d/ (and there is a symmetric if-down.d directory for deactivation). So you could add your own script there. The scripts get information through several environment variables, this is documented in the interfaces man page.

There is a page in the Ubuntu wiki on the topic.

With a default Ubuntu configuration and a reasonably modern network card, the interface is automatically activated when a cable is plugged it. The commands to (de)activate the interface manually are ifup eth0 and ifdown eth0; or you can use Network Manager if you prefer.

For ppp connections (e.g., dial-up and DSL with some providers), the scripts under /etc/ppp/ip-up.d are run instead. They are documented in the pppd man page.

Gilles