Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to check with a script, whether eth0 is configured. If so, the script will do nothing. Otherwise it will start wlan0. (I don't want both eth0 and wlan0 to be up at the same time).

What would be the easiest way to check, whether eth0 is already up?

I am using Debian Wheezy

CLARIFICATION:

I would like to check not only that the cable in eth0 is plugged in, but rather that the interface is configured (i.e. it has either static IP set, or it has received a DHCP IP). If cable is plugged in, but eth0 is not configured correctly, I want to start wlan0

share|improve this question
2  
I'm not sure about what I'm going to say but cat /sys/class/net/ | grep eth0 should give you easilly parsable data. You could also do sudo ethtool eth0 | grep Link –  Kiwy Mar 26 at 9:07
    

2 Answers 2

up vote 4 down vote accepted

You can do it many ways. Here an example:

$ cat /sys/class/net/eth0/operstate
up
share|improve this answer
ip a | grep -Eq ': eth0:.*state UP' || _do_your_thing 

So here we grep the ubiquitous ip tool's stdout for a line which contains both our interface of interest and the phrase "state UP" ( thanks to @Lekensteyn for pointing out the need for a little more specificity than just UP). We use the argument a as the short form for address and that should be enough to get a listing of all configured network cards in system.

One advantage to using ip could be that it really should be available everywhere - it is how I commonly configure my Android phone's network devices, for instance.

The :colons are used to avoid partial matches - in this way we guarantee a match for eth0 as opposed to an otherwise possible someothereth0 or eth007.

Thanks @RaphaelAhrens for nudging me toward correctness and explaining my solution.

EDIT:

To handle the current requirements you could:

ip a | sed -rn '/: '"$if"':.*state UP/{N;N;s/.*inet (\S*).*/\1/p}'

The above will only print a CIDR ip address if your target $if is UP, plugged in, and has one. For ipv6 the solution is just as simple with only minor modification.

If you don't like sed you could achieve similar results with another |pipe ... grep and adding a -A context option to the first grep - but I like sed.

share|improve this answer
1  
You should explain what you are doing. So that everyone can benefit from your answer. –  Raphael Ahrens Mar 26 at 9:37
2  
This is insufficient. The output also contains UP if the device is up, but no cable is connected: 2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000. You can also specify the device to avoid false positives: ip addr show dev eth0. –  Lekensteyn Mar 26 at 9:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.