Good explanation, but the conclusion is backwards. Line 10 and 28 are correct; it's line 5 that's wrong. There are multiple nics, so there should be multiple hashes. The real problem is that the function pretends there's only one. It should be returning a list of nics (list of hashes).
sub networkInfo {
my @nics;
my $platform = getPlatform();
if ($platform =~ /RedHat/) {
my $ifconfig = qx|/sbin/ifconfig|
or die("Can't get info from Linux ifconfig: $!\n");
for (split /(?<=\n)(?=\w)/, $ifconfig) {
my %nic;
($nic{device}) = /^(eth\d)\s/
or next;
if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) {
$nic{ip} = $1;
$nic{bcast} = $2;
$nic{mask} = $3;
}
if (/^\s+ inet6 addr:\s*(\S+)/m) {
$nic{ip6} = $1;
}
push @nics, \%nic;
}
} elsif ($platform =~ /SunOS/) {
my $ifconfig = qx|/sbin/ifconfig -a|
or die("Can't get info from Solaris ifconfig: $!\n");
for (split /(?<=\n)(?=\w)/, $ifconfig) {
my %nic;
($nic{device}) = /^(fjgi\d)\s/
or next;
if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) {
$nic{ip} = $1;
$nic{bcast} = $2;
$nic{mask} = $3;
}
if (/^\s+ inet6 addr:\s*(\S+)/m) {
$nic{ip6} = $1;
}
push @nics, \%nic;
}
}
return @nics;
}
for my $nic (networkInfo()) {
print(join( "\t",
map $_//'[undef]',
@{$nic}{qw( device ip ipv6 bcast mask )} ), "\n");
}
Update: Cleaned up opening paragraph.
Update: Second push discovered ability to turn invisible. Added anti-invisibility field to post so it's visible again. |