File Coverage

blib/lib/Net/Appliance/Session/Transport.pm
Criterion Covered Total %
statement 9 45 20.0
branch 0 34 0.0
condition 0 18 0.0
subroutine 3 5 60.0
pod 0 2 0.0
total 12 104 11.5


line stmt bran cond sub pod time code
1             package Net::Appliance::Session::Transport;
2             { $Net::Appliance::Session::Transport::VERSION = '4.300005' }
3              
4             {
5             package # hide from pause
6             Net::Appliance::Session::Transport::ConnectOptions;
7 1     1   578 use Moo;
  1         2  
  1         8  
8 1     1   314 use MooX::Types::MooseLike::Base qw(Str);
  1         3  
  1         114  
9              
10             has username => (
11             is => 'ro',
12             isa => Str,
13             required => 0,
14             predicate => 1,
15             );
16              
17             has password => (
18             is => 'ro',
19             isa => Str,
20             required => 0,
21             predicate => 1,
22             );
23              
24             has privileged_password => (
25             is => 'ro',
26             isa => Str,
27             required => 0,
28             predicate => 1,
29             );
30             }
31              
32 1     1   15 use Moo::Role;
  1         3  
  1         12  
33              
34             sub connect {
35 0     0 0   my $self = shift;
36 0           my $options = Net::Appliance::Session::Transport::ConnectOptions->new(@_);
37              
38 0           foreach my $slot (qw/ username password privileged_password /) {
39 0           my $has = 'has_' . $slot;
40 0           my $set = 'set_' . $slot;
41 0 0         $self->$set($options->$slot) if $options->$has;
42             }
43              
44 0 0 0       if ($self->nci->transport->is_win32 and $self->has_password) {
45 0           $self->set_password($self->get_password . $self->nci->transport->ors);
46             }
47              
48             # SSH transport takes a username if we have one
49 0 0 0       $self->nci->transport->connect_options->username($self->get_username)
50             if $self->has_username
51             and $self->nci->transport->connect_options->can('username');
52              
53             # poke remote device (whether logging in or not)
54 0           $self->find_prompt($self->wake_up);
55              
56             # optionally, log in to the remote host
57 0 0 0       if ($self->do_login and not $self->prompt_looks_like('generic')) {
58              
59 0 0 0       if ($self->nci->phrasebook->has_prompt('user')
60             and $self->prompt_looks_like('user')) {
61 0 0         die 'a set username is required to connect to this host'
62             if not $self->has_username;
63              
64 0           $self->cmd($self->get_username, { match => 'pass' });
65             }
66              
67 0 0         die 'a set password is required to connect to this host'
68             if not $self->has_password;
69              
70             # support for serial console servers where, after loggin in to the
71             # server, the console is asleep and needs waking up to show its prompt
72 0           $self->say($self->get_password);
73 0           $self->find_prompt($self->wake_up);
74             }
75              
76 0 0         $self->prompt_looks_like('generic')
77             or die 'login failed to remote host - prompt does not match';
78              
79 0           $self->close_called(0);
80 0           $self->logged_in(1);
81              
82 0 0         $self->in_privileged_mode( $self->do_privileged_mode ? 0 : 1 );
83 0 0         $self->in_configure_mode( $self->do_configure_mode ? 0 : 1 );
84              
85             # disable paging... this is undone in our close() method
86 0 0         $self->disable_paging if $self->do_paging;
87              
88 0           return $self;
89             }
90              
91             sub close {
92 0     0 0   my $self = shift;
93              
94             # protect against death spiral (rt.cpan #53796)
95 0 0         return if $self->close_called;
96 0           $self->close_called(1);
97              
98 0 0         if ($self->nci->transport->connect_ready) {
99 0 0 0       $self->end_configure
100             if $self->do_configure_mode and $self->in_configure_mode;
101 0 0 0       $self->end_privileged
102             if $self->do_privileged_mode and $self->in_privileged_mode;
103              
104             # re-enable paging
105 0 0         $self->enable_paging if $self->do_paging;
106              
107             # issue disconnect macro if the phrasebook has one
108 0 0         if ($self->nci->phrasebook->has_macro('disconnect')) {
109 0           eval { $self->macro('disconnect') };
  0            
110             # this should die as there's no returned prompt (NCI pump() fails)
111             }
112              
113 0           $self->nci->transport->disconnect;
114             # there is no longer a transport
115             }
116              
117 0           $self->logged_in(0);
118             }
119              
120             1;