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.

larsks
  • 43,623
  • 14
  • 121
  • 180
  • 1
    What's wrong with parsing `ifconfig` output? For Python I'm surprised some are writing their own [getifaddrs](http://code.google.com/p/pydlnadms/source/browse/getifaddrs.py) module – Steve-o Sep 23 '11 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 '11 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 '11 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 '11 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 '11 at 19:21
  • so something simple like this: http://codepad.org/jLPCQAFV – Steve-o Sep 23 '11 at 19:21
  • I'm not sure this question actually belongs on serverfault. Here is an identical one on stackoverflow: http://stackoverflow.com/questions/6243276/how-to-get-the-physical-interface-ip-address-from-an-interface – polynomial Sep 24 '11 at 01:52
  • Since I started out looking at `/sys` and `/proc` I figured this was more of a Serverfault sort of thing. In any case, I found a ctypes-based interface to getifaddrs that seems to work for my purposes. Thanks for the Stackoverflow pointer. – larsks Sep 24 '11 at 01:54

1 Answers1

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.

Handyman5
  • 5,257
  • 26
  • 30