Re: portable Python ifconfig



It was Sun, 4 Mar 2007 02:38:58 +0500, when Bart Van Loon wrote:
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!
.



Relevant Pages

  • Re: FreeBSD 4.x Opteron Question
    ... the FreeBSD developers told everyone that 5.3 was da ... initially over linux not because there's a bunch of good guys on the ... My tests measure kernel performance; ... > a networking device is a key performance indicator. ...
    (freebsd-questions)
  • Re: Newbie Experience
    ... I've only been around since FreeBSD 5.4 ... FreeBSD kernel too. ... always sunshine and linux is farts. ... in the hey day of AT&T Unix I'm ...
    (freebsd-questions)
  • Re: Review of FreeBSD 5.4
    ... but not less problems compared to FreeBSD. ... If you like to have a bleeding edge system using debian --- just go ... > the linux kernel suffers. ... When the kernel suffers, everyone who uses ...
    (comp.unix.bsd.freebsd.misc)
  • Re: FreeBSD & Linux distro
    ... as a FreeBSD advocacy the tone of the article should be neutral and all ... do not like Linux and more over I have never used it in my life but I ... Statement of the type BSD appears more stable than Linux is ... fewer FreeBSD advocates make claims like that, however, is part of the ...
    (freebsd-questions)
  • Re: Compare Linux and Freebsd Redux
    ... >> Perhaps the most unsettling 'misinformation' about the FreeBSD vs. ... >> Linux situation was the spin that Linux was somehow more 'open', ... >> the official Linux tree... ... your own distribution is that the FreeBSD distribution is/was already ...
    (comp.unix.bsd.freebsd.misc)