moot has asked for the wisdom of the Perl Monks concerning the following question:
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
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Network interface discovery
by sgifford (Prior) on Apr 23, 2005 at 21:25 UTC | |
by moot (Chaplain) on Apr 23, 2005 at 22:08 UTC | |
by sgifford (Prior) on Apr 23, 2005 at 22:44 UTC | |
by moot (Chaplain) on Apr 24, 2005 at 01:12 UTC | |
by sgifford (Prior) on Apr 24, 2005 at 01:58 UTC | |
|
Re: Network interface discovery
by NetWallah (Canon) on Apr 24, 2005 at 01:23 UTC | |
by moot (Chaplain) on Apr 24, 2005 at 14:43 UTC | |
|
Re: Network interface discovery
by rg0now (Chaplain) on Apr 24, 2005 at 09:57 UTC | |
|
Re: Network interface discovery
by moot (Chaplain) on Apr 24, 2005 at 16:27 UTC |