« blib/lib/App/Netdisco/SSHCollector/Platform/FreeBSD.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/Linux.pm »
| 1 | package App::Netdisco::SSHCollector::Platform::GAIAEmbedded; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Netdisco::SSHCollector::Platform::GAIAEmbedded | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Collect ARP entries from Checkpoint GAIA embedded Systems | ||
| 10 | To get this Plugin to work you have to add a user like 'netdisco' with | ||
| 11 | 'Network admin' rights in the GAIA embedded OS. | ||
| 12 | |||
| 13 | This collector uses "C<arp>" as the command for the arp utility on your | ||
| 14 | system. If you wish to specify an absolute path, then add an C<arp_command> | ||
| 15 | item to your configuration: | ||
| 16 | |||
| 17 | device_auth: | ||
| 18 | - tag: sshgaia | ||
| 19 | driver: cli | ||
| 20 | platform: GAIAEmbedded | ||
| 21 | only: '192.0.2.1' | ||
| 22 | username: oliver | ||
| 23 | password: letmein | ||
| 24 | arp_command: 'arp' | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | |||
| 31 | use Dancer ':script'; | ||
| 32 | use Expect; | ||
| 33 | use Moo; | ||
| 34 | |||
| 35 | =head1 PUBLIC METHODS | ||
| 36 | |||
| 37 | =over 4 | ||
| 38 | |||
| 39 | =item B<arpnip($host, $ssh)> | ||
| 40 | |||
| 41 | Retrieve ARP entries from device. C<$host> is the hostname or IP address | ||
| 42 | of the device. C<$ssh> is a Net::OpenSSH connection to the device. | ||
| 43 | |||
| 44 | Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub arpnip { | ||
| 51 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 52 | |||
| 53 | debug "$hostlabel $$ arpnip()"; | ||
| 54 | |||
| 55 | my ($pty, $pid) = $ssh->open2pty; | ||
| 56 | unless ($pty) { | ||
| 57 | debug "unable to run remote command [$hostlabel] " . $ssh->error; | ||
| 58 | return (); | ||
| 59 | } | ||
| 60 | my $expect = Expect->init($pty); | ||
| 61 | |||
| 62 | my ($pos, $error, $match, $before, $after); | ||
| 63 | my $prompt = qr/>/; | ||
| 64 | |||
| 65 | ($pos, $error, $match, $before, $after) = $expect->expect(30, -re, $prompt); | ||
| 66 | |||
| 67 | my $command = ($args->{arp_command} || 'arp'); | ||
| 68 | $expect->send("$command -n \n"); | ||
| 69 | ($pos, $error, $match, $before, $after) = $expect->expect(30, -re, $prompt); | ||
| 70 | |||
| 71 | my @arpentries = (); | ||
| 72 | my @lines = split(m/\n/, $before); | ||
| 73 | |||
| 74 | # ? (192.168.17.178) at 5C:F9:DD:71:1F:08 [ether] on LAN1 | ||
| 75 | # https://github.com/netdisco/netdisco/issues/365 | ||
| 76 | my $linereg = qr/([0-9.]+)\s+ether\s+([a-fA-F0-9:]+).+/; | ||
| 77 | foreach my $line (@lines) { | ||
| 78 | if ($line =~ $linereg) { | ||
| 79 | my ($ip, $mac) = ($1, $2); | ||
| 80 | push @arpentries, { mac => $mac, ip => $ip }; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | $expect->send("exit\n"); | ||
| 85 | $expect->soft_close(); | ||
| 86 | |||
| 87 | return @arpentries; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; |