30

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

45

You can do it many ways. Here an example:

$ cat /sys/class/net/eth0/operstate
up
  • 5
    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. – orodbhen Jul 29 '16 at 19:36
7
ip a show ethX up

If nothing displayed then your interface is down

  • This method: "ip a show ethX up" and check for output, is not sufficient – Joshua Clayton Feb 2 '18 at 22:43
5
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.

  • 1
    You should explain what you are doing. So that everyone can benefit from your answer. – Raphael Ahrens Mar 26 '14 at 9:37
  • 5
    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 '14 at 9:37
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
  • There is no "linkdown" to grep with ip route on Debian Stretch. – Ingo Apr 3 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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