2
  • Using terminal mode I want to display the current network interfaces that are NOT CONNECTED on my computer.

  • I have used ifconfig to find the connected interfaces along with their IP-address but can't figure out how to display just those that are NOT CONNECTED.

  • ifconfig -a shows all network interfaces, connected or not – ivanivan Apr 6 '18 at 19:08
  • 4
    With "not connected" you mean "cable not plugged in" or "deactivated"? – Hauke Laging Apr 6 '18 at 19:58
2

You can just do:

$ ip link show

for a list of interfaces including their status. You can filter results for devices that are not in use by piping grep DOWN after the ip command:

$ ip link show | grep DOWN
  • 2
    Not connected is not the same as down. – Hauke Laging Apr 6 '18 at 19:57
2
ip link show |
awk '/^[1-9]/ && $0 !~ "LOWER_UP" { inf=$2; sub(":","",inf); print inf; }' |
while read iface; do
    [[ $(readlink /sys/class/net/$iface) =~ devices/virtual ]] || echo $iface
done

The first part (ip link show | awk) gets all the interfaces for which the L2 driver reports that they are connected. The second part discards all virtual interfaces because "not connected" does not mean much for them.

1

Using ifconfig (because that's the command you said you know how to use) and bash with diff and sed:

diff <( ifconfig ) <( ifconfig -a ) | sed -nE 's/^> ([^[:blank:]]+).*/\1/p'

This will compare the output of ifconfig with that of ifconfig -a. From that output, any line starting with > (signifying that they are only in the ifconfig -a output) will be relating to interfaces that are not "UP". The sed expression parses out the interface names.

The result will be a list of interfaces that are not up.


The sed expression s/^> ([^[:blank:]]+).*/\1/p:

This is a substitution. It will match any line starting with > followed by a space. After that, it will capture any non-blank string of characters. This, with the rest of the line, is then replaced with the captured string of non-blank characters and the result is printed. The only lines that matches the regular expression in the output from diff are lines that mentions the interface name at the start of the line after > and space.

  • Thank you all! much appreciated! Kusalananda had the best answer for me. – nismoasfuh Apr 6 '18 at 21:05
  • Not connected is not the same as down. ifconfig is deprecated. – Pablo Bianchi Jan 14 at 4:36
1

You can check the state of the network interface from /sys/class/net/$interface/carrier file. (1 = connetcted , 0 = disconnected)

To get the disconnected network interface:

for i in $( ls /sys/class/net );do 
    if grep -q 0 /sys/class/net/$i/carrier; then
        echo $i; 
    fi
done
  • I'm not sure why I get errors like grep: /sys/class/net/lo/carrier: No such file or directory in all cases. Doesn't seem a quote/escape issue. Works outside the if. – Pablo Bianchi Jan 14 at 4:30
  • No need to invoke ls in a subshell, just do for i in /sys/class/net/*; do … – sleblanc Feb 26 at 6:51

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.