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.
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
Then again, mabye not.
http://freebsd.active-venture.com/FreeBSD-srctree/newsrc/compat/linux/linux_ioctl.h.html
Regards,
Jordan
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
thanks for you suggestions!
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.
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!
err... I'm NOT proficient in c at all :-)
--
groetjes,