[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: nslu2: two devices: one by default

En/na Xan ha escrit:
How to use ip link for seeing if eth0 is plugged ? ip link show eth0 gets me lot of output. cable unplugged==NO-CARRIER???

Thanks a lot,

Yes. This:

#!/usr/bin/env python
# This script see if eth0 cable is plugged or unplugged
# and prints the corresponding output in a file.
import subprocess
from time import strftime, localtime

# Global variables
now = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime()) # get the local time eth0_cmd = "ip link show eth0" # command for showing the state of eth0 device wifi_cmd = "ip link show wlan0" # command for showing the state of wlan0 device filepath = "/home/xan/script-per-cable-xarxa/log.txt" # the path of the log file

f = open(filepath, 'a') # open file for appending lines

# See if eth0 cable is plugged
p_eth0 = subprocess.Popen(eth0_cmd, shell=True,stdout=subprocess.PIPE) # run eth0_cmd
out_eth0 = p_eth0.stdout.read().strip() # catch the output
if out_eth0.find('NO-CARRIER') != -1:
f.write("eth0 cable unplugged: " + now + "\n") # if no-carrier is present, then cable is unplugged
f.write("eth0 cable plugged: " + now + "\n") # if no find, then cable is plugged

[The source is in: http://github.com/somenxavi/misc/blob/master/cable-check.py]

script serves as example. You could modify for modifying the route table if eth0 cable is plugged or not.
I add this script in crontab (crontab -e and then put
* * * * * /usr/bin/python /home/xan/script-per-cable-xarxa/cable.py) for running that minutely.

Thanks a lot all of you and, overall, Lennart for your help. When I have modified script that modify routing table I will post it.


Reply to: