Mnet/Expect/Cli/Ios.pm

filter: Statementstmt 80.4% 78 / 97 Branchbran 55.8% 19 / 34 Conditioncond 50.0% 8 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 70.3% 121 / 172 CC 26 SCAR 22.3
CC
26
Cov
71%
new
15.1
enable
10.3
__ANON__
9.3
CRAP 9.3
« Mnet/Stanza.pm ↑ Summary Mnet/Expect.pm »
1 package Mnet::Expect::Cli::Ios;
2  
3 =head1 NAME
4  
5 Mnet::Expect::Cli::Ios - Expect sessions to cisco ios devices
6  
7 =head1 SYNOPSIS
8  
9 # refer also to Mnet::Expect::Cli
10 use Mnet::Expect::Cli::Ios
11  
12 # Mnet::Expect::Cli has example with ssh host/key checks disabled
13 my $expect = Mnet::Expect::Cli::Ios->new({
14 spawn => "ssh user@1.2.3.4", password_in => 1
15 });
16  
17 # ensure we are in ios enable mode
18 $expect->enable() or die "enable failed";
19  
20 # get output from command on connected ios device
21 my $output = $expect->command("show version");
22  
23 # gracefully end/exit ios session
24 $expect->close;
25  
26 =head1 DESCRIPTION
27  
28 Mnet::Expect::Cli::Ios can be used to spawn L<Expect> processes which can be
29 used to programmatically control ssh or telnet command line user, enable, and
30 config sessions to cisco ios and similar devices, with support for L<Mnet>
31 options, logging, caching, and testing.
32  
33 Refer also to the L<Mnet::Expect> and L<Mnet::Expect::Cli> modules. The
34 methods in those modules are inherited by objects created with this module.
35  
36 =head1 METHODS
37  
38 Mnet::Expect::Cli::Ios implements the methods listed below.
39  
40 =cut
41  
42 # required modules
43 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 9 ▶use parent qw( Mnet::Expect::Cli );
Subroutine BEGIN: called CC=1 SCAR=0.0
46 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 9 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 9 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50  
51  
52 9 ▶sub new {
Subroutine new: called CC=14 SCAR=27.1
Pod documented
53  
54 =head2 new
55  
56 $expect = Mnet::Expect::Cli::Ios->new(\%opts)
57  
58 This method can be used to create new Mnet::Expect::Cli::Ios objects.
59  
60 The following input opts may be specified, in addition to options from
61 the L<Mnet::Expect::Cli> and L<Mnet::Expect> modules:
62  
63 enable set to password for enable mode during login
64 enable_in stderr prompt for stdin entry of enable if not set
65 enable_user default enable username set from username option
66 failed_re default recognizes lines starting w/ios % error char
67 paging_key default space key to send for ios pagination prompts
68 paging_re default recognizes ios pagination prompt --more--
69 prompt_re defaults for ios user or enable prompts, see below
70  
71 An error is issued if there are login problems.
72  
73 For example, the following call will start an ssh expect session to a device,
74 with a prompt for password input if necessary:
75  
76 # refer to SYNOPSIS example and Mnet::Expect::Cli for more info
77 my $expect = Mnet::Expect::Cli::Ios->new({
78 spawn => "ssh user@1.2.3.4", password_in => 1
79 });
80  
81 Set failed_re to detect failed logins faster, as long as there's no conflict
82 with text that appears in login banners. For example:
83  
84 (?i)(^\s*%|closed|error|denied|fail|incorrect|invalid|refused|sorry)
85  
86 A default prompt_re regex string for ios devices is used by this method to
87 detect normal user and enable mode command prompts:
88  
89 (^|\r|\n)\S+(>|#) ?(\r|\n|$)
90  
91 The default ios prompt_re will be adjusted after login to work in various
92 configuration modes where the prompt may be truncated with various suffixes
93 applied. This adjustment is disabled if prompt_re exists as an input option
94 to this function. Refer also to the L<Mnet::Expect::Cli> module new method
95 for more information on prompt_re.
96  
97 Refer to the L<Mnet::Expect::Cli> and L<Mnet::Expect> modules for more
98 information.
99  
100 =cut
101  
102 # read input class and options hash ref merged with cli options
103 9 ▶ my $class = shift // croak("missing class arg");
Condition50%1/2
shift() // croak("missing class arg")
l!l
90
MC/DC0%0/1
shift() // croak("missing class arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
104 9 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition50%1/2
shift() // {}
l!l
90
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
105  
106 # create log object with input opts hash, cli opts, and pragmas in effect
107 # ensures we can log correctly even if inherited object creation fails
108 9 my $log = Mnet::Log::Conditional->new($opts);
109 9 $log->debug("new starting");
110  
111 # create hash that will become new object from input opts hash
112 9 my $self = $opts;
113  
114 # note default options for this class
115 # refer also to Mnet::Expect::Cli defaults, these are overlaid on top
116 # includes recognized cli opts and opts for this object
117 # update perldoc for this sub with changes
118 9 my $defaults = {
119 enable => undef,
120 enable_in => undef,
121 enable_user => undef,
122 # failed_re default undef to be safe, refer to perldoc for more info
123 # add ios err /^\s*%/ to failed_re values in Mnet::Expect::Cli
124 # failed_re also used in the enable method in this module
125 failed_re => undef,
126 paging_key => ' ',
127 paging_re => '--(M|m)ore--',
128 prompt_re => '(^|\r|\n)\S+(>|#) ?(\r|\n|$)', # ios user/enable modes
129 };
130  
131 # set prompt_truncate flag only if prompt_re was not set by caller
132 9 my $prompt_truncate = undef;
133 9 ▶ $prompt_truncate = 1 if not exists $opts->{prompt_re};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"prompt_re"} 9 0 9
134  
135 # update future object $self hash with default opts
136 9 foreach my $opt (sort keys %$defaults) {
137 63 ▶ $self->{$opt} = $defaults->{$opt} if not exists $self->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$opt} 45 18 63
138 }
139  
140 # debug set opts here, hide internal opts w/name starting w/underscore
141 9 foreach my $opt (sort keys %$self) {
142 189 ▶ if ($opt !~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt =~ /^_/) 189 0 189
143 189 my $value = Mnet::Dump::line($self->{$opt});
144 189 $log->debug("new opt $opt = $value");
145 }
146 }
147  
148 # call Mnet::Expect::Cli::new to create new object
149 9 $log->debug("new calling Mnet::Expect::Cli::new");
150 9 $self = Mnet::Expect::Cli::new($class, $self);
151  
152 # return undef if Mnet::Expect::Cli object could not be created
153 9 ▶ if (not $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 9 9
154 0 $log->debug("new Mnet::Expect::Cli object failed, returning undef");
155 0 return undef;
156 }
157  
158 # change detected prompt for configuration modes
159 # prompt_truncate is set only if prompt_re was not set by caller
160 # note that this cannot be done reliably with user supplied prompt_re
161 # regex here needs to match prompt_re changes in Mnet::Expect::Cli->new
162 # refer also to perldoc for this sub
163 9 ▶ if ($prompt_truncate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prompt_truncate) 9 0 9
164 9 $log->debug("new prompt_truncate adjusting default ios prompt");
165 9 my $prompt_re = $self->prompt_re;
166 9 ▶ if ($self->prompt_re =~ /^(\([^\)]+\))(\S+)((>|#)\s?\\r\?\$)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_re =~ /^(\([^\)]+\))(\S+)((>|#)\s?\\r\?\$)$/) 8 1 9
167 8 my ($beginning, $middle, $end) = ($1, $2, $3);
168 8 ▶ $middle = substr($middle, 0, 5).'\S+' if length($middle) > 5;
Branch: 1 branch
BranchTrueFalseTotal
if length $middle > 5 8 0 8
169 8 my $prompt_new = $beginning . $middle . $end;
170 8 $self->prompt_re($prompt_new);
171 }
172 }
173  
174 # change detected prompt to ensure it works in different ios modes
175 # need to work in user, enable, config, interface config, etc
176 # also needs to work if long hostname in prompt gets truncated
177 9 $self->debug("new updateing prompt for both enable and user modes");
178 9 my $prompt_re = $self->prompt_re;
179 9 $prompt_re =~ s/(>|#)/(>|#)/;
180 9 $self->prompt_re($prompt_re);
181  
182 # call enable method if enable or enable_in option is set
183 9 ▶ $self->enable if defined $self->{enable} or $self->{enable_in};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"enable"} or $self->{"enable_in"} 7 2 9
Condition66%2/3
defined $self->{"enable"} or $self->{"enable_in"}
l!l&&r!l&&!r
702
MC/DC50%1/2
defined $self->{"enable"} or $self->{"enable_in"}
defined $self->{"enable"} $self->{"enable_in"}
Truth table66%2/3
A or B
A= defined $self->{"enable"}B= $self->{"enable_in"}
ABresult
1X1
011
000
184  
185 # finished new method, return Mnet::Expect::Cli::Ios object
186 9 $self->debug("new finished, returning $self");
187 9 return $self;
188 }
189  
190  
191  
192 7 ▶sub enable {
Subroutine enable: called CC=6 SCAR=23.3
Pod documented
193  
194 =head2 enable
195  
196 $boolean = $expect->enable($password)
197  
198 Use this method to check if an ios device session is currently in enable mode,
199 and/or to enter enable mode on the device.
200  
201 The input password argument will be used if there is an enable password prompt,
202 otherwise the enable option set for the current object will be used, or the
203 user will be prompted if the enable_in option is set.
204  
205 A fatal error is issued if an enable password is required and none is set.
206  
207 A value of true is returned if the ios device is at an enable mode command
208 prompt, otherwise a value of false is returned.
209  
210 =cut
211  
212 # read input object, password, and username args
213 # set undefined enable password and username args from object option
214 7 ▶ my $self = shift or croak("missing self arg");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("missing self arg") 7 0 7
215 7 ▶ my $password = shift // $self->{enable};
Condition33%1/3
shift() // $self->{"enable"}
l!l&&r!l&&!r
070
MC/DC0%0/2
shift() // $self->{"enable"}
shift() $self->{"enable"}
Truth table33%1/3
A // B
A= shift()B= $self->{"enable"}
ABresult
1X1
011
000
216 7 ▶ my $username = shift // $self->{username};
Condition33%1/3
shift() // $self->{"username"}
l!l&&r!l&&!r
007
MC/DC0%0/2
shift() // $self->{"username"}
shift() $self->{"username"}
Truth table33%1/3
A // B
A= shift()B= $self->{"username"}
ABresult
1X1
011
000
217 7 $self->debug("enable starting");
218  
219 # send enable comand
220 # return output if we receive normal cisco enable# prompt
221 # return output if we receive an % ios error
222 # send username if prompted for user or username
223 # return output if replay option is set
224 # return output if enable_user or username is not set
225 # send password if prompted for password_re
226 # return output if replay option is set
227 # prompt stderr/stdin for undef password if enable_in is set
228 # _log_filter is used to keep password out of Mnet::Expect->log
229 # return output if we get anything that matches prompt_re
230 my $output = $self->command("enable", undef, [
231 '#' => undef,
232 '\%' => undef,
233 1 ▶ '(?i)\s*user(name)?:?\s*$' => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
234 1 my ($self, $output) = (shift, shift);
235 1 ▶ return undef if $self->{replay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"replay"} 0 1 1
236 1 ▶ if (defined $self->{enable_user}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'enable_user'}) { } 1 0 1
elsif (defined $self->{'username'}) { } 0 0 0
237 1 $self->debug("enable sending enable_user");
238 1 return "$self->{enable_user}\r";
239 } elsif (defined $self->{username}) {
240 0 $self->debug("enable sending username");
241 0 return "$self->{username}\r";
242 }
243 0 return undef;
244 },
245 5 ▶ $self->{password_re} => sub {
Subroutine __ANON__: called CC=4 SCAR=22.3
246 5 my ($self, $output) = (shift, shift);
247 5 ▶ return undef if $self->{replay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"replay"} 0 5 5
248 5 ▶ if (not defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password) 0 5 5
249 0 ▶ if ($self->{enable_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'enable_in'}) { } 0 0 0
250 0 $self->debug("enable enable_in prompt starting");
251 {
252 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 system("stty echo 2>/dev/null");
254 0 syswrite STDERR, "\n\n";
255 0 $self->fatal("interrupt signal received");
256 0 };
257 0 syswrite STDERR,
258 "\nEnter enable $self->expect->match: ";
259 0 system("stty -echo 2>/dev/null");
260 0 chomp($password = <STDIN>);
261 0 system("stty echo 2>/dev/null");
262 0 syswrite STDERR, "\n";
263 };
264 0 $self->debug("enable enable_in prompt finished");
265 } else {
266 0 $self->fatal("enable or enable_in required and not set");
267 }
268 }
269 5 $self->debug("enable sending enable password");
270 5 $self->{_log_filter} = $password;
271 5 return "$password\r";
272 },
273 $self->{prompt_re} => undef,
274 7 ]);
275  
276 # return true if we confirmed we are at an enable prompt
277 7 ▶ if (defined $output and $output =~ /#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output and $output =~ /#/) 6 1 7
Condition66%2/3
defined $output and $output =~ /#/
!ll&&!rl&&r
016
MC/DC50%1/2
defined $output and $output =~ /#/
defined $output $output =~ /#/
Truth table66%2/3
A and B
A= defined $outputB= $output =~ /#/
ABresult
0X0
100
111
278 6 $self->debug("enable finished, returning true");
279 6 return 1;
280 }
281  
282 # finished enable method, return true
283 1 $self->debug("enable finished, returning false");
284 1 return 0;
285 }
286  
287  
288  
289 9 ▶sub close {
Subroutine close: called CC=2 SCAR=7.0
Pod documented
290  
291 =head2 close
292  
293 $expect->close
294  
295 This method sends the end and exit ios commands before closing the current
296 expect session. Timeouts and disconnects are gracefully handled. Refer to the
297 close method in the L<Mnet::Expect::Cli> module for more information.
298  
299 =cut
300  
301 # read input object
302 9 ▶ my $self = shift or croak("missing self arg");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("missing self arg") 9 0 9
303 9 $self->debug("close starting");
304  
305 # send the end command, might be necessary on some platforms
306 # gracefully handle any timeout when sending this command
307 9 $self->command("end", undef, [ "" => undef ]);
308  
309 # call parent module close method
310 9 $self->SUPER::close("exit");
311  
312 # finished close method
313 9 $self->debug("close finished");
314 9 return;
315 }
316  
317  
318  
319 =head1 TESTING
320  
321 L<Mnet::Test> --record and --replay functionality are supported. Refer to the
322 TESTING perldoc section of L<Mnet::Expect::Cli> module for more information.
323  
324 =head1 SEE ALSO
325  
326 L<Expect>
327  
328 L<Mnet>
329  
330 L<Mnet::Expect>
331  
332 L<Mnet::Expect::Cli>
333  
334 L<Mnet::Log>
335  
336 L<Mnet::Opts::Cli>
337  
338 L<Mnet::Test>
339  
340 =cut
341  
342 # normal package return
343 9 1;
344  
« Mnet/Stanza.pm ↑ Summary Mnet/Expect.pm »