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

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439094
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439093

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

http://mail.python.org/pipermail/python-list/1999-August/009274.html

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')
Out[2]:
{'addr': '192.168.50.104',
 'brdaddr': '192.168.50.255',
 'hwaddr': '00:17:f2:4c:a5:0c',
 'netmask': '255.255.255.0'}

on Linux, and

In [1]: from pyifconfig import pyifconfig

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

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')
Out[3]:
{'addr': '104.154.165.183',
 'brdaddr': '104.154.165.183',
 'hwaddr': '00:00:68:9a:a5:b7',
 'netmask': '104.154.165.183'}

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

any help is appreciated

-- 
regards,
BBBart

   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