File Coverage

blib/lib/Net/Appliance/Session/Engine.pm
Criterion Covered Total %
statement 9 75 12.0
branch 0 74 0.0
condition 0 12 0.0
subroutine 3 9 33.3
pod 0 6 0.0
total 12 176 6.8


line stmt bran cond sub pod time code
1             package Net::Appliance::Session::Engine;
2             { $Net::Appliance::Session::Engine::VERSION = '4.300003' }
3              
4 1     1   527 use Moo::Role;
  1         3  
  1         5  
5 1     1   305 use Sub::Quote;
  1         2  
  1         57  
6 1     1   6 use MooX::Types::MooseLike::Base qw(Int);
  1         2  
  1         859  
7              
8             has 'pager_enable_lines' => (
9             is => 'rw',
10             isa => Int,
11             required => 0,
12             default => quote_sub('24'),
13             );
14              
15             has 'pager_disable_lines' => (
16             is => 'rw',
17             isa => Int,
18             required => 0,
19             default => quote_sub('0'),
20             );
21              
22             sub enable_paging {
23 0     0 0   my $self = shift;
24              
25 0 0         return unless $self->do_paging;
26 0 0         return unless $self->logged_in;
27              
28 0           my $privstate = $self->in_privileged_mode;
29 0 0         $self->begin_privileged if $self->privileged_paging;
30              
31 0 0         my $pagercmd = ($self->nci->phrasebook->has_macro('enable_paging')
32             ? 'enable_paging' : 'paging');
33              
34 0           $self->macro($pagercmd, { params => [
35             $self->pager_enable_lines
36             ]} );
37              
38 0 0 0       $self->end_privileged
39             if $self->privileged_paging and not $privstate;
40             }
41              
42             sub disable_paging {
43 0     0 0   my $self = shift;
44              
45 0 0         return unless $self->do_paging;
46 0 0         return unless $self->logged_in;
47              
48 0           my $privstate = $self->in_privileged_mode;
49 0 0         $self->begin_privileged if $self->privileged_paging;
50              
51 0 0         my $pagercmd = ($self->nci->phrasebook->has_macro('disable_paging')
52             ? 'disable_paging' : 'paging');
53              
54 0           $self->macro($pagercmd, { params => [
55             $self->pager_disable_lines
56             ]} );
57              
58 0 0 0       $self->end_privileged
59             if $self->privileged_paging and not $privstate;
60             }
61              
62             # method to enter privileged mode on the remote device.
63             # optionally, use a different username and password to those
64             # used at login time. if using a different username then we'll
65             # explicily login rather than privileged.
66              
67             sub begin_privileged {
68 0     0 0   my $self = shift;
69 0           my $options = Net::Appliance::Session::Transport::ConnectOptions->new(@_);
70              
71 0 0         return unless $self->do_privileged_mode;
72 0 0         return if $self->in_privileged_mode;
73              
74 0 0         die 'must connect before you can begin_privileged'
75             unless $self->logged_in;
76              
77             # rt.cpan#47214 check if we are already enabled by peeking the prompt
78 0 0         if ($self->prompt_looks_like('privileged')) {
79 0           $self->in_privileged_mode(1);
80 0           return;
81             }
82              
83             # default is to re-use login credentials
84 0 0         my $username = $options->has_username ? $options->username : $self->get_username;
85              
86             # rt.cpan#69139 support passing of privileged_password to the constructor
87 0 0         my $password = $options->has_password ? $options->password :
    0          
88             $self->has_privileged_password ? $self->get_privileged_password
89             : $self->get_password;
90              
91 0           $self->macro('begin_privileged');
92              
93             # whether login or enable, we still must be prepared for username
94 0 0         if ($self->prompt_looks_like('user')) {
95 0 0         die 'a set username is required to enter priv on this host'
96             if not $username;
97            
98 0           $self->cmd($username, { match => 'pass' });
99             }
100              
101 0 0         if ($self->prompt_looks_like('pass')) {
102 0 0         die 'a set password is required before begin_privileged'
103             if not $password;
104              
105             # rt.cpan#92376 timeout when incorrect password
106 0           $self->cmd($password, { match => 'generic' });
107             }
108              
109 0 0         $self->prompt_looks_like('privileged')
110             or die 'should be in privileged mode but prompt does not match';
111 0           $self->in_privileged_mode(1);
112             }
113              
114             sub end_privileged {
115 0     0 0   my $self = shift;
116            
117 0 0         return unless $self->do_privileged_mode;
118 0 0         return unless $self->in_privileged_mode;
119              
120 0 0         die 'must leave configure mode before leaving privileged mode'
121             if $self->in_configure_mode;
122              
123 0           $self->macro('end_privileged');
124              
125 0 0         not $self->prompt_looks_like('privileged')
126             or die 'should have left privileged mode but prompt still matches';
127 0           $self->in_privileged_mode(0);
128             }
129              
130             sub begin_configure {
131 0     0 0   my $self = shift;
132              
133 0 0         return unless $self->do_configure_mode;
134 0 0         return if $self->in_configure_mode;
135              
136 0 0         die 'must enter privileged mode before configure mode'
137             unless $self->in_privileged_mode;
138              
139             # rt.cpan#47214 check if we are already in config by peeking the prompt
140 0 0         if ($self->prompt_looks_like('configure')) {
141 0           $self->in_configure_mode(1);
142 0           return;
143             }
144              
145 0           $self->macro('begin_configure');
146              
147 0 0         $self->prompt_looks_like('configure')
148             or die 'should be in configure mode but prompt does not match';
149 0           $self->in_configure_mode(1);
150             }
151              
152             sub end_configure {
153 0     0 0   my $self = shift;
154              
155 0 0         return unless $self->do_configure_mode;
156 0 0         return unless $self->in_configure_mode;
157              
158 0           $self->macro('end_configure');
159              
160             # we didn't manage to escape configure mode (must be nested?)
161 0 0         if ($self->prompt_looks_like('configure')) {
162 0           my $caller3 = (caller(3))[3];
163              
164             # max out at three tries to exit configure mode
165 0 0 0       if ( $caller3 and $caller3 =~ m/end_configure$/ ) {
166 0           die 'failed to leave configure mode';
167             }
168             # try again to exit configure mode
169             else {
170 0           $self->end_configure;
171             }
172             }
173              
174             # return if recursively called
175 0           my $caller1 = (caller(1))[3];
176 0 0 0       if ( defined $caller1 and $caller1 =~ m/end_configure$/ ) {
177 0           return;
178             }
179              
180 0 0         not $self->prompt_looks_like('configure')
181             or die 'should have exited configure mode but prompt still matches';
182 0           $self->in_configure_mode(0);
183             }
184              
185             1;