portable Python ifconfig

B

Bart Van Loon

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.
 
M

MonkeeSage

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

Here's a pure python version of the C extension, based on the recipes
you posted. In this version, the 'addr' key will not exist for a non-
existent / non-active interface.

import socket, fcntl, struct, platform

def _ifinfo(sock, addr, ifname):
iface = struct.pack('256s', ifname[:15])
info = fcntl.ioctl(sock.fileno(), addr, iface)
if addr == 0x8927:
hwaddr = []
for char in info[18:24]:
hwaddr.append(hex(ord(char))[2:])
return ':'.join(hwaddr)
else:
return socket.inet_ntoa(info[20:24])

def ifconfig(ifname):
ifreq = {'ifname': ifname}
infos = {}
osys = platform.system()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if osys == 'Linux':
# offsets defined in /usr/include/linux/sockios.h on linux 2.6
infos['addr'] = 0x8915 # SIOCGIFADDR
infos['brdaddr'] = 0x8919 # SIOCGIFBRDADDR
infos['hwaddr'] = 0x8927 # SIOCSIFHWADDR
infos['netmask'] = 0x891b # SIOCGIFNETMASK
elif 'BSD' in osys: # ???
infos['addr'] = 0x8915
infos['brdaddr'] = 0x8919
infos['hwaddr'] = 0x8927
infos['netmask'] = 0x891b
try:
for k,v in infos.items():
ifreq[k] = _ifinfo(sock, v, ifname)
except:
pass
sock.close()
return ifreq

ifc = ifconfig('ath0')
if ifc.has_key('addr'):
print ifc

I'm pretty sure the offsets would be different for BSD, but I don't
have any BSD boxes to test on (looks like from a bit of googling that
you might need to look at /compat/linux/linux_ioctl.h for the offsets
on BSDs). I'll leave it to you to fill in the BSD stuff.

Regards,
Jordan
 
M

MonkeeSage

Bart,

Can you try this and let us know if it works for FreeBSD?

import socket, fcntl, struct

def _ifinfo(sock, addr, ifname):
iface = struct.pack('256s', ifname[:15])
info = fcntl.ioctl(sock.fileno(), addr, iface)
if addr == 0x8927:
hwaddr = []
for char in info[18:24]:
hwaddr.append(hex(ord(char))[2:])
return ':'.join(hwaddr)
else:
return socket.inet_ntoa(info[20:24])

def ifconfig(ifname):
ifreq = {'ifname': ifname}
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
ifreq['addr'] = _ifinfo(sock, 0x8915, ifname) # SIOCGIFADDR
ifreq['brdaddr'] = _ifinfo(sock, 0x8919, ifname) #
SIOCGIFBRDADDR
ifreq['netmask'] = _ifinfo(sock, 0x891b, ifname) #
SIOCGIFNETMASK
ifreq['hwaddr'] = _ifinfo(sock, 0x8927, ifname) #
SIOCSIFHWADDR
except:
pass
sock.close()
return ifreq

Regards,
Jordan
 
B

Bart Van Loon

Bart,

Can you try this and let us know if it works for FreeBSD?

thanks for you suggestions!
import socket, fcntl, struct

def _ifinfo(sock, addr, ifname):
iface = struct.pack('256s', ifname[:15])
info = fcntl.ioctl(sock.fileno(), addr, iface)
if addr == 0x8927:
hwaddr = []
for char in info[18:24]:
hwaddr.append(hex(ord(char))[2:])
return ':'.join(hwaddr)
else:
return socket.inet_ntoa(info[20:24])

def ifconfig(ifname):
ifreq = {'ifname': ifname}
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
ifreq['addr'] = _ifinfo(sock, 0x8915, ifname) # SIOCGIFADDR
ifreq['brdaddr'] = _ifinfo(sock, 0x8919, ifname) #
SIOCGIFBRDADDR
ifreq['netmask'] = _ifinfo(sock, 0x891b, ifname) #
SIOCGIFNETMASK
ifreq['hwaddr'] = _ifinfo(sock, 0x8927, ifname) #
SIOCSIFHWADDR
except:
pass
sock.close()
return ifreq

apparenlty, it doesn't. :-(

I changes the except block into

except Exception, e:
print e

and added

if __name__ == '__main__':
print ifconfig('ng0')
print ifconfig('vr0')
print ifconfig('vr2')
print ifconfig('xl0')

