portable Python ifconfig

Bart Van Loon bbbart at inGen.be
Sat Mar 3 22:38:58 CET 2007

Hi all,

I'm looking for a portable (FreeBSD and Linux) way of getting typical
ifconfig information into Python.

Some research on the web brought me to Linux only solutions


which I didn't manage to port to FreeBSD (I'm not that experienced).
Finally though, I found out about pyifconfig:


which compiles fine on Linux after changing <python1.5/Python.h> to
<python2.4/Python.h>. On FreeBSD there is no SIOCGIFHWADDR, so I
just commented out that part. 

I can now do

In [1]: from pyifconfig import pyifconfig

In [2]: pyifconfig('ath0')
{'addr': '',
 'brdaddr': '',
 'hwaddr': '00:17:f2:4c:a5:0c',
 'netmask': ''}

on Linux, and

In [1]: from pyifconfig import pyifconfig

In [2]: pyifconfig('vr1')
{'addr': '',
 'brdaddr': '',
 'hwaddr': '\xff\xff',
 'netmask': ''}

on FreeBSD.

The problem now is that I get seemingly random information when I pass a
non-existing interface, a down interface or an empty string to
pyifconfig, which is very hard to figure out from inside a script:

In [3]: pyifconfig('foobar')
{'addr': '',
 'brdaddr': '',
 'hwaddr': '00:00:68:9a:a5:b7',
 'netmask': ''}

so, any pointers here on how I can go on from this point?

any help is appreciated


   Wormwood : Calvin, how about you?  
   Calvin : Hard to say ma'am. I think my cerebellum just fused.

More information about the Python-list mailing list