SIOCGIFADDR & ioctl()

Jürgen Hermann jh at web.de
Fri Aug 11 18:50:16 EDT 2000


"Micheal Kelly" <michealk at newlix.com> schrieb im Newsbeitrag
news:WZyk5.49690$07.1536275 at news1.rdc1.on.wave.home.com...
> There was some discussion awhile back about getting nic config info (ip

And there were answers...

import string,socket,fcntl
SIOCGIFADDR = 0x8915
ifname = 'eth0'
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ifr = ifname+'\0'*(16-len(ifname))+chr(socket.AF_INET)+15*'\0'
r= fcntl.ioctl(s.fileno(),SIOCGIFADDR,ifr)
addr = string.join(map(str,map(ord,r[20:24])),'.')
print addr


Unfortunately, really determining a local configured address just
isn't that portable a thing from my experience.  Probably the closest
would be the SIOCGIFCONF IOCtl (for Unix systems) as previously
posted.  There is an Windows socket IOCtl that is similar
(SIO_ADDRESS_LIST_QUERY), but I'm not sure how best to get to it from
Python, aside from something like a CallDLL approach.


http://www.linuxcare.it/developers/davidw/files/ifconfigmodule.c


Bye, Jürgen





More information about the Python-list mailing list