moot has asked for the wisdom of the Perl Monks concerning the following question:

All hail fellow monks.

So it has come to this. In the course of a refactor I am updating some code that determines network interface addresses by parsing some flavour of ifconfig depending on platform (targetting Linux (debian) and OpenBSD specifically).

The refactored code is intended to be a little less brittle, so I've been investigating CPAN for i/f discovery modules, and here's a summary of what I've found. The test script is shown at the end.

Results:

Test Systems:
Linux: SuSE 9.2 stock, perl 5.8.3; Debian Sarge stock, perl 5.8.4 (Net::Interface not tested due to compilation problems)
OpenBSD: v3.2 stock, perl 5.6.1

Any problems are compounded by the fact that on Linux, alias addresses show as a new interface, where on OpenBSD they show as separate addresses for the native interface.

So my question is: Are there any reliable cross-platform methods of performing interface discovery without resorting to parsing qx{ifconfig}? All (!) I need to know is which interfaces are currently running, and which address(es) (including netmask) they are using.

Perhaps there is some other way to obtain this information, such as via netstat. Any takers? Bueller?

I have thought of just somehow dumping the info to a file and parsing that, but this smells of hackery to me.

Test script used to validate i/f information:

#!/usr/bin/perl -w use strict; eval { use IO::Interface; use IO::Socket; print head('IO::Interface'); my $s = IO::Socket::INET->new(Proto => 'udp'); for ($s->if_list) { print body('IO::I', $_, $s->if_addr($_), $s->if_netmask($_), $s->if_hwaddr($_)); } print foot('IO::Interface'); }; { use Net::Interface; print head('Net::Interface'); for (Net::Interface->interfaces) { no warnings; # Otherwise sprintf whines. print body('Net::I', $_->name, join('.', unpack "C4", $_->address), join('.', unpack "C4", $_->netmask), sprintf(("%02x:" x 5)."%02x", unpack "C6", $_->hwaddress)); } print foot('Net::Interface'); } { use Net::Ifconfig::Wrapper; print head('Net::Ifconfig::Wrapper'); my $ifs = Net::Ifconfig::Wrapper::Ifconfig('list'); for (keys %$ifs) { print body('N::I::W', $_, (keys %{$ifs->{$_}->{inet}})[0], (values %{$ifs->{$_}->{inet}})[0], $ifs->{$_}->{ether}, scalar keys %{$ifs->{$_}->{inet}}, ); } print foot('Net::Ifconfig::Wrapper'); } sub head { my $pkg = shift; sprintf "%18s %-30s %18s\n", '=' x 18, $pkg, '=' x 18; } sub foot { my $pkg = shift; sprintf "%s\n", "=" x 70; } sub body { my ($pkg, $if, $addr, $mask, $hw, $numint) = @_; $if ||= ''; $addr ||= ''; $mask ||= ''; $hw ||= ''; $numint ||= 1; sprintf "%-8s: %-6s (%15s/%15s) (%s) (%d)\n", $pkg, $if, $addr, $mask, $hw, $numint; }
Update: Added readmore tags per request.

Replies are listed 'Best First'.
Re: Network interface discovery
by sgifford (Prior) on Apr 23, 2005 at 21:25 UTC
      That sounded ideal, but sadly my Net::Pcap does not define findalldevs, although it is present in libpcap:
      $ perl -MNet::Pcap -e 'Net::Pcap::findalldevs()' Can't locate auto/Net/Pcap/findalldevs.al in @INC $ perlver Net::Pcap 0.04
        You could try a newer version of Net::Pcap; the documentation on CPAN says this function is supported in 0.05.
Re: Network interface discovery
by NetWallah (Canon) on Apr 24, 2005 at 01:23 UTC
    Try one of the SNMP modules.

    The Interface MIB is standard across all devices and OS's, so you should be able to get everything you need.

         "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein

      *smacks head* of course.. that should work nicely for most of what I need. Thanks for helping me see the wood when all around me were trees!
Re: Network interface discovery
by rg0now (Chaplain) on Apr 24, 2005 at 09:57 UTC
    After many years of doing maintenance work on various UNIX network daemons, I have gradually grown to believe that we do not have "reliable cross-platform methods of performing interface discovery", neither for Perl nor for C. In the case of Linux, the standard way was to parse /proc/net/dev (it is not that hard, even with C), even though recent libcs provide the if_nameindex API. For *BSD, coders usually rolled their own if_nameindex implementation, because the BSD libc does not seem to porvide the API (at least, back those days when the daemons were written, this was the case).

    Note that I do not want to foster these programming practices. I just want to stress that this paticular problem is really hard to solve in a cross-platform-way...

Re: Network interface discovery
by moot (Chaplain) on Apr 24, 2005 at 16:27 UTC
    For those watching at home, Net::SNMP::Interfaces gets me most of the way there. Don't know how I missed it originally. Thanks to all respondents.