3

I would like to produce a list of all the interfaces available on a [Linux] system, and for each interface, produce a list of available IP addresses. I was hoping to do this in a more graceful fashion than parsing the output of the ip command while still avoiding the hassle of ioctl() and friends.

Unfortunately, while /sys/class/net/INTERFACE contains link-level addressing information (such as the interface MAC address), it does not contain any ip addressing information. Nor do any of the files in /proc contain this information.

Before I go off and make my Python code look a lot more like C, are there better tools for this? Something like ip that produces structured output would be nice.

8
  • 1
    What's wrong with parsing ifconfig output? For Python I'm surprised some are writing their own getifaddrs module
    – Steve-o
    Sep 23, 2011 at 18:40
  • First and foremost, ifconfig is actively broken. It doesn't understand multiple addresses associated with an interface, and it has the unfortunate habit of truncating long interface names. More generally, having structured access to so much information via /sys makes me wish for the same convenience here.
    – larsks
    Sep 23, 2011 at 18:43
  • Also, I have come across a few Python modules, and many of them look suspiciously unmaintained. A system level solution would be nicely language agnostic.
    – larsks
    Sep 23, 2011 at 18:53
  • You mean about ifconfig being broken? Try working in an environment using VLAN trunking and bonded interfaces, and watch things relying on ifconfig break because all of a sudden all of your interface names look the same. Sure, it doesn't affect home or workstation users, but it't not all that uncommon, either.
    – larsks
    Sep 23, 2011 at 19:01
  • 1
    @Steve-o: Slight exaggeration? ifconfig is DEPRECATED. Stop recommending and using it. It is also broken in some situations as it can't properly handle things as larsks mentions.
    – MikeyB
    Sep 23, 2011 at 19:21

1 Answer 1

1

Puppet Labs (the company that makes Puppet) also makes a tool called Facter:

Facter is a lightweight program that gathers basic node information about the hardware and operating system. Facter is especially useful for retrieving things like operating system names, hardware characteristics, IP addresses, MAC addresses, and SSH keys.

It does not require Puppet to be installed or used, just a Ruby interpreter.

I find Facter to be incredibly useful in my day-to-day sysadmin tasks for just this sort of reason. For instance, to show the IP address(es) on a box:

$ facter interfaces
eth0,eth0_0,eth1,eth2,eth3,sit0
$ facter | grep ipaddress
ipaddress => 10.2.5.100
ipaddress_eth0 => 10.2.5.100
ipaddress_eth0_0 => 10.2.6.1
$ facter ipaddress_eth0
10.2.5.100

There are lots of other pieces of information Facter can provide, and it's easy to write your own custom facts to extend it.

You must log in to answer this question.

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