Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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).

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

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

share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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