4

I would like to get the list of all active network interfaces (e.g. eth0, wlan0). I assume I'll have to parse the output of ifconfig, but the formatting is different across the various OS's. Does anybody have a portable way to get this information so it can be used on the majority of unices (linux, *bsd, mac).

4 Answers 4

1

Not sure about portabilty because a list of network interfaces may be related to the kernel implementation and linux is not related at all to *bsd/mac... however if ifconfig is not good for you you can try netstat -i

7

Check out the facter package. It is used mostly by the puppet config management program to grab metadata from installed machines, but it can be run in a standalone fashion for similar purposes. Its output does include information about network interfaces found on the machine, and its formatting will be consistent across platforms.

Example output:

user@host:~$ facter
architecture => i386
domain => members.linode.com
facterversion => 1.3.8
fqdn => host.members.linode.com
hardwareisa => unknown
hardwaremodel => i686
hostname => host 
id => user 
ipaddress => 1.2.3.4 
ipaddress_eth0 => 1.2.3.4 
kernel => Linux
kernelrelease => 2.6.32.16-linode28
lsbdistcodename => hardy
lsbdistdescription => Ubuntu 8.04.4 LTS
lsbdistid => Ubuntu
lsbdistrelease => 8.04
macaddress => 76:c9:3e:0b:xx:xx
macaddress_eth0 => fe:fd:43:12:xx:xx
memoryfree => 393.08 MB
memorysize => 498.68 MB
operatingsystem => Debian
operatingsystemrelease => 2.6.32.16-linode28
processor0 => Intel(R) Xeon(R) CPU           L5335  @ 2.00GHz
processor1 => Intel(R) Xeon(R) CPU           L5335  @ 2.00GHz
processor2 => Intel(R) Xeon(R) CPU           L5335  @ 2.00GHz
processor3 => Intel(R) Xeon(R) CPU           L5335  @ 2.00GHz
processorcount => 4
ps => ps -ef
rubysitedir => /usr/local/lib/site_ruby/1.8
rubyversion => 1.8.6
swapfree => 186.84 MB
swapsize => 255.99 MB
uniqueid => 007f0101
1
  • This is hudge ! I didn't knew this tool, this is absolutely perfect. Thanks !
    – brunetton
    Jan 23, 2019 at 18:05
2

The following will return a list of active interfaces on all the common Linux distros:

ifconfig | grep "Link encap" | awk '{ print $1 '}

If you want to also include interfaces which are currently down (inactive), simply add the -a flag as follows:

ifconfig -a | grep "Link encap" | awk '{ print $1 '}

As far as I know, there is no common method which can be used on any POSIX system to retrieve a list of network interfaces, so you'll have to localize your command for the OS currently in use.

0

Netstat -i and ifconfig will only show you the currently "active" network interfaces. An active interface can also have no carrier and thus be unusable. You need to look for the RUNNING flag (ifconfig), or R flag (netstat -i). This flag denotes that the interface has an active carrier, or most commonly is has something the network interface is physically connected to such as a switch.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .