| 1 | |||||||||||||||||||||||||||||
| 2 | package Net::Address::Ethernet; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Net::Address::Ethernet - find hardware ethernet address | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | use Net::Address::Ethernet qw( get_address ); | ||||||||||||||||||||||||||||
| 14 | my $sAddress = get_address; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | The following functions will be exported to your namespace if you request :all like so: | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Net::Address::Ethernet qw( :all ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =over | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use Data::Dumper; # for debugging only | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use Net::Domain; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use Net::Ifconfig::Wrapper qw( Ifconfig ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use Regexp::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use constant DEBUG_MATCH => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | use vars qw( $DEBUG $VERSION @EXPORT_OK %EXPORT_TAGS ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 5 | $VERSION = 1.128; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | $DEBUG = 0 || $ENV{N_A_E_DEBUG}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | %EXPORT_TAGS = ( 'all' => [ qw( get_address get_addresses canonical is_address ), ], ); | |||||||||||||||||||||||||||
| 44 | 5 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | my @ahInfo; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item get_address | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Returns the 6-byte ethernet address in canonical form. | ||||||||||||||||||||||||||||
| 51 | For example, '1A:2B:3C:4D:5E:6F'. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | When called in array context, returns a 6-element list representing | ||||||||||||||||||||||||||||
| 54 | the 6 bytes of the address in decimal. For example, | ||||||||||||||||||||||||||||
| 55 | (26,43,60,77,94,111). | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | If any non-zero argument is given, | ||||||||||||||||||||||||||||
| 58 | debugging information will be printed to STDERR. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub get_address | ||||||||||||||||||||||||||||
| 63 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_address: called CC=11 SCAR=39.8
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | # warn " TTT get_address()"; | ||||||||||||||||||||||||||||
| 65 | 4 | if (0) | |||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | # If you know the name of the adapter, you can use this code to | ||||||||||||||||||||||||||||
| 68 | # get its IP address: | ||||||||||||||||||||||||||||
| 69 | 5 | ▶ | use Socket qw/PF_INET SOCK_DGRAM inet_ntoa sockaddr_in/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | if (! socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip'))) | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | warn " WWW socket() failed\n"; | ||||||||||||||||||||||||||||
| 73 | goto IFCONFIG_VERSION; | ||||||||||||||||||||||||||||
| 74 | } # if | ||||||||||||||||||||||||||||
| 75 | # use ioctl() interface with SIOCGIFADDR. | ||||||||||||||||||||||||||||
| 76 | my $ifreq = pack('a32', 'enp1s0'); | ||||||||||||||||||||||||||||
| 77 | if (! ioctl(SOCKET, 0x8915, $ifreq)) | ||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | warn " WWW ioctl failed\n"; | ||||||||||||||||||||||||||||
| 80 | goto IFCONFIG_VERSION; | ||||||||||||||||||||||||||||
| 81 | } # if | ||||||||||||||||||||||||||||
| 82 | # Format the IP address from the output of ioctl(). | ||||||||||||||||||||||||||||
| 83 | my $s = inet_ntoa((sockaddr_in((unpack('a16 a16', $ifreq))[1]))[1]); | ||||||||||||||||||||||||||||
| 84 | if (! $s) | ||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | warn " WWW inet_ntoa failed\n"; | ||||||||||||||||||||||||||||
| 87 | goto IFCONFIG_VERSION; | ||||||||||||||||||||||||||||
| 88 | } # if | ||||||||||||||||||||||||||||
| 89 | warn Dumper($s); exit 88; # for debugging | ||||||||||||||||||||||||||||
| 90 | } # if 0 | ||||||||||||||||||||||||||||
| 91 | IFCONFIG_VERSION: | ||||||||||||||||||||||||||||
| 92 | 4 | my @a = get_addresses(@_); | |||||||||||||||||||||||||||
| 93 | 4 | _debug(" DDD in get_address, a is ", Dumper(\@a)); | |||||||||||||||||||||||||||
| 94 | # Even if none are active, we'll return the first one: | ||||||||||||||||||||||||||||
| 95 | 4 | my $sAddr = $a[0]->{sEthernet}; | |||||||||||||||||||||||||||
| 96 | # Look through the list, returning the first active one that has a | ||||||||||||||||||||||||||||
| 97 | # non-loopback IP address assigned to it: | ||||||||||||||||||||||||||||
| 98 | TRY_ADDR: | ||||||||||||||||||||||||||||
| 99 | 4 | foreach my $rh (@a) | |||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 4 | my $sName = $rh->{sAdapter}; | |||||||||||||||||||||||||||
| 102 | 4 | _debug(" DDD inspecting interface $sName...\n"); | |||||||||||||||||||||||||||
| 103 | 4 | ▶ | if (! $rh->{iActive}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 4 | _debug(" DDD but it is not active.\n"); | |||||||||||||||||||||||||||
| 106 | 4 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 107 | } # if | ||||||||||||||||||||||||||||
| 108 | 0 | _debug(" DDD it is active...\n"); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (! exists $rh->{sIP}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | _debug(" DDD but it has no IP address.\n"); | |||||||||||||||||||||||||||
| 112 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 113 | } # if | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (! defined $rh->{sIP}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 0 | _debug(" DDD but its IP address is undefined.\n"); | |||||||||||||||||||||||||||
| 117 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 118 | } # if | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($rh->{sIP} eq '') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | _debug(" DDD but its IP address is empty.\n"); | |||||||||||||||||||||||||||
| 122 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 123 | } # if | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ($rh->{sIP} eq '127.0.0.1') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 0 | _debug(" DDD but it's the loopback.\n"); | |||||||||||||||||||||||||||
| 127 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 128 | } # if | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (! exists $rh->{sEthernet}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | { | ||||||||||||||||||||||||||||
| 131 | 0 | _debug(" DDD but it has no ethernet address.\n"); | |||||||||||||||||||||||||||
| 132 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 133 | } # if | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (! defined $rh->{sEthernet}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||
| 136 | 0 | _debug(" DDD but its ethernet address is undefined.\n"); | |||||||||||||||||||||||||||
| 137 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 138 | } # if | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ($rh->{sEthernet} eq q{}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 0 | _debug(" DDD but its ethernet address is empty.\n"); | |||||||||||||||||||||||||||
| 142 | 0 | next TRY_ADDR; | |||||||||||||||||||||||||||
| 143 | } # if | ||||||||||||||||||||||||||||
| 144 | 0 | $sAddr = $rh->{sEthernet}; | |||||||||||||||||||||||||||
| 145 | 0 | _debug(" DDD and its ethernet address is $sAddr.\n"); | |||||||||||||||||||||||||||
| 146 | 0 | last TRY_ADDR; | |||||||||||||||||||||||||||
| 147 | } # foreach TRY_ADDR | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return wantarray ? map { hex } split(/[-:]/, $sAddr) : $sAddr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } # get_address | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item get_addresses | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Returns an array of hashrefs. | ||||||||||||||||||||||||||||
| 155 | Each hashref describes one Ethernet adapter found in the current hardware configuration, | ||||||||||||||||||||||||||||
| 156 | with the following entries filled in to the best of our ability to determine: | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =over | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item sEthernet -- The MAC address in canonical form. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item rasIP -- A reference to an array of all the IP addresses on this adapter. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item sIP -- The "first" IP address on this adapter. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item sAdapter -- The name of this adapter. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item iActive -- Whether this adapter is active. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =back | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | For example: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 'sAdapter' => 'Ethernet adapter Local Area Connection', | ||||||||||||||||||||||||||||
| 176 | 'sEthernet' => '12:34:56:78:9A:BC', | ||||||||||||||||||||||||||||
| 177 | 'rasIP' => ['111.222.33.44',], | ||||||||||||||||||||||||||||
| 178 | 'sIP' => '111.222.33.44', | ||||||||||||||||||||||||||||
| 179 | 'iActive' => 1, | ||||||||||||||||||||||||||||
| 180 | }, | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | If any non-zero argument is given, | ||||||||||||||||||||||||||||
| 183 | debugging information will be printed to STDERR. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub get_addresses | ||||||||||||||||||||||||||||
| 188 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_addresses: called CC=20 SCAR=54.2
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | # warn " TTT get_addresses()"; | ||||||||||||||||||||||||||||
| 190 | 6 | ▶ | $DEBUG ||= shift; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Net::Address::Ethernet::DEBUG ||= shift()
MC/DC0%0/2
$Net::Address::Ethernet::DEBUG ||= shift()
$Net::Address::Ethernet::DEBUG
shift()
Truth table33%1/3
A ||= B
A= $Net::Address::Ethernet::DEBUGB= shift()
| |||||||||||||||||||||||||||||
| 191 | # Short-circuit if this function has already been called: | ||||||||||||||||||||||||||||
| 192 | 6 | ▶ | if (! $DEBUG && @ahInfo) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $Net::Address::Ethernet::DEBUG and @ahInfo
MC/DC0%0/2
not $Net::Address::Ethernet::DEBUG and @ahInfo
not $Net::Address::Ethernet::DEBUG
@ahInfo
Truth table33%1/3
not(A) and B
A= not $Net::Address::Ethernet::DEBUGB= @ahInfo
| |||||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||
| 194 | 0 | goto ALL_DONE; | |||||||||||||||||||||||||||
| 195 | } # if | ||||||||||||||||||||||||||||
| 196 | 6 | my $sAddr = undef; | |||||||||||||||||||||||||||
| 197 | 6 | my $rh = Ifconfig('list', '', '', ''); | |||||||||||||||||||||||||||
| 198 | 6 | ▶ | if ((! defined $rh) || (! scalar keys %$rh)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $rh and scalar keys %$rh
MC/DC0%0/2
defined $rh and scalar keys %$rh
defined $rh
scalar keys %$rh
Truth table33%1/3
A and B
A= defined $rhB= scalar keys %$rh
| |||||||||||||||||||||||||||||
| 199 | { | ||||||||||||||||||||||||||||
| 200 | # warn " WWW Ifconfig failed: $@"; | ||||||||||||||||||||||||||||
| 201 | 6 | ▶ | if ($@ =~ m/not found/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | { | ||||||||||||||||||||||||||||
| 203 | # At this point we might try another method, such as calling /sbin/ip | ||||||||||||||||||||||||||||
| 204 | 6 | my $sCmdIp = '/sbin/ip'; | |||||||||||||||||||||||||||
| 205 | 6 | ▶ | if (! -f $sCmdIp) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||
| 207 | 6 | warn " DDD $sCmdIp does not exist"; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | else | ||||||||||||||||||||||||||||
| 210 | { | ||||||||||||||||||||||||||||
| 211 | 0 | $sCmdIp .= q/ addr show/; | |||||||||||||||||||||||||||
| 212 | 0 | my @asOutput = qx/$sCmdIp/; | |||||||||||||||||||||||||||
| 213 | # print STDERR " DDD asOutput ==@asOutput"; | ||||||||||||||||||||||||||||
| 214 | 0 | my $sInterface = q//; | |||||||||||||||||||||||||||
| 215 | 0 | my %hash; | |||||||||||||||||||||||||||
| 216 | 0 | foreach my $sLine (@asOutput) | |||||||||||||||||||||||||||
| 217 | { | ||||||||||||||||||||||||||||
| 218 | # print STDERR " DDD sLine ==$sLine"; | ||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if ($sLine =~ m/\d:\s(.+?):.+,UP/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | { | ||||||||||||||||||||||||||||
| 221 | # Found an interface that is in UP state | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | push @ahInfo, {%hash} if %hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | $sInterface = $1; | |||||||||||||||||||||||||||
| 224 | # Start a new adapter's info: | ||||||||||||||||||||||||||||
| 225 | 0 | %hash = (); | |||||||||||||||||||||||||||
| 226 | 0 | $hash{sAdapter} = $sInterface; | |||||||||||||||||||||||||||
| 227 | 0 | $hash{iActive} = 1; | |||||||||||||||||||||||||||
| 228 | 0 | _debug(" DDD hash is ", Dumper(\%hash)); | |||||||||||||||||||||||||||
| 229 | } # if | ||||||||||||||||||||||||||||
| 230 | 0 | ▶ | if ($sLine =~ m/ether\s+(([0-9a-f]{2}:){5}[0-9a-f]{2})/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||
| 232 | 0 | $hash{sEthernet} = canonical($1); | |||||||||||||||||||||||||||
| 233 | 0 | _debug(" DDD hash is ", Dumper(\%hash)); | |||||||||||||||||||||||||||
| 234 | } # if | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | if ($sLine =~ m/inet\s+((\d+\.){3}\d+)/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||
| 237 | 0 | $hash{sAdapter} = $sInterface; | |||||||||||||||||||||||||||
| 238 | 0 | $hash{sIP} = $1; | |||||||||||||||||||||||||||
| 239 | 0 | $hash{rasIP} = [$1]; | |||||||||||||||||||||||||||
| 240 | 0 | _debug(" DDD hash is ", Dumper(\%hash)); | |||||||||||||||||||||||||||
| 241 | } # if | ||||||||||||||||||||||||||||
| 242 | } # foreach | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | push @ahInfo, {%hash} if %hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | } # if | ||||||||||||||||||||||||||||
| 245 | } # if | ||||||||||||||||||||||||||||
| 246 | # No sense trying to parse non-existent output: | ||||||||||||||||||||||||||||
| 247 | 6 | goto ALL_DONE; | |||||||||||||||||||||||||||
| 248 | } # if | ||||||||||||||||||||||||||||
| 249 | 0 | _debug(" DDD raw output from Ifconfig is ", Dumper($rh)); | |||||||||||||||||||||||||||
| 250 | # Convert their hashref to our array format: | ||||||||||||||||||||||||||||
| 251 | 0 | foreach my $key (keys %$rh) | |||||||||||||||||||||||||||
| 252 | { | ||||||||||||||||||||||||||||
| 253 | 0 | my %hash; | |||||||||||||||||||||||||||
| 254 | 0 | _debug(" DDD working on key $key...\n"); | |||||||||||||||||||||||||||
| 255 | 0 | my $sAdapter = $key; | |||||||||||||||||||||||||||
| 256 | 0 | ▶ | if ($key =~ m!\A\{.+}\z!) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | { | ||||||||||||||||||||||||||||
| 258 | 0 | $sAdapter = $rh->{$key}->{descr}; | |||||||||||||||||||||||||||
| 259 | } # if | ||||||||||||||||||||||||||||
| 260 | 0 | $hash{sAdapter} = $sAdapter; | |||||||||||||||||||||||||||
| 261 | 0 | my @asIP = keys %{$rh->{$key}->{inet}}; | |||||||||||||||||||||||||||
| 262 | # Thanks to Sergey Kotenko for the array idea: | ||||||||||||||||||||||||||||
| 263 | 0 | $hash{rasIP} = \@asIP; | |||||||||||||||||||||||||||
| 264 | 0 | $hash{sIP} = $asIP[0]; | |||||||||||||||||||||||||||
| 265 | 0 | ▶ | my $sEther = $rh->{$key}->{ether} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$rh->{$key}{'ether'} || ''
MC/DC0%0/1
$rh->{$key}{'ether'} || ''
$rh->{$key}{'ether'}
Truth table0%0/2
A
A= $rh->{$key}{'ether'}
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ($sEther eq '') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | { | ||||||||||||||||||||||||||||
| 268 | 0 | $sEther = _find_mac($sAdapter, $hash{sIP}); | |||||||||||||||||||||||||||
| 269 | } # if | ||||||||||||||||||||||||||||
| 270 | 0 | $hash{sEthernet} = canonical($sEther); | |||||||||||||||||||||||||||
| 271 | 0 | $hash{iActive} = 0; | |||||||||||||||||||||||||||
| 272 | 0 | ▶ | if (defined $rh->{$key}->{status} && ($rh->{$key}->{status} =~ m!\A(1|UP)\z!i)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $rh->{$key}{"status"} and $rh->{$key}{"status"} =~ /\A(1|UP)\z/i
MC/DC0%0/2
defined $rh->{$key}{"status"} and $rh->{$key}{"status"} =~ /\A(1|UP)\z/i
defined $rh->{$key}{"status"}
$rh->{$key}{"status"} =~ /\A(1|UP)\z/i
Truth table0%0/3
A and B
A= defined $rh->{$key}{"status"}B= $rh->{$key}{"status"} =~ /\A(1|UP)\z/i
| |||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||
| 274 | 0 | $hash{iActive} = 1; | |||||||||||||||||||||||||||
| 275 | } # if | ||||||||||||||||||||||||||||
| 276 | 0 | push @ahInfo, \%hash; | |||||||||||||||||||||||||||
| 277 | } # foreach | ||||||||||||||||||||||||||||
| 278 | ALL_DONE: | ||||||||||||||||||||||||||||
| 279 | 6 | return @ahInfo; | |||||||||||||||||||||||||||
| 280 | } # get_addresses | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # Attempt other ways of finding the MAC Address: | ||||||||||||||||||||||||||||
| 284 | sub _find_mac | ||||||||||||||||||||||||||||
| 285 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_find_mac: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | my $sAdapter = shift || return; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 287 | 0 | ▶ | my $sIP = shift || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 288 | # No hope on some OSes: | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | return if ($^O eq 'MSWIn32'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 0 | my @asARP = qw( /usr/sbin/arp /sbin/arp /bin/arp /usr/bin/arp ); | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | my $sHostname = hostname || Net::Domain::hostname || ''; | ||||||||||||||||||||||||||
|
Condition0%0/5
hostname() || Net::Domain::hostname() || ''
hostname() or Net::Domain::hostname()
MC/DC0%0/2
hostname() || Net::Domain::hostname() || ''
hostname()
Net::Domain::hostname()
Truth table0%0/3
A or B
A= hostname()B= Net::Domain::hostname()
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | my $sHostfqdn = Net::Domain::hostfqdn || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
Net::Domain::hostfqdn() || ''
MC/DC0%0/1
Net::Domain::hostfqdn() || ''
Net::Domain::hostfqdn()
Truth table0%0/2
A
A= Net::Domain::hostfqdn()
| |||||||||||||||||||||||||||||
| 293 | 0 | my @asHost = ($sHostname, $sHostfqdn, ''); | |||||||||||||||||||||||||||
| 294 | ARP: | ||||||||||||||||||||||||||||
| 295 | 0 | foreach my $sARP (@asARP) | |||||||||||||||||||||||||||
| 296 | { | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | next ARP if ! -x $sARP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | HOSTNAME: | ||||||||||||||||||||||||||||
| 299 | 0 | foreach my $sHost (@asHost) | |||||||||||||||||||||||||||
| 300 | { | ||||||||||||||||||||||||||||
| 301 | 0 | ▶ | $sHost ||= q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$sHost ||= ""
MC/DC0%0/1
$sHost ||= ""
$sHost
Truth table0%0/2
A
A= $sHost
| |||||||||||||||||||||||||||||
| 302 | 0 | ▶ | next HOSTNAME if ($sHost eq q{}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 0 | my $sCmd = qq{$sARP $sHost}; | |||||||||||||||||||||||||||
| 304 | # print STDERR " DDD trying ==$sCmd==\n"; | ||||||||||||||||||||||||||||
| 305 | 0 | my @as = qx{$sCmd}; | |||||||||||||||||||||||||||
| 306 | LINE_OF_CMD: | ||||||||||||||||||||||||||||
| 307 | 0 | ▶ | while (@as) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | { | ||||||||||||||||||||||||||||
| 309 | 0 | my $sLine = shift @as; | |||||||||||||||||||||||||||
| 310 | 0 | DEBUG_MATCH && print STDERR " DDD output line of cmd ==$sLine==\n"; | |||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ($sLine =~ m!\(($RE{net}{IPv4})\)\s+AT\s+($RE{net}{MAC})\b!i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | { | ||||||||||||||||||||||||||||
| 313 | # Looks like arp on Solaris. | ||||||||||||||||||||||||||||
| 314 | 0 | my ($sIPFound, $sEtherFound) = ($1, $2); | |||||||||||||||||||||||||||
| 315 | # print STDERR " DDD found IP =$sIPFound=, found ether =$sEtherFound=\n"; | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | return $sEtherFound if ($sIPFound eq $sIP); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | # print STDERR " DDD does NOT match the one I wanted =$sIP=\n"; | ||||||||||||||||||||||||||||
| 318 | } # if | ||||||||||||||||||||||||||||
| 319 | 0 | ▶ | if ($sLine =~ m!($RE{net}{IPv4})\s+ETHER\s+($RE{net}{MAC})\b!i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | { | ||||||||||||||||||||||||||||
| 321 | # Looks like arp on Solaris. | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | return $2 if ($1 eq $sIP); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | } # if | ||||||||||||||||||||||||||||
| 324 | } # while LINE_OF_CMD | ||||||||||||||||||||||||||||
| 325 | } # foreach HOSTNAME | ||||||||||||||||||||||||||||
| 326 | } # foreach ARP | ||||||||||||||||||||||||||||
| 327 | } # _find_mac | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =item is_address | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | Returns a true value if its argument looks like an ethernet address. | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | sub is_address | ||||||||||||||||||||||||||||
| 336 | 20 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_address: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 20 | ▶ | my $s = uc(shift || ''); | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 338 | # Convert all non-hex digits to colon: | ||||||||||||||||||||||||||||
| 339 | 20 | $s =~ s![^0-9A-F]+!:!g; | |||||||||||||||||||||||||||
| 340 | 20 | return ($s =~ m!\A$RE{net}{MAC}\Z!i); | |||||||||||||||||||||||||||
| 341 | } # is_address | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =item canonical | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Given a 6-byte ethernet address, converts it to canonical form. | ||||||||||||||||||||||||||||
| 347 | Canonical form is 2-digit uppercase hexadecimal numbers with colon | ||||||||||||||||||||||||||||
| 348 | between the bytes. The address to be converted can have any kind of | ||||||||||||||||||||||||||||
| 349 | punctuation between the bytes, the bytes can be 1-digit, and the bytes | ||||||||||||||||||||||||||||
| 350 | can be lowercase; but the bytes must already be hex. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | sub canonical | ||||||||||||||||||||||||||||
| 355 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
canonical: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 356 | 10 | my $s = shift; | |||||||||||||||||||||||||||
| 357 | 10 | ▶ | return '' if ! is_address($s); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | # Convert all non-hex digits to colon: | ||||||||||||||||||||||||||||
| 359 | 6 | $s =~ s![^0-9a-fA-F]+!:!g; | |||||||||||||||||||||||||||
| 360 | 6 | my @as = split(':', $s); | |||||||||||||||||||||||||||
| 361 | # Cobble together 2-digit hex bytes: | ||||||||||||||||||||||||||||
| 362 | 6 | $s = ''; | |||||||||||||||||||||||||||
| 363 | 6 | ▶ | map { $s .= length() < 2 ? "0$_" : $_; $s .= ':' } @as; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | 6 | chop $s; | |||||||||||||||||||||||||||
| 365 | 6 | return uc $s; | |||||||||||||||||||||||||||
| 366 | } # canonical | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | sub _debug | ||||||||||||||||||||||||||||
| 369 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 370 | 12 | ▶ | return if ! $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 0 | print STDERR @_; | |||||||||||||||||||||||||||
| 372 | } # _debug | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =back | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head1 NOTES | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | arp, ifconfig, ipconfig | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head1 BUGS | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Please tell the author if you find any! And please show me the output | ||||||||||||||||||||||||||||
| 385 | of `arp <hostname>` | ||||||||||||||||||||||||||||
| 386 | or `ifconfig` | ||||||||||||||||||||||||||||
| 387 | or `ifconfig -a` | ||||||||||||||||||||||||||||
| 388 | from your system. | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Martin 'Kingpin' Thurn, C<mthurn at cpan.org>, L<http://tinyurl.com/nn67z>. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | This software is released under the same license as Perl itself. | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =cut | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 5 | 1; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | __END__ | ||||||||||||||||||||||||||||