Re: Portable way to get my NICs' IPs
- To: firstname.lastname@example.org
- Subject: Re: Portable way to get my NICs' IPs
- From: Almut Behrens <email@example.com>
- Date: Fri, 28 Oct 2005 01:52:59 +0200
- Message-id: <20051027235258.GK31124@citadelle.homelinux.net>
- In-reply-to: <20051027205422.GH19334@TheloniousMonk.laniels.org>
- References: <20051027202933.GA19334@TheloniousMonk.laniels.org> <firstname.lastname@example.org> <20051027204254.GE19334@TheloniousMonk.laniels.org> <email@example.com> <20051027205422.GH19334@TheloniousMonk.laniels.org>
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 .
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...
 in case you're interested in the nitty-gritty details, see the
in-depth discussion in Stevens' networking bible , chapter 16/17.
Or google for SIOCGIFCONF.
 W. Richard Stevens: "Unix network programming", vol 1, 2nd ed.