« blib/lib/App/Netdisco/SSHCollector/Platform/ASA.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/VOSS.pm »
| 1 | package App::Netdisco::SSHCollector::Platform::ASAContext; | ||
| 2 | |||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | App::Netdisco::SSHCollector::Platform::ASAContext | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | Collect IPv4 ARP and IPv6 neighbor entries from Cisco ASA devices. | ||
| 11 | |||
| 12 | You will need the following configuration for the user to automatically enter | ||
| 13 | C<enable> status after login: | ||
| 14 | |||
| 15 | aaa authorization exec LOCAL auto-enable | ||
| 16 | |||
| 17 | To use an C<enable> password seaparate from the login password, add an | ||
| 18 | C<enable_password> under C<device_auth> in your configuration file: | ||
| 19 | |||
| 20 | device_auth: | ||
| 21 | - tag: sshasa | ||
| 22 | driver: cli | ||
| 23 | platform: ASAContext | ||
| 24 | only: '192.0.2.1' | ||
| 25 | username: oliver | ||
| 26 | password: letmein | ||
| 27 | enable_password: myenablepass | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | |||
| 34 | use Dancer ':script'; | ||
| 35 | use Expect; | ||
| 36 | use Moo; | ||
| 37 | |||
| 38 | =head1 PUBLIC METHODS | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =item B<arpnip($host, $ssh)> | ||
| 43 | |||
| 44 | Retrieve ARP and neighbor entries from device. C<$host> is the hostname or IP | ||
| 45 | address of the device. C<$ssh> is a Net::OpenSSH connection to the device. | ||
| 46 | |||
| 47 | Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>. | ||
| 48 | |||
| 49 | This was kindly created by @haught and mentioned in https://github.com/netdisco/netdisco/issues/754 | ||
| 50 | as being a context-aware version of ASA.pm. | ||
| 51 | |||
| 52 | The code is imported from https://github.com/haught/netdisco/blob/ASAContext/lib/App/Netdisco/SSHCollector/Platform/ASAContext.pm. | ||
| 53 | However this version did not have some ASA.pm improvements added in dc9feb747f..b58a62f300, | ||
| 54 | so we tried to merge all of this in here. However we lack the ability to try it, so we also | ||
| 55 | left in place the original ASA.pm which is confirmed to work. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub arpnip { | ||
| 62 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 63 | |||
| 64 | debug "$hostlabel $$ arpnip()"; | ||
| 65 | |||
| 66 | my ($pty, $pid) = $ssh->open2pty; | ||
| 67 | unless ($pty) { | ||
| 68 | debug "unable to run remote command [$hostlabel] " . $ssh->error; | ||
| 69 | return (); | ||
| 70 | } | ||
| 71 | my $expect = Expect->init($pty); | ||
| 72 | |||
| 73 | my ($pos, $error, $match, $before, $after); | ||
| 74 | my $prompt; | ||
| 75 | |||
| 76 | if ($args->{enable_password}) { | ||
| 77 | $prompt = qr/>/; | ||
| 78 | ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt); | ||
| 79 | |||
| 80 | $expect->send("enable\n"); | ||
| 81 | |||
| 82 | $prompt = qr/Password:/; | ||
| 83 | ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt); | ||
| 84 | |||
| 85 | $expect->send( $args->{enable_password} ."\n" ); | ||
| 86 | } | ||
| 87 | |||
| 88 | $prompt = qr/#\s*$/; | ||
| 89 | ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt); | ||
| 90 | |||
| 91 | $expect->send("terminal pager 2147483647\n"); | ||
| 92 | ($pos, $error, $match, $before, $after) = $expect->expect(5, -re, $prompt); | ||
| 93 | |||
| 94 | $expect->send("changeto system\n"); | ||
| 95 | ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt); | ||
| 96 | |||
| 97 | $expect->send("show run | include ^context\n"); | ||
| 98 | ($pos, $error, $match, $before, $after) = $expect->expect(60, -re, $prompt); | ||
| 99 | my @contexts = split(m/\n/, $before); | ||
| 100 | |||
| 101 | |||
| 102 | my @arpentries = (); | ||
| 103 | for (my $i = 1; $i < scalar @contexts; $i++) { | ||
| 104 | $contexts[$i] =~ s/context //; | ||
| 105 | debug "$hostlabel/$contexts[$i]"; | ||
| 106 | $expect->send("changeto context $contexts[$i]\n"); | ||
| 107 | ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt); | ||
| 108 | |||
| 109 | $expect->send("show names\n"); | ||
| 110 | ($pos, $error, $match, $before, $after) = $expect->expect(60, -re, $prompt); | ||
| 111 | |||
| 112 | my @names = split(m/\n/, $before); | ||
| 113 | |||
| 114 | $expect->send("terminal pager 2147483647\n"); | ||
| 115 | ($pos, $error, $match, $before, $after) = $expect->expect(5, -re, $prompt); | ||
| 116 | |||
| 117 | $expect->send("show arp\n"); | ||
| 118 | ($pos, $error, $match, $before, $after) = $expect->expect(60, -re, $prompt); | ||
| 119 | |||
| 120 | my @lines = split(m/\n/, $before); | ||
| 121 | |||
| 122 | # ifname 192.0.2.1 0011.2233.4455 123 | ||
| 123 | my $linereg = qr/[A-z0-9\-\.]+\s([A-z0-9\-\.]+)\s | ||
| 124 | ([0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4})/x; | ||
| 125 | |||
| 126 | foreach my $line(@lines) { | ||
| 127 | if ($line =~ $linereg) { | ||
| 128 | my ($ip, $mac) = ($1, $2); | ||
| 129 | if ($ip !~ m/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) { | ||
| 130 | foreach my $name (@names) { | ||
| 131 | if ($name =~ qr/name\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s([\w-]*)/x) { | ||
| 132 | if ($ip eq $2) { | ||
| 133 | $ip = $1; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | if ($ip =~ m/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) { | ||
| 139 | push @arpentries, { mac => $mac, ip => $ip }; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | # start ipv6 | ||
| 145 | $expect->send("show ipv6 neighbor\n"); | ||
| 146 | ($pos, $error, $match, $before, $after) = $expect->expect(60, -re, $prompt); | ||
| 147 | |||
| 148 | @lines = split(m/\n/, $before); | ||
| 149 | |||
| 150 | # IPv6 age MAC state ifname | ||
| 151 | $linereg = qr/([0-9a-fA-F\:]+)\s+[0-9]+\s | ||
| 152 | ([0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4})/x; | ||
| 153 | |||
| 154 | foreach my $line (@lines) { | ||
| 155 | if ($line =~ $linereg) { | ||
| 156 | my ($ip, $mac) = ($1, $2); | ||
| 157 | push @arpentries, { mac => $mac, ip => $ip }; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | # end ipv6 | ||
| 161 | } | ||
| 162 | $expect->send("exit\n"); | ||
| 163 | $expect->soft_close(); | ||
| 164 | return @arpentries; | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; | ||
| 168 | |||
| 169 |