问题
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.
回答1:
Also take a look at files under /sys/class/net/
for eth0 subdirectory /sys/class/net/eth0/ files:
carrier, dormant, operstate
change depending on link conditions:
if up + cable: carrier=1, dormant=0, operstate=up
if up + NO cable: carrier=0, dormant=0, operstate=down
if down + cable: cat carrier=Invalid argument, cat dormant=Invalid argument, operstate=down
回答2:
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.
回答3:
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)...
回答4:
#!/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
回答5:
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.
来源:https://stackoverflow.com/questions/2394804/is-there-a-command-similar-to-iwevent-to-monitor-eth0-state