HowTo: Show/Display available network interfaces
The biggest problem I found was not identifying all the network interfaces available but the ones that are up. For instance, the 2 quickest way to identify all the available network interfaces are:
ip link
$> ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 5c:26:0a:7b:7b:f6 brd ff:ff:ff:ff:ff:ff 3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000 link/ether a0:88:b4:ca:8b:4c brd ff:ff:ff:ff:ff:ff
Obviously, there are 2 interfaces (sans the default, lo): eth0 & wlan0. Btw, can you tell which of these 2 are up & which is NOT?!!!! Trust me I couldn’t. At least not in the first look! Then figured a pattern in the presence of ‘DOWN’ or ‘UP’ keywords!! Yet, this was not sufficient for me as I was wanting to identify the network interfaces that are UP using a generic script. ‘grep’-ing for ‘UP’ & ‘DOWN’ would not work out here.
ifconfig
$> ifconfig -a eth0 Link encap:Ethernet HWaddr 5c:26:0a:7b:7b:f6 inet addr:10.212.140.25 Bcast:10.212.140.255 Mask:255.255.255.0 inet6 addr: fe80::5e26:aff:fe7b:7bf6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:919885 errors:0 dropped:0 overruns:0 frame:0 TX packets:418690 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1098622617 (1.0 GB) TX bytes:48860614 (48.8 MB) Interrupt:20 Memory:e2e00000-e2e20000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:92466 errors:0 dropped:0 overruns:0 frame:0 TX packets:92466 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:12115565 (12.1 MB) TX bytes:12115565 (12.1 MB) wlan0 Link encap:Ethernet HWaddr a0:88:b4:ca:8b:4c BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:506150 errors:0 dropped:0 overruns:0 frame:0 TX packets:344206 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:557950489 (557.9 MB) TX bytes:40095705 (40.0 MB)
Another obvious problem! Can you see that it is not easy to figure out which of the interfaces are down!! Of course, the presense of UP (or the lack of it) is not sufficient to write a simple bash script!!
So, after a bit of research & some soul-searching as well, I figure out a simpler way:
netstat -i
$> netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 921323 0 0 0 419605 0 0 0 BMRU lo 65536 0 93141 0 0 0 93141 0 0 0 LRU
Ah! There you can see only eth0 is displayed! Voila! I found my unicorn here! 🙂 So, now my script would be a simple:
$> netstat -i | grep -vi 'kernel' | grep -vi 'iface' | grep -v 'lo' | awk '{print $1}' eth0
Isn’t this awesome?! 🙂
One can even use ‘ifconfig -s‘ instead of ‘netstat -i‘ and the output be the same! 🙂
FYI: I am using Ubuntu 14.04 LTS 64-bit.
Leave a Reply