[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Portable way to get my NICs' IPs



On Thu, Oct 27, 2005 at 04:54:22PM -0400, Stephen R Laniel wrote:
> On Thu, Oct 27, 2005 at 01:45:36PM -0700, John Purser wrote:
> > You might consider one of the multi-platform languages like Python or
> > Perl.  Both have modules that will do this I believe.
> 
> Yeah, that's what I'm looking for. Does anyone know the Perl
> function for this task?

Sorry to disappoint you, but if you want to write something portable,
you've chosen the wrong task :)

At the system call level there's essentially two ways to get a list
of interfaces: (a) the SIOCGIFCONF request to the ioctl(2) call on a
socket, and (b) using the appropriate sysctl(2) request to examine the
kernel's interface list.  Both are definitely non-trivial to use [1].
So, your best bet probably _is_ to parse the output of ifconfig --
certainly much easier.

There's a perl module Net::Ifconfig::Wrapper, which more or less tries
to do exactly this for a number of platforms.  I'm not aware of any
direct and easy-to-use standard perl function for this (of course you
can use perl's interface to ioctl, but that would gain you nothing at
all).  Not sure about Python, Ruby, etc., though.

AFAIK, there's a C lib call getifaddrs(3) on BSD, however that doesn't
seem like a good idea if you're worried about portability...

Cheers,
Almut


[1] in case you're interested in the nitty-gritty details, see the
in-depth discussion in Stevens' networking bible [2], chapter 16/17.
Or google for SIOCGIFCONF.

[2] W. Richard Stevens: "Unix network programming", vol 1, 2nd ed.
    ISBN 0-13-490012-X



Reply to: