« blib/lib/App/Netdisco/Worker/Actions.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/IOSXR.pm »
| 1 | package App::Netdisco::SSHCollector::Platform::IOSXE; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Netdisco::SSHCollector::Platform::IOSXE | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | CLI collectors for Cisco IOS-XE. | ||
| 10 | |||
| 11 | ARP: | ||
| 12 | IOS-XE does not always support "show ip arp vrf all". This module collects | ||
| 13 | the global ARP table and, when VRFs are present, enumerates VRFs from | ||
| 14 | "show vrf" and collects "show ip arp vrf <vrf>" for each. With C<store_vrfs> | ||
| 15 | on, C<arpnip_by_vrf> also reads IPv6 neighbors and keeps each entry's VRF. | ||
| 16 | |||
| 17 | MAC: | ||
| 18 | Collects forwarding entries from "show mac address-table" via an interactive | ||
| 19 | PTY (Expect) to avoid BRIDGE/Q-BRIDGE MIB gaps seen on some IOS-XE builds | ||
| 20 | and controller-managed deployments. | ||
| 21 | |||
| 22 | Both collectors normalise MAC formatting for Netdisco. | ||
| 23 | |||
| 24 | =head1 PUBLIC METHODS | ||
| 25 | |||
| 26 | =over 4 | ||
| 27 | |||
| 28 | =item B<arpnip_by_vrf($host, $ssh, $args)> | ||
| 29 | |||
| 30 | As C<arpnip>, used when C<store_vrfs> is on. Reads C<show vrf>, then the global | ||
| 31 | C<show ip arp> and C<show ipv6 neighbors> and, for each listed VRF, | ||
| 32 | C<show ip arp vrf NAME> and, when the VRF lists C<ipv6> and the global | ||
| 33 | C<show ipv6 neighbors> answered, C<show ipv6 neighbors vrf NAME>. Each entry's | ||
| 34 | VRF is the one it was read from (the global table is C<''>). A VRF that does | ||
| 35 | not list C<ipv6> has no IPv6 table, so its IPv6 counts as read whole and empty. | ||
| 36 | |||
| 37 | Returns C<{ entries =E<gt> [ { ip, mac, vrf } ], vrf_read =E<gt> { VRF =E<gt> 0|1 }, | ||
| 38 | vrf_list_complete =E<gt> 0|1, families =E<gt> [4, 6] }>, or undef when the | ||
| 39 | session fails or C<show vrf> times out. C<families> is C<[4]> when the global | ||
| 40 | C<show ipv6 neighbors> did not answer cleanly (IPv6 is unsupported or | ||
| 41 | unreached), and then C<vrf_read> covers IPv4 only; otherwise it is C<[4, 6]>. | ||
| 42 | A table is read whole when it returned to the prompt with no C<%> error and | ||
| 43 | every line was a header or an entry. A VRF is 1 only when its table for every | ||
| 44 | family in C<families> was read whole, so a VRF whose IPv6 table fails is 0 | ||
| 45 | though its IPv4 entries are reported. A C<show vrf> is a whole list only when | ||
| 46 | it holds the header line and every other line is a VRF row or an interface-only | ||
| 47 | continuation (one name in the header's C<Interfaces> column); a reply without | ||
| 48 | the header (an error, a denial, nothing) makes C<vrf_list_complete> 0 and the | ||
| 49 | global tables are still read. A VRF named C<all> or with an unusual name is 0 | ||
| 50 | and is never queried. Only a VRF named exactly C<default> is the global table; | ||
| 51 | C<Default> is a user VRF. After a timeout the rest of the session is not read, | ||
| 52 | and that VRF and every one not yet read are 0. | ||
| 53 | |||
| 54 | A subclass that overrides C<arpnip> must also override C<arpnip_by_vrf>, or | ||
| 55 | with C<store_vrfs> on this C<arpnip_by_vrf> is used instead of the override. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | use strict; | ||
| 62 | use warnings; | ||
| 63 | |||
| 64 | use Dancer ':script'; | ||
| 65 | use Expect; | ||
| 66 | use NetAddr::MAC qw/mac_as_ieee/; | ||
| 67 | use Moo; | ||
| 68 | |||
| 69 | # Expand short interface name prefixes into canonical ifNames | ||
| 70 | my $IF_NAME_MAP = { | ||
| 71 | Vl => "Vlan", | ||
| 72 | Lo => "Loopback", | ||
| 73 | Fa => "FastEthernet", | ||
| 74 | Gi => "GigabitEthernet", | ||
| 75 | Tw => "TwoGigabitEthernet", | ||
| 76 | Fi => "FiveGigabitEthernet", | ||
| 77 | Te => "TenGigabitEthernet", | ||
| 78 | Twe => "TwentyFiveGigE", | ||
| 79 | Fo => "FortyGigabitEthernet", | ||
| 80 | Hu => "HundredGigE", | ||
| 81 | Po => "Port-channel", | ||
| 82 | Bl => "Bluetooth", | ||
| 83 | Wl => "Wlan-GigabitEthernet", | ||
| 84 | }; | ||
| 85 | |||
| 86 | sub _open_expect { | ||
| 87 | my ($hostlabel, $ssh, $args, $purpose) = @_; | ||
| 88 | |||
| 89 | my ($pty, $pid) = $ssh->open2pty; | ||
| 90 | unless ($pty) { | ||
| 91 | warn "unable to run remote command [$hostlabel] " . $ssh->error; | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $exp = Expect->init($pty); | ||
| 96 | $exp->raw_pty(1); | ||
| 97 | |||
| 98 | my $prompt = qr/[>#]\s*$/; # IOS-XE exec/enable prompt | ||
| 99 | my $timeout = ($args && $args->{timeout}) ? $args->{timeout} : 30; | ||
| 100 | |||
| 101 | my ($pos, $err, $match, $before, $after) = $exp->expect($timeout, -re => $prompt); | ||
| 102 | unless (defined $pos) { | ||
| 103 | info "$hostlabel $$ $purpose() unable to reach CLI prompt"; | ||
| 104 | $exp->hard_close(); | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Disable paging so Expect sees complete command output | ||
| 109 | $exp->send("terminal length 0\n"); | ||
| 110 | ($pos, $err, $match, $before, $after) = $exp->expect($timeout, -re => $prompt); | ||
| 111 | |||
| 112 | debug "$hostlabel $$ $purpose() cli session ready"; | ||
| 113 | return ($exp, $prompt, $timeout); | ||
| 114 | } | ||
| 115 | |||
| 116 | # arpnip skips "default" in any case, as it always has; arpnip_by_vrf passes | ||
| 117 | # $case_sensitive because IOS-XE VRF names are | ||
| 118 | sub _parse_vrfs { | ||
| 119 | my ($text, $case_sensitive) = @_; | ||
| 120 | my @vrfs; | ||
| 121 | |||
| 122 | # "show vrf" includes a Platform iVRF section on some builds; stop before that. | ||
| 123 | for my $line (split /\r?\n/, ($text // '')) { | ||
| 124 | last if $line =~ /^\s*Platform iVRF Name/i; | ||
| 125 | next if $line =~ /^\s*$/; | ||
| 126 | next if $line =~ /^\s*Name\s+Default RD/i; | ||
| 127 | next if $line =~ /^\s*-+\s*$/; | ||
| 128 | |||
| 129 | # IOS-XE prints VRF name in column 1 (indented) | ||
| 130 | if ($line =~ /^\s+(\S+)\s+\S+\s+\S+/) { | ||
| 131 | my $vrf = $1; | ||
| 132 | next if ($case_sensitive ? $vrf eq 'default' : lc($vrf) eq 'default'); # global handled separately | ||
| 133 | push @vrfs, $vrf; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | return @vrfs; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _parse_arp { | ||
| 141 | my ($text, $seen) = @_; | ||
| 142 | my @entries; | ||
| 143 | my $added = 0; | ||
| 144 | |||
| 145 | # Parse both "show ip arp" and "show ip arp vrf <name>" formats. | ||
| 146 | for my $line (split /\r?\n/, ($text // '')) { | ||
| 147 | next if !$line || $line =~ /^\s*$/; | ||
| 148 | next if $line =~ /^\s*Protocol\s+Address\s+Age/i; | ||
| 149 | |||
| 150 | my ($ip) = $line =~ /(\d{1,3}(?:\.\d{1,3}){3})/; | ||
| 151 | my ($mac) = $line =~ /([0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4})/i; | ||
| 152 | next unless $ip && $mac; | ||
| 153 | |||
| 154 | # Dedupe across global + VRFs | ||
| 155 | my $key = lc("$ip|$mac"); | ||
| 156 | next if $seen->{$key}++; | ||
| 157 | |||
| 158 | push @entries, { ip => $ip, mac => mac_as_ieee($mac) }; | ||
| 159 | ++$added; | ||
| 160 | } | ||
| 161 | |||
| 162 | return (\@entries, $added); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub arpnip { | ||
| 166 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 167 | debug "$hostlabel $$ arpnip()"; | ||
| 168 | |||
| 169 | my ($exp, $prompt, $timeout) = _open_expect($hostlabel, $ssh, $args, 'arpnip'); | ||
| 170 | return () unless $exp; | ||
| 171 | |||
| 172 | my @arpentries; | ||
| 173 | my %seen; | ||
| 174 | |||
| 175 | # Stats are only for one end-of-run summary line | ||
| 176 | my %stats = ( global => 0, vrf => {} ); | ||
| 177 | |||
| 178 | my ($pos, $err, $match, $before, $after); | ||
| 179 | |||
| 180 | debug "$hostlabel $$ arpnip() collecting ARP from [global]"; | ||
| 181 | $exp->send("show ip arp\n"); | ||
| 182 | ($pos, $err, $match, $before, $after) = $exp->expect($timeout, -re => $prompt); | ||
| 183 | |||
| 184 | my ($g_entries, $g_added) = _parse_arp($before, \%seen); | ||
| 185 | push @arpentries, @$g_entries; | ||
| 186 | $stats{global} += $g_added; | ||
| 187 | |||
| 188 | # VRF enumeration; failures here should not block global ARP. | ||
| 189 | $exp->send("show vrf\n"); | ||
| 190 | ($pos, $err, $match, $before, $after) = $exp->expect($timeout, -re => $prompt); | ||
| 191 | |||
| 192 | my @vrfs = _parse_vrfs($before); | ||
| 193 | debug "$hostlabel $$ arpnip() detected " . scalar(@vrfs) . " VRFs"; | ||
| 194 | |||
| 195 | for my $vrf (@vrfs) { | ||
| 196 | debug "$hostlabel $$ arpnip() collecting ARP from [vrf:$vrf]"; | ||
| 197 | $exp->send("show ip arp vrf $vrf\n"); | ||
| 198 | ($pos, $err, $match, $before, $after) = $exp->expect($timeout, -re => $prompt); | ||
| 199 | |||
| 200 | my ($v_entries, $v_added) = _parse_arp($before, \%seen); | ||
| 201 | push @arpentries, @$v_entries; | ||
| 202 | $stats{vrf}{$vrf} = ($stats{vrf}{$vrf} || 0) + $v_added; | ||
| 203 | } | ||
| 204 | |||
| 205 | $exp->send("exit\n"); | ||
| 206 | $exp->hard_close(); | ||
| 207 | |||
| 208 | my $total = scalar(@arpentries); | ||
| 209 | my @parts = ("global=$stats{global}"); | ||
| 210 | for my $v (sort keys %{ $stats{vrf} }) { | ||
| 211 | push @parts, "$v=$stats{vrf}{$v}"; | ||
| 212 | } | ||
| 213 | debug "$hostlabel $$ arpnip() summary: total=$total (" . join(', ', @parts) . ")"; | ||
| 214 | |||
| 215 | return @arpentries; | ||
| 216 | } | ||
| 217 | |||
| 218 | # the prompt alone at the end of the output, so a row never ends a read | ||
| 219 | my $VRF_PROMPT = qr/^[^\s#>]+[>#]\s*\z/m; | ||
| 220 | |||
| 221 | my $IPV4_HEADER = qr/^\s*Protocol\s+Address\s+Age/i; | ||
| 222 | my $IPV6_HEADER = qr/^\s*IPv6 Address\s+Age\s+Link-layer/i; | ||
| 223 | my $MAC_DOTTED = qr/[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}/i; | ||
| 224 | |||
| 225 | sub arpnip_by_vrf { | ||
| 226 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 227 | debug "$hostlabel $$ arpnip_by_vrf()"; | ||
| 228 | |||
| 229 | my ($exp, $timeout) = _open_vrf_session($hostlabel, $ssh, $args) or return undef; | ||
| 230 | |||
| 231 | my $list = _run_command($exp, $timeout, 'show vrf'); | ||
| 232 | unless (defined $list) { | ||
| 233 | info "$hostlabel $$ arpnip_by_vrf() show vrf timed out, so VRF collection by CLI was skipped"; | ||
| 234 | _close_vrf_session($exp); | ||
| 235 | return undef; | ||
| 236 | } | ||
| 237 | my @vrfs = _parse_vrfs($list, 1); | ||
| 238 | my $list_whole = _vrf_list_whole($list); | ||
| 239 | info "$hostlabel $$ arpnip_by_vrf() show vrf was not a whole VRF list, so no VRF is vouched absent" | ||
| 240 | unless $list_whole; | ||
| 241 | my $protocols = _parse_vrf_protocols($list); | ||
| 242 | |||
| 243 | my $state = { entries => [], seen => {}, whole => {}, unread => {}, timed_out => 0 }; | ||
| 244 | my $ipv6_answers = 0; | ||
| 245 | my @plan = ([ '', 4, 'show ip arp' ], [ '', 6, 'show ipv6 neighbors' ]); | ||
| 246 | while (my $step = shift @plan) { | ||
| 247 | my ($vrf, $family, $command) = @$step; | ||
| 248 | my $errored = _read_table($state, $exp, $timeout, $vrf, $family, $command); | ||
| 249 | if ($family == 6 and $vrf eq '') { | ||
| 250 | $ipv6_answers = !$errored; | ||
| 251 | push @plan, map { _vrf_steps($_, $protocols, $ipv6_answers, $state) } @vrfs; | ||
| 252 | } | ||
| 253 | if ($state->{timed_out}) { | ||
| 254 | info "$hostlabel $$ arpnip_by_vrf() [$command] timed out, so nothing after it was read"; | ||
| 255 | last; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | _close_vrf_session($exp); | ||
| 259 | |||
| 260 | my @families = ($ipv6_answers ? (4, 6) : (4)); | ||
| 261 | my %vrf_read = map { | ||
| 262 | my $vrf = $_; | ||
| 263 | ($vrf => ((!$state->{unread}->{$vrf} and 0 == grep { !$state->{whole}->{$vrf}->{$_} } @families) ? 1 : 0)) | ||
| 264 | } ('', @vrfs); | ||
| 265 | |||
| 266 | return { | ||
| 267 | entries => $state->{entries}, vrf_read => \%vrf_read, | ||
| 268 | vrf_list_complete => (($state->{timed_out} or !$list_whole) ? 0 : 1), families => \@families, | ||
| 269 | }; | ||
| 270 | } | ||
| 271 | |||
| 272 | # The list is whole only when it holds the header and every other line is | ||
| 273 | # a VRF row or the interface-only line that continues the row above; a | ||
| 274 | # denied or unrecognized reply must not read as "no VRFs". | ||
| 275 | sub _vrf_list_whole { | ||
| 276 | my ($text) = @_; | ||
| 277 | my ($header, $after_row, $interfaces_column) = (0, 0, undef); | ||
| 278 | for my $line (split /\r?\n/, $text) { | ||
| 279 | last if $line =~ /^\s*Platform iVRF Name/i; | ||
| 280 | next if $line =~ /^\s*$/ or $line =~ /^\s*-+\s*$/; | ||
| 281 | return 0 if $line =~ /^\s*%/; | ||
| 282 | if ($line =~ /^\s*Name\s+Default RD/i) { | ||
| 283 | ($header, $interfaces_column) = (1, index($line, 'Interfaces')); | ||
| 284 | next; | ||
| 285 | } | ||
| 286 | if ($line =~ /^\s+\S+\s+\S+\s+\S+/) { $after_row = 1; next } | ||
| 287 | # a wrapped VRF name sits in the name column; only the Interfaces column continues a row | ||
| 288 | return 0 unless $after_row and $interfaces_column >= 0 | ||
| 289 | and $line =~ /^(\s+)([A-Za-z]\S*\d\S*)\s*$/ and length($1) >= $interfaces_column; | ||
| 290 | } | ||
| 291 | return $header; | ||
| 292 | } | ||
| 293 | |||
| 294 | # the commands for one VRF; a VRF that lists no ipv6 has no IPv6 table, so | ||
| 295 | # that family is whole and empty with no command sent. A name that is not | ||
| 296 | # plain is never sent, and "all" would answer for every VRF at once. | ||
| 297 | sub _vrf_steps { | ||
| 298 | my ($vrf, $protocols, $ipv6_answers, $state) = @_; | ||
| 299 | unless ($vrf =~ m/\A[A-Za-z0-9_.:-]+\z/ and lc $vrf ne 'all') { | ||
| 300 | $state->{unread}->{$vrf} = 1; | ||
| 301 | return (); | ||
| 302 | } | ||
| 303 | my @steps = ([ $vrf, 4, "show ip arp vrf $vrf" ]); | ||
| 304 | if (!$ipv6_answers) { return @steps } | ||
| 305 | if (($protocols->{$vrf} // 'ipv6') =~ m/ipv6/i) { | ||
| 306 | push @steps, [ $vrf, 6, "show ipv6 neighbors vrf $vrf" ]; | ||
| 307 | } | ||
| 308 | else { $state->{whole}->{$vrf}->{6} = 1 } | ||
| 309 | return @steps; | ||
| 310 | } | ||
| 311 | |||
| 312 | # VRF name => its Protocols column, where the row has one; wrapped interface | ||
| 313 | # lines hold no name and protocols, so they never match | ||
| 314 | sub _parse_vrf_protocols { | ||
| 315 | my ($text) = @_; | ||
| 316 | my %protocols; | ||
| 317 | for my $line (split /\r?\n/, ($text // '')) { | ||
| 318 | last if $line =~ /^\s*Platform iVRF Name/i; | ||
| 319 | next unless $line =~ m/^\s+(\S+)\s+.*?\s(ipv[46](?:,ipv[46])*)(?:\s|$)/i; | ||
| 320 | $protocols{$1} = $2; | ||
| 321 | } | ||
| 322 | return \%protocols; | ||
| 323 | } | ||
| 324 | |||
| 325 | # returns the Expect session once the pager is off, and the timeout, or nothing | ||
| 326 | sub _open_vrf_session { | ||
| 327 | my ($hostlabel, $ssh, $args) = @_; | ||
| 328 | |||
| 329 | my ($pty, $pid) = $ssh->open2pty; | ||
| 330 | unless ($pty) { | ||
| 331 | warn "unable to run remote command [$hostlabel] " . $ssh->error; | ||
| 332 | return; | ||
| 333 | } | ||
| 334 | |||
| 335 | my $exp = Expect->init($pty); | ||
| 336 | $exp->raw_pty(1); | ||
| 337 | my $timeout = ($args && $args->{timeout}) ? $args->{timeout} : 30; | ||
| 338 | |||
| 339 | my ($pos) = $exp->expect($timeout, -re => $VRF_PROMPT); | ||
| 340 | # width 0 keeps a long command from scrolling the echo, which would spoil the read | ||
| 341 | unless ($pos and defined _run_command($exp, $timeout, 'terminal length 0') | ||
| 342 | and defined _run_command($exp, $timeout, 'terminal width 0')) { | ||
| 343 | info "$hostlabel $$ arpnip_by_vrf() unable to reach CLI prompt"; | ||
| 344 | $exp->hard_close(); | ||
| 345 | return; | ||
| 346 | } | ||
| 347 | |||
| 348 | return ($exp, $timeout); | ||
| 349 | } | ||
| 350 | |||
| 351 | sub _close_vrf_session { | ||
| 352 | my $exp = shift; | ||
| 353 | $exp->send("exit\n"); | ||
| 354 | $exp->hard_close(); | ||
| 355 | } | ||
| 356 | |||
| 357 | # the command's output without the echoed command, or undef on a timeout, | ||
| 358 | # after which Expect still holds the partial output | ||
| 359 | sub _run_command { | ||
| 360 | my ($exp, $timeout, $command) = @_; | ||
| 361 | |||
| 362 | $exp->send("$command\n"); | ||
| 363 | my ($pos, undef, undef, $before) = $exp->expect($timeout, -re => $VRF_PROMPT); | ||
| 364 | return undef unless $pos; | ||
| 365 | |||
| 366 | my @lines = split /\r?\n/, ($before // ''); | ||
| 367 | shift @lines if @lines and $lines[0] =~ m/^\s*\Q$command\E\s*$/; | ||
| 368 | return join "\n", @lines; | ||
| 369 | } | ||
| 370 | |||
| 371 | # runs one command and records its entries and whether it was read whole; | ||
| 372 | # returns true when the device answered with an error or timed out | ||
| 373 | sub _read_table { | ||
| 374 | my ($state, $exp, $timeout, $vrf, $family, $command) = @_; | ||
| 375 | |||
| 376 | my $output = _run_command($exp, $timeout, $command); | ||
| 377 | unless (defined $output) { | ||
| 378 | $state->{timed_out} = 1; | ||
| 379 | $state->{unread}->{$vrf} = 1; | ||
| 380 | return 1; | ||
| 381 | } | ||
| 382 | |||
| 383 | my ($entries, $whole, $errored) = _parse_table($output, $family); | ||
| 384 | foreach my $entry (@$entries) { | ||
| 385 | my $key = lc join '|', $vrf, @$entry{qw/ip mac/}; | ||
| 386 | next if $state->{seen}->{$key}++; | ||
| 387 | push @{ $state->{entries} }, { %$entry, vrf => $vrf }; | ||
| 388 | } | ||
| 389 | $state->{whole}->{$vrf}->{$family} = $whole; | ||
| 390 | return $errored; | ||
| 391 | } | ||
| 392 | |||
| 393 | # returns the entries, whether every line was a header or an entry, and | ||
| 394 | # whether a "%" error line was seen | ||
| 395 | sub _parse_table { | ||
| 396 | my ($output, $family) = @_; | ||
| 397 | my ($header, $row) = ($family == 4 | ||
| 398 | ? ($IPV4_HEADER, \&_parse_arp_row) : ($IPV6_HEADER, \&_parse_neighbor_row)); | ||
| 399 | my (@entries, $errored); | ||
| 400 | my $whole = 1; | ||
| 401 | |||
| 402 | foreach my $line (split /\r?\n/, $output) { | ||
| 403 | next if $line =~ m/^\s*$/ or $line =~ $header; | ||
| 404 | if ($line =~ m/^\s*%/) { $errored = 1; $whole = 0; next } | ||
| 405 | my ($recognized, $entry) = $row->($line); | ||
| 406 | $whole = 0 unless $recognized; | ||
| 407 | push @entries, $entry if $entry; | ||
| 408 | } | ||
| 409 | |||
| 410 | return (\@entries, $whole, $errored); | ||
| 411 | } | ||
| 412 | |||
| 413 | # returns whether the line is a row, and its entry unless the row is incomplete | ||
| 414 | sub _parse_arp_row { | ||
| 415 | my ($line) = @_; | ||
| 416 | return (0) unless $line =~ m/^\s*Internet\s+(\d{1,3}(?:\.\d{1,3}){3})\s+\S+\s+(?:($MAC_DOTTED)|Incomplete)\s/; | ||
| 417 | return (1, ($2 ? { ip => $1, mac => mac_as_ieee($2) } : undef)); | ||
| 418 | } | ||
| 419 | |||
| 420 | # IOS-XE prints IPv6 addresses in uppercase; an INCMP row has no usable MAC | ||
| 421 | sub _parse_neighbor_row { | ||
| 422 | my ($line) = @_; | ||
| 423 | return (0) unless $line =~ m/^\s*([0-9a-f:.]+)\s+(?:\d+|-)\s+(?:($MAC_DOTTED)|\S+)\s+([A-Z]+)\s+\S+\s*$/i; | ||
| 424 | my ($ip, $mac, $state) = ($1, $2, $3); | ||
| 425 | return (1, { ip => lc $ip, mac => mac_as_ieee($mac) }) if $mac; | ||
| 426 | return (uc($state) eq 'INCMP' ? 1 : 0); | ||
| 427 | } | ||
| 428 | |||
| 429 | sub macsuck { | ||
| 430 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 431 | debug "$hostlabel $$ macsuck()"; | ||
| 432 | |||
| 433 | my ($exp, $prompt, $timeout) = _open_expect($hostlabel, $ssh, $args, 'macsuck'); | ||
| 434 | return unless $exp; | ||
| 435 | |||
| 436 | my ($pos, $err, $match, $before, $after); | ||
| 437 | |||
| 438 | $exp->send("show mac address-table\n"); | ||
| 439 | ($pos, $err, $match, $before, $after) = $exp->expect($timeout, -re => $prompt); | ||
| 440 | |||
| 441 | my @lines = split /\r?\n/, ($before // ''); | ||
| 442 | $exp->send("exit\n"); | ||
| 443 | $exp->hard_close(); | ||
| 444 | |||
| 445 | my $macentries = {}; | ||
| 446 | |||
| 447 | # Tolerant line match: grab VLAN + MAC + final port token | ||
| 448 | # Handles variants with extra columns (age/secure/ntfy/etc). | ||
| 449 | my $re_line = qr{ | ||
| 450 | ^\s* | ||
| 451 | (\S+) # VLAN (or All) | ||
| 452 | \s+([0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}) # MAC dotted | ||
| 453 | \s+.*? # anything (Type / Age / flags) | ||
| 454 | \s+(\S+) # Port (last column token) | ||
| 455 | \s*$ | ||
| 456 | }ix; | ||
| 457 | |||
| 458 | LINE: for my $line (@lines) { | ||
| 459 | next if $line =~ /^\s*(Vlan|----|Mac Address Table|Total|$)/i; | ||
| 460 | |||
| 461 | if ($line =~ $re_line) { | ||
| 462 | my ($vlan_raw, $mac_dotted, $port_raw) = ($1, $2, $3); | ||
| 463 | |||
| 464 | my $vlan = 0; | ||
| 465 | if ($vlan_raw =~ /^\d+$/) { $vlan = $vlan_raw; } | ||
| 466 | elsif (lc($vlan_raw) eq 'all') { $vlan = 0; } | ||
| 467 | else { next LINE; } | ||
| 468 | |||
| 469 | next LINE if uc($port_raw) eq 'CPU'; | ||
| 470 | |||
| 471 | # Expand short ifName prefix | ||
| 472 | my ($pfx, $rest) = ($port_raw =~ /^([A-Za-z]+)(.*)$/); | ||
| 473 | my $port = defined $pfx | ||
| 474 | ? sprintf('%s%s', ($IF_NAME_MAP->{$pfx} || $pfx), ($rest || '')) | ||
| 475 | : $port_raw; | ||
| 476 | |||
| 477 | my $mac_ieee = mac_as_ieee($mac_dotted); | ||
| 478 | ++$macentries->{$vlan}->{$port}->{$mac_ieee}; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | debug "$hostlabel $$ macsuck() parsed VLANs: " . join(',', sort keys %$macentries) | ||
| 483 | if keys %$macentries; | ||
| 484 | |||
| 485 | return $macentries; | ||
| 486 | } | ||
| 487 | |||
| 488 | 1; |