ng0 exists and has an IP (virtual interface created by mpd)
vr0 exists but does not have an IP (phisical interface for mpd)
vr2 exists and has an IP configured
xl0 does not exist

output:

[Errno 25] Inappropriate ioctl for device
{'ifname': 'ng0'}
[Errno 25] Inappropriate ioctl for device
{'ifname': 'vr0'}
[Errno 25] Inappropriate ioctl for device
{'ifname': 'vr2'}
[Errno 25] Inappropriate ioctl for device
{'ifname': 'xl0'}

however, in Linux I get the following results:

[Errno 99] Cannot assign requested address
{'ifname': 'eth0'}
{'hwaddr': '0:17:f2:4c:a5:c', 'ifname': 'ath0', 'netmask': '255.255.255.0', 'addr': '192.168.50.104', 'brdaddr': '192.168.50.255'}
{'hwaddr': '0:0:0:0:0:0', 'ifname': 'tun0', 'netmask': '255.255.255.255', 'addr': '192.168.3.6', 'brdaddr': '0.0.0.0'}
[Errno 19] No such device
{'ifname': 'wielewoele'}

which seems 100% correct.

--
regards,
BBBart

Susie: You'd get a good grade without doing any work.
Calvin: So?
Susie: It's wrong to get rewards you haven't earned.
Calvin: I've never heard of anyone who couldn't live with that.
 
B

Bart Van Loon

It said:
Hi all,

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

After lots of trial and error (I'm proficient in C at all), I puzzled
togehter the following. It works (at least on FreeBSD and Linux), but is
still fairly rough, as it returns an empty string when given a non
existing or down interface.

I'll clean it up in due time. :)

#include "Python.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <ifaddrs.h>
#include <string.h>

// parameters: string (interface name)
// output: string (ip address of interface in decimal notation)
PyObject * ipaddr(PyObject *self, PyObject *args) {

char ip[ 200 ];
char *itf;

if (! PyArg_ParseTuple(args, "s", &itf)) {
PyErr_SetString(PyExc_Exception, "no interface given!");
return NULL;
}

struct ifaddrs *ifa = NULL, *ifp = NULL;

if (getifaddrs (&ifp) < 0)
{
perror ("getifaddrs");
return NULL;
}

for (ifa = ifp; ifa; ifa = ifa->ifa_next)
{
socklen_t salen;

if (ifa->ifa_addr->sa_family == AF_INET)
salen = sizeof (struct sockaddr_in);
else if (ifa->ifa_addr->sa_family == AF_INET6)
salen = sizeof (struct sockaddr_in6);
else
continue;

if (strncmp(ifa->ifa_name, itf, sizeof(itf))) {
continue;
}

if (getnameinfo (ifa->ifa_addr, salen,
ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) < 0)
{
perror ("getnameinfo");
continue;
}
break;

}

freeifaddrs (ifp);

return Py_BuildValue("s", ip);
}

static PyMethodDef ifconfig_methods[] = {
{"ipaddr", (PyCFunction)ipaddr, METH_VARARGS, "ipaddr(string)\n"},
{NULL, NULL, 0, NULL}
};

DL_EXPORT(void) initifconfig(void)
{
Py_InitModule3("ifconfig", ifconfig_methods, "Provides a function to get an ip address of a certain interface.\n");
}

Inspiration came from and credit goes to the author of
http://www.hungry.com/~alves/local-ip-in-C.html

--
regards,
BBBart

Hobbes : How is the diorama coming along?
Calvin : I'm almost finished.
Hobbes : I don't see the roadrunner. Weren't you going to put one in?
Calvin : See the cotton balls I glued down?
Hobbes : Yeah?
Calvin : The roadrunner just ran out of the scene leaving behind clouds
of dust!
 
B

Bart Van Loon

It said:
After lots of trial and error (I'm proficient in C at all), I puzzled

err... I'm NOT proficient in c at all :)

--
groetjes,
BBBart

Hobbes : How is the diorama coming along?
Calvin : I'm almost finished.
Hobbes : I don't see the roadrunner. Weren't you going to put one in?
Calvin : See the cotton balls I glued down?
Hobbes : Yeah?
Calvin : The roadrunner just ran out of the scene leaving behind clouds
of dust!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top