44

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

2
  • 3
    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 Commented Mar 26, 2014 at 9:07
  • Pretty related: How to check the physical status of an ethernet port in Linux? Commented Mar 26, 2014 at 12:20

4 Answers 4

69

You can do it many ways. Here an example:

$ cat /sys/class/net/eth0/operstate up 
2
  • 6
    Unfortunately, this doesn't guarantee that the interface is fully configured. You can cat /sys/class/net/eth0/carrier, to check for a signal, but it still may not have a DHCP lease yet, etc. Commented Jul 29, 2016 at 19:36
  • In addition to this comment, several other answers here have gotten comments to the effect: This doesn't work. While it's useful knowing that this answer may not work in all instances, it would be much more helpful if the commentor: 1.) gave a reference, cited an example or explained how to correct the exception, or 2.) submitted an answer that covers the exception. Commented Dec 21, 2020 at 7:15
12
ip a show ethX up 

If nothing displayed then your interface is down

4
  • 1
    This method: "ip a show ethX up" and check for output, is not sufficient Commented Feb 2, 2018 at 22:43
  • @JoshuaClayton It would be nice if you could elaborate, because for me it works. Commented Feb 26, 2021 at 14:26
  • I can't remember for the world what didn't work about this for me back then. It does seem to always print something, UP or DOWN, but thats easily parsed by human or machine. Commented Feb 26, 2021 at 17:43
  • I see no different in output with or without up (e.g. ip a show wlan0 and ip a show wlan0 up produce identical output on my machine). The other answers all grep for state UP; is there something extra needed to make the up argument work? Commented Feb 20, 2022 at 19:03
7
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.

3
  • 1
    You should explain what you are doing. So that everyone can benefit from your answer. Commented Mar 26, 2014 at 9:37
  • 7
    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. Commented Mar 26, 2014 at 9:37
  • On an embedded linux 2.6 I get state UNKNOWN for all operable interfaces Commented Jun 6, 2020 at 21:06
0

The other methods do not work, but this one does (on Ubuntu at least):

while true; do ip route | grep "linkdown" if [ $? -eq 0 ]; then sleep 1 # network not yet up else break # network up fi done 
2
  • 4
    There is no "linkdown" to grep with ip route on Debian Stretch. Commented Apr 3, 2019 at 22:50
  • Debian stretch here: if you configured a post-down to ip route del default dev NICNAME then linkdown will definitely will be shown. Commented Jul 11, 2022 at 13:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.