« blib/lib/App/Netdisco/Backend/Role/Scheduler.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/EOS.pm »
| 1 | package App::Netdisco::Transport::SSH; | ||
| 2 | |||
| 3 | use Dancer qw/:syntax :script/; | ||
| 4 | |||
| 5 | use App::Netdisco::Util::Device 'get_device'; | ||
| 6 | use Module::Load (); | ||
| 7 | use Net::OpenSSH; | ||
| 8 | use Try::Tiny; | ||
| 9 | |||
| 10 | use base 'Dancer::Object::Singleton'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | App::Netdisco::Transport::SSH | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Returns an object which has an active SSH connection which can be used | ||
| 19 | for some actions such as arpnip. | ||
| 20 | |||
| 21 | my $cli = App::Netdisco::Transport::SSH->session_for( ... ); | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | __PACKAGE__->attributes(qw/ sessions /); | ||
| 26 | |||
| 27 | sub init { | ||
| 28 | my ( $class, $self ) = @_; | ||
| 29 | $SIG{CHLD} = 'IGNORE'; | ||
| 30 | $self->sessions( {} ); | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | =head1 session_for( $ip ) | ||
| 35 | |||
| 36 | Given an IP address, returns an object instance configured for and connected | ||
| 37 | to that device. | ||
| 38 | |||
| 39 | Returns C<undef> if the connection fails. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | { | ||
| 44 | package MySession; | ||
| 45 | use Moo; | ||
| 46 | |||
| 47 | has 'ssh' => ( is => 'rw' ); | ||
| 48 | has 'auth' => ( is => 'rw' ); | ||
| 49 | has 'host' => ( is => 'rw' ); | ||
| 50 | has 'platform' => ( is => 'rw' ); | ||
| 51 | |||
| 52 | sub arpnip { | ||
| 53 | my $self = shift; | ||
| 54 | $self->platform->arpnip(@_, $self->host, $self->ssh, $self->auth) | ||
| 55 | if $self->platform->can('arpnip'); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub arpnip_by_vrf { | ||
| 59 | my $self = shift; | ||
| 60 | $self->platform->arpnip_by_vrf(@_, $self->host, $self->ssh, $self->auth) | ||
| 61 | if $self->platform->can('arpnip_by_vrf'); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub macsuck { | ||
| 65 | my $self = shift; | ||
| 66 | $self->platform->macsuck(@_, $self->host, $self->ssh, $self->auth) | ||
| 67 | if $self->platform->can('macsuck'); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub subnets_by_vrf { | ||
| 71 | my $self = shift; | ||
| 72 | $self->platform->subnets_by_vrf(@_, $self->host, $self->ssh, $self->auth) | ||
| 73 | if $self->platform->can('subnets_by_vrf'); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub subnets { | ||
| 77 | my $self = shift; | ||
| 78 | $self->platform->subnets(@_, $self->host, $self->ssh, $self->auth) | ||
| 79 | if $self->platform->can('subnets'); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub session_for { | ||
| 84 | my ($class, $ip) = @_; | ||
| 85 | |||
| 86 | my $device = get_device($ip) or return undef; | ||
| 87 | my $sessions = $class->instance->sessions or return undef; | ||
| 88 | |||
| 89 | return $sessions->{$device->ip} if exists $sessions->{$device->ip}; | ||
| 90 | debug sprintf 'cli session cache warm: [%s]', $device->ip; | ||
| 91 | |||
| 92 | my $auth = (setting('device_auth') || []); | ||
| 93 | if (1 != scalar @$auth) { | ||
| 94 | error sprintf " [%s] require only one matching auth stanza", $device->ip; | ||
| 95 | return undef; | ||
| 96 | } | ||
| 97 | $auth = $auth->[0]; | ||
| 98 | |||
| 99 | if (!defined $auth->{platform}) { | ||
| 100 | error sprintf " [%s] Perl SSH platform not specified, assuming Python", $device->ip; | ||
| 101 | return undef; | ||
| 102 | } | ||
| 103 | |||
| 104 | my @master_opts = qw(-o BatchMode=no); | ||
| 105 | push(@master_opts, @{$auth->{ssh_master_opts}}) | ||
| 106 | if $auth->{ssh_master_opts}; | ||
| 107 | |||
| 108 | # bit 512 traces the ssh process into files under /tmp | ||
| 109 | $Net::OpenSSH::debug = ($ENV{SSH_TRACE} || 0) & ~512; | ||
| 110 | my $ssh = Net::OpenSSH->new( | ||
| 111 | $device->ip, | ||
| 112 | user => $auth->{username}, | ||
| 113 | password => $auth->{password}, | ||
| 114 | key_path => $auth->{key_path}, | ||
| 115 | passphrase => $auth->{passphrase}, | ||
| 116 | port => $auth->{port}, | ||
| 117 | batch_mode => $auth->{batch_mode}, | ||
| 118 | timeout => $auth->{timeout} ? $auth->{timeout} : 30, | ||
| 119 | async => 0, | ||
| 120 | default_stderr_file => '/dev/null', | ||
| 121 | master_opts => \@master_opts | ||
| 122 | ); | ||
| 123 | |||
| 124 | if ($ssh->error) { | ||
| 125 | error sprintf " [%s] ssh connection error [%s]", $device->ip, $ssh->error; | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | elsif (! $ssh) { | ||
| 129 | error sprintf " [%s] Net::OpenSSH instantiation error", $device->ip; | ||
| 130 | return undef; | ||
| 131 | } | ||
| 132 | |||
| 133 | my $platform = "App::Netdisco::SSHCollector::Platform::" . $auth->{platform}; | ||
| 134 | my $happy = false; | ||
| 135 | try { | ||
| 136 | Module::Load::load $platform; | ||
| 137 | $happy = true; | ||
| 138 | } catch { error $_ }; | ||
| 139 | return unless $happy; | ||
| 140 | |||
| 141 | my $sess = MySession->new( | ||
| 142 | ssh => $ssh, | ||
| 143 | auth => $auth, | ||
| 144 | host => $device->ip, | ||
| 145 | platform => $platform->new(), | ||
| 146 | ); | ||
| 147 | |||
| 148 | return ($sessions->{$device->ip} = $sess); | ||
| 149 | } | ||
| 150 | |||
| 151 | true; |