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.

In reply to Network interface discovery by moot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.