lib/App/Tel.pm

filter: Statementstmt 68.2% 236 / 346 Branchbran 52.9% 71 / 134 Conditioncond 41.7% 28 / 67 MC/DCmcdc 15.7% 6 / 38 Subroutinesub 69.8% 37 / 53 Podpod 100% 20 / 20 totaltotal 60.4% 398 / 658 CC 102 SCAR 31.0
CC
102
Cov
61%
control_loop
82.3
login
65.8
password
32.8
CRAP 22.1
« blib/lib/App/Tel/Passwd/Pass.pm ↑ Summary lib/App/Tel/HostRange.pm »
1 package App::Tel;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Expect qw( exp_continue );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use POSIX qw(:sys_wait_h :unistd_h); # For WNOHANG
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use App::Tel::HostRange qw (check_hostrange);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use App::Tel::Passwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use App::Tel::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use App::Tel::Macro;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use App::Tel::Merge qw ( merge );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use App::Tel::Expect;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Time::HiRes qw ( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 App::Tel - A script for logging into devices
19  
20 =head1 VERSION
21  
22 0.201601
23  
24 =cut
25  
26 7 our $VERSION = '0.201601';
27  
28  
29 =head1 SYNOPSIS
30  
31 tel gw1-my-dev
32  
33 See the README and COMMANDS files for examples of usage and ways to extend the
34 application.
35  
36 =head1 AUTHOR
37  
38 Robert Drake, C<< <rdrake at cpan.org> >>
39  
40 =head1 COPYRIGHT & LICENSE
41  
42 Copyright 2006 Robert Drake, all rights reserved.
43  
44 This program is free software; you can redistribute it and/or modify it
45 under the same terms as Perl itself.
46  
47 =cut
48  
49 #### GLOBALS
50 # For reasons related to state I needed to make $_winch_it global
51 # because it needs to be written inside signals.
52 7 my $_winch_it=0;
53  
54 0 ▶sub _winch_handler {
Subroutine _winch_handler: not called CC=1 SCAR=6.9
55 0 $_winch_it=1;
56 }
57  
58 1 ▶sub _winch {
Subroutine _winch: called CC=2 SCAR=7.9
59 1 my $session = shift->session;
60 # these need to be wrapped in eval or you get Given filehandle is not a
61 # tty in clone_winsize_from if you call winch() under a scripted
62 # environment like rancid (or just under par, or anywhere there is no pty)
63 1 eval {
64 1 $session->slave->clone_winsize_from(\*STDIN);
65 0 ▶ kill WINCH => $session->pid if $session->pid;
Branch: 1 branch
BranchTrueFalseTotal
if $session->pid 0 0 0
66 };
67 1 $_winch_it=0;
68 1 $SIG{WINCH} = \&_winch_handler;
69 }
70  
71 =head1 METHODS
72  
73 =head2 new
74  
75 my $tel = App::Tel->new();
76  
77 Creates a new App::Tel object.
78  
79 =cut
80  
81 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
82 6 my ($class, %args) = @_;
83  
84 my $self = {
85 'stdin' => Expect->exp_init(\*STDIN),
86 'connected' => 0,
87 'enabled' => 0,
88 'title_stack' => 0,
89 'log_stdout' => 1,
90 'profile' => {},
91 'perl' => $args{perl} || '',
92 'opts' => $args{opts},
93 6 ▶ 'colors' => App::Tel::Color->new($args{opts}->{d}),
Condition100%2/2
$args{'perl'} || ''
l!l
15
MC/DC100%1/1
$args{'perl'} || ''
$args{'perl'}
Truth table100%2/2
A
A= $args{'perl'}
Aresult
11
00
94 };
95  
96 6 return bless($self, $class);
97 }
98  
99  
100 =head2 go
101  
102 $self->go('hostname');
103  
104 Handles all the routines of connecting, enable, looping and disconnection.
105 This is the method called by bin/tel for each host.
106  
107 =cut
108  
109 1 ▶sub go {
Subroutine go: called CC=4 SCAR=14.6
Pod documented
110 1 my ($self, $host) = @_;
111  
112 # default profile always loads before anything else. replace == 1
113 1 $self->profile('default', 1);
114 1 ▶ $self->profile($self->{opts}->{P}) if ($self->{opts}->{P});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"P"} 0 1 1
115 1 $self->hostname($host);
116 1 $self->login($self->hostname);
117 1 ▶ $self->profile($self->{opts}->{A}) if ($self->{opts}->{A});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"A"} 0 1 1
118 1 ▶ if ($self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connected) 1 0 1
119 1 $self->enable->logging->control_loop->disconnect;
120 }
121  
122 0 return $self;
123 }
124  
125 =head2 disconnect
126  
127 $self->disconnect($hard_close);
128  
129 Tears down the session gracefully and resets internal variables to their
130 default values. Useful if you want to connect to another host after the first
131 one.
132  
133 If you supply a true value, it will hard_close the session.
134  
135 =cut
136  
137 0 ▶sub disconnect {
Subroutine disconnect: not called CC=4 SCAR=30.0
Pod documented
138 0 my ($self, $hard) = @_;
139 0 $self->{profile} = {};
140 0 ▶ $self->{timeout} = $self->{opts}->{t} ? $self->{opts}->{t} : 90;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'opts'}{'t'} ? : 0 0 0
141 0 $self->{banners} = undef;
142 0 $self->{methods} = ();
143 0 $self->connected(0);
144 0 $self->{colors}=App::Tel::Color->new($self->{opts}->{d});
145 0 $self->{enabled}=0;
146  
147 0 ▶ if ($self->{title_stack} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"title_stack"} > 0) 0 0 0
148 0 $self->{title_stack}--;
149 0 print "\e[23t";
150 }
151  
152 0 ▶ if ($hard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hard) { } 0 0 0
153 0 $self->session->hard_close();
154 } else {
155 0 $self->session->soft_close();
156 }
157 0 return $self;
158 }
159  
160 =head2 send
161  
162 $self->send("text\r");
163  
164 Wrapper for Expect's send() method.
165  
166 =cut
167  
168 4 ▶sub send {
Subroutine send: called CC=1 SCAR=0.0
Pod documented
169 4 return shift->session->send(@_);
170 }
171  
172 =head2 expect
173  
174 $self->expect("text");
175  
176 Wrapper for Expect's expect() method.
177  
178 =cut
179  
180 2 ▶sub expect {
Subroutine expect: called CC=1 SCAR=0.0
Pod documented
181 2 return shift->session->expect(@_);
182 }
183  
184 =head2 load_config
185  
186 Loads the config from /etc/telrc, /usr/local/etc/telrc, $ENV{HOME}/.telrc2, or
187 it can be appended to by using the environment variable TELRC, or overridden
188 by calling load_config with an argument:
189  
190 $self->load_config('/home/user/.my_custom_override');
191  
192 =cut
193  
194 4 ▶sub load_config {
Subroutine load_config: called CC=7 SCAR=19.5
Pod documented
195 4 my $self = shift;
196 4 ▶ my $xdg = $ENV{XDG_CONFIG_HOME} || "$ENV{HOME}/.config/";
Condition50%1/2
$ENV{'XDG_CONFIG_HOME'} || "$ENV{'HOME'}/.config/"
l!l
04
MC/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || "$ENV{'HOME'}/.config/"
$ENV{'XDG_CONFIG_HOME'}
Truth table50%1/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
197 4 my @configs = @_;
198 4 ▶ @configs = ( "/etc/telrc", "/usr/local/etc/telrc", "$ENV{HOME}/.telrc2", "$xdg/telrc") if (!@configs);
Branch: 1 branch
BranchTrueFalseTotal
unless @configs 0 4 4
199 4 ▶ push(@configs, $ENV{TELRC}) if (defined($ENV{TELRC}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TELRC"} 0 4 4
200 4 our $telrc;
201 4 my $config;
202  
203 4 foreach my $conf (@configs) {
204 4 ▶ if (-r $conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $conf) 3 1 4
205 3 require $conf;
206 3 push(@{$config->{'telrc_file'}}, $conf);
207 3 $config = merge($config, $telrc);
208 }
209 }
210  
211 4 ▶ if (!defined($config->{'telrc_file'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"telrc_file"}) 1 3 4
212 1 warn "No configuration files loaded. You may need to run mktelrc.";
213 }
214  
215 # load global syntax highlighting things if found
216 4 $self->{colors}->load_syntax($config->{syntax});
217 4 $self->{config} = $config;
218 4 return $self;
219 }
220  
221 =head2 hostname
222  
223 $hostname = $self->hostname("hostname");
224  
225 Called to parse the hostname provided by the user when first making a
226 connection. If the hostname has special attributes like a port designation,
227 it's parsed here.
228  
229 This also looks up the profile for the hostname to see if it needs to be
230 translated because of an alias. The final hostname is stored and returned.
231  
232  
233 =cut
234  
235 14 ▶sub hostname {
Subroutine hostname: called CC=13 SCAR=25.6
Pod documented
236 14 my ($self, $hostname) = @_;
237  
238 14 ▶ if (!defined($hostname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hostname) 2 12 14
239 2 return $self->{hostname};
240 }
241  
242 12 $hostname =~ s#/tftpboot/##;
243 12 $hostname =~ s/-confg//;
244  
245 12 ▶ if ($hostname =~ qr@(ssh|telnet)://\[([a-zA-Z0-9\-\.\:]+)\](?::(\d+))?@) {
Branch: 4 branches
BranchTrueFalseTotal
if ($hostname =~ qr"(ssh|telnet)://\[([a-zA-Z0-9\-\.\:]+)\](?::(\d+))?") { } 2 10 12
elsif ($hostname =~ qr"(ssh|telnet)://([a-zA-Z0-9\-\.]+)(?::(\d+))?") { } 2 8 10
elsif ($hostname =~ /\[(\S+)\](?::(\d+))?/) { } 2 6 8
elsif ($hostname =~ tr/:// < 2 and $hostname =~ /(\S+):(\d+)$/) { } 1 6 7
246 2 ▶ $self->{port} = $3 if ($3);
Branch: 1 branch
BranchTrueFalseTotal
if $3 1 1 2
247 2 $self->methods($1);
248 2 $hostname = $2;
249 } elsif ($hostname =~ qr@(ssh|telnet)://([a-zA-Z0-9\-\.]+)(?::(\d+))?@) {
250 2 ▶ $self->{port} = $3 if ($3);
Branch: 1 branch
BranchTrueFalseTotal
if $3 1 1 2
251 2 $self->methods($1);
252 2 $hostname = $2;
253 } elsif ($hostname =~ /\[(\S+)\](?::(\d+))?/) {
254 2 ▶ $self->{port} = $2 if ($2);
Branch: 1 branch
BranchTrueFalseTotal
if $2 1 1 2
255 2 ▶ $self->methods('telnet') if ($2);
Branch: 1 branch
BranchTrueFalseTotal
if $2 1 1 2
256 2 $hostname = $1;
257 ▶ } elsif (($hostname =~ tr/://) < 2 && $hostname =~ /(\S+):(\d+)$/) {
Condition100%3/3
$hostname =~ tr/:// < 2 and $hostname =~ /(\S+):(\d+)$/
!ll&&!rl&&r
141
MC/DC100%2/2
$hostname =~ tr/:// < 2 and $hostname =~ /(\S+):(\d+)$/
$hostname =~ tr/:// < 2 $hostname =~ /(\S+):(\d+)$/
Truth table100%3/3
A and B
A= $hostname =~ tr/:// < 2B= $hostname =~ /(\S+):(\d+)$/
ABresult
0X0
100
111
258 1 $self->{port} = $2;
259 1 $self->methods('telnet');
260 1 $hostname = $1;
261 }
262  
263 # load profile based on the hostname
264 12 $self->rtr_find($hostname);
265  
266 # if rtr->hostname is defined as code then call it to determine the real hostname
267 # if it's not then treat it as an alias.
268 12 ▶ if (defined($self->profile->{hostname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->profile->{"hostname"}) 2 10 12
269 2 ▶ if (ref($self->profile->{hostname}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->profile->{'hostname'} eq 'CODE') { } 1 1 2
270 1 $hostname = &{$self->profile->{hostname}}($hostname);
271 } else {
272 1 $hostname = $self->profile->{hostname};
273 }
274 }
275 12 $self->{hostname} = $hostname;
276 12 return $self->{hostname};
277 }
278  
279 =head2 methods
280  
281 $self->methods();
282  
283 This is used to determine the method used to connect to the remote device.
284 Generally, the CLI argument -m has the highest priority. The uri should be
285 second, profiles third, and the defaults would be last. If called without
286 arguments it will return whatever the currently decided method array is. If
287 called with an argument that will be set as the new method array.
288  
289 If you call it multiple times it won't change right now. I may need to
290 rethink this later but right now it works with the way the program flows.
291 $self->disconnect removes all methods so connecting to another router will run
292 this again.
293  
294 $self->methods('ssh', 'telnet');
295  
296 =cut
297  
298 11 ▶sub methods {
Subroutine methods: called CC=5 SCAR=16.1
Pod documented
299 11 my $self = shift;
300  
301 11 ▶ if (@_) {
Branch: 4 branches
BranchTrueFalseTotal
if (@_) { } 7 4 11
elsif (defined $self->{'methods'}) { } 1 3 4
elsif ($self->{'opts'}{'m'}) { } 1 2 3
elsif (defined $self->{'profile'}{'method'}) { } 1 1 2
302 7 @{$self->{methods}} = @_;
303 } elsif (defined($self->{methods})) {
304 1 return $self->{methods};
305 } elsif ($self->{opts}->{m}) {
306 1 $self->{methods} = [ $self->{opts}->{m} ];
307 } elsif (defined($self->{'profile'}->{method})) {
308 1 @{$self->{methods}} = split(/,/, $self->{'profile'}->{method});
309 } else {
310 1 $self->{methods} = [ 'ssh', 'telnet' ];
311 }
312  
313 10 return $self->{methods};
314 }
315  
316 # this is overridable in the individual test file to connect a special
317 # way if the method is listed as "test"
318 0 ▶sub _test_connect { undef }
Subroutine _test_connect: not called CC=1 SCAR=6.9
319  
320 1 ▶sub _banners {
Subroutine _banners: called CC=3 SCAR=12.7
321 1 my $self = shift;
322 1 ▶ return $self->{banners} if ($self->{banners});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"banners"} 0 1 1
323 1 my $config = $self->{'config'};
324  
325 # if there are no banners then we want to return an empty list
326 1 $self->{banners} = [];
327 1 ▶ while (my ($regex, $profile) = each %{$config->{banners}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$config{"banners"};}) 0 1 1
328 0 ▶ push @{$self->{banners}}, [ $regex, sub { $self->profile($profile); exp_continue; } ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
329 }
330 1 return $self->{banners};
331 }
332  
333 =head2 rtr_find
334  
335 my $profile = $self->rtr_find($regex);
336  
337 Find the router by hostname/regex and load the config associated with it.
338 Load a profile for it if there is one.
339  
340 If none of the router lines match then this does not merge another profile,
341 but will return the existing profile. That may not be what you want in all
342 cases. The workaround for it would be to clear profiles before calling this
343 with $self->profile('default',1);
344  
345 I'm trying to decide if there should be a better way to do this.
346  
347 =cut
348  
349 12 ▶sub rtr_find {
Subroutine rtr_find: called CC=5 SCAR=16.3
Pod documented
350 12 my ($self, $host) = @_;
351 12 my $profile = $self->{'profile'};
352 12 my $config = $self->{'config'};
353  
354 12 foreach my $h (@{$config->{rtr}}) {
355 4 my $h2 = $h->{regex};
356 4 ▶ if ($host =~ /$h2/i || check_hostrange($h2, $host)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /$h2/i or check_hostrange($h2, $host)) 3 1 4
Condition66%2/3
$host =~ /$h2/i or check_hostrange($h2, $host)
l!l&&r!l&&!r
301
MC/DC50%1/2
$host =~ /$h2/i or check_hostrange($h2, $host)
$host =~ /$h2/i check_hostrange($h2, $host)
Truth table66%2/3
A or B
A= $host =~ /$h2/iB= check_hostrange($h2, $host)
ABresult
1X1
011
000
357 3 $profile=merge($profile, $h);
358 3 last;
359 }
360 }
361 12 $self->{'profile'}=$profile;
362  
363 # if the host specified a profile to load then load it here
364 12 ▶ if (defined($profile->{profile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $profile->{"profile"}) 0 12 12
365 0 $self->profile($profile->{profile});
366 }
367  
368 12 return $self;
369 }
370  
371 =head2 profile
372  
373 $profile = $self->profile;
374 $profile = $self->profile('a_example_profile', $replace);
375  
376 Called without arguments this will return the current profile. If called with
377 a profile name it will load that new profile on top of whatever profile
378 currently exists. You can set the second argument to true if you want to
379 replace the current profile with the new one.
380  
381 =cut
382  
383 22 ▶sub profile {
Subroutine profile: called CC=8 SCAR=22.3
Pod documented
384 22 my ($self, $profile_arg, $replace) = @_;
385 22 my $stdin = $self->{'stdin'};
386 22 my $profile = $self->{'profile'};
387  
388 22 ▶ return $profile if (!defined($profile_arg));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $profile_arg 20 2 22
389  
390 2 ▶ if ($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) 2 0 2
391 # wipe out the old profile if we're replacing it.
392 2 $profile = {};
393 }
394  
395 2 foreach(split(/\+/, $profile_arg)) {
396 2 $profile = merge($profile, $self->{'config'}->{profile}{$_});
397  
398 # load handlers for profile
399 2 ▶ if ($profile->{handlers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile->{"handlers"}) 0 2 2
400 0 foreach my $v (keys %{$profile->{handlers}}) {
401 0 $stdin->set_seq($v, $profile->{handlers}{$v}, [ \$self ]);
402 }
403 }
404 # load syntax highlight
405 2 $self->{colors}->load_syntax($profile->{syntax});
406 2 $profile->{profile_name}=$_;
407 }
408  
409 # add some sane defaults if the profile doesn't have them
410 2 ▶ $profile->{'user'} ||= $ENV{'USER'} || $ENV{'LOGNAME'};
Condition50%3/6
$profile->{"user"} ||= $ENV{"USER"} || $ENV{"LOGNAME"}
l!l&&r!l&&!r
101
$ENV{'USER'} || $ENV{'LOGNAME'}
l!l&&r!l&&!r
001
MC/DC0%0/3
$profile->{"user"} ||= $ENV{"USER"} || $ENV{"LOGNAME"}
$profile->{"user"} $ENV{'USER'} $ENV{'LOGNAME'}
Truth table0%0/4
A ||= B || C
A= $profile->{"user"}B= $ENV{'USER'}C= $ENV{'LOGNAME'}
ABCresult
1XX1
01X1
0011
0000
411 # need to warn if user is still not defined?
412 2 $self->{'profile'}=$profile;
413 2 return $profile;
414 }
415  
416 0 ▶sub _stty_rows {
Subroutine _stty_rows: not called CC=2 SCAR=17.9
417 0 my $new_rows = shift;
418 0 eval {
419 0 Module::Load::load Term::ReadKey;
420 0 my ($columns, undef, $xpix, $ypix) = GetTerminalSize(\*STDOUT);
421 0 SetTerminalSize($columns, $new_rows, $xpix, $ypix, \*STDOUT);
422 };
423  
424 0 ▶ warn $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
425 }
426  
427 =head2 password
428  
429 my $password = $self->password;
430  
431 This pulls the password from the config. If the password is blank it checks
432 to see if you have a password manager, then tries to load the password from
433 there. It then tries to use an OS keyring.
434  
435 By default it will pull the regular password. To pull the enable password
436 you can call it with $self->password('enable');
437  
438 =cut
439  
440 2 ▶sub password {
Subroutine password: called CC=10 SCAR=34.9
Pod documented
441 2 my ($self, $type) = @_;
442 2 my $profile = $self->profile;
443 2 my $router = $self->{hostname};
444  
445 2 ▶ $type ||= 'password';
Condition100%2/2
$type ||= "password"
l!l
11
MC/DC100%1/1
$type ||= "password"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
446  
447 2 ▶ warn "Unknown password type $type" if ($type ne 'password' && $type ne 'enable');
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "password" and $type ne "enable" 0 2 2
Condition66%2/3
$type ne "password" and $type ne "enable"
!ll&&!rl&&r
110
MC/DC0%0/2
$type ne "password" and $type ne "enable"
$type ne "password" $type ne "enable"
Truth table66%2/3
A and B
A= $type ne "password"B= $type ne "enable"
ABresult
0X0
100
111
448  
449 2 ▶ if (defined($profile->{$type}) && $profile->{$type} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $profile->{$type} and $profile->{$type} ne "") 2 0 2
Condition33%1/3
defined $profile->{$type} and $profile->{$type} ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $profile->{$type} and $profile->{$type} ne ""
defined $profile->{$type} $profile->{$type} ne ""
Truth table33%1/3
A and B
A= defined $profile->{$type}B= $profile->{$type} ne ""
ABresult
0X0
100
111
450 2 return $profile->{$type};
451 }
452  
453 # if enable is blank but password has something then use the same password
454 # for enable.
455 0 ▶ if ($type eq 'enable' and $profile->{$type} eq '' && $profile->{'password'} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enable" and ($profile->{$type} eq "" and $profile->{"password"} ne "")) 0 0 0
Condition0%0/6
$type eq "enable" and ($profile->{$type} eq "" and $profile->{"password"} ne "")
!ll&&!rl&&r
000
$profile->{$type} eq "" and $profile->{"password"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
$type eq "enable" and ($profile->{$type} eq "" and $profile->{"password"} ne "")
$type eq "enable" $profile->{$type} eq "" $profile->{"password"} ne ""
Truth table0%0/4
A and B and C
A= $type eq "enable"B= $profile->{$type} eq ""C= $profile->{"password"} ne ""
ABCresult
0XX0
10X0
1100
1111
456 0 return $profile->{'password'};
457 }
458  
459 # if we get here, the password is blank so try other means
460 0 my $pass = App::Tel::Passwd::load_from_profile($profile);
461 0 ▶ if ($pass ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass ne "") 0 0 0
462 0 return $pass;
463 }
464  
465 # I was wondering how to decide what to prompt for, but I think it should
466 # be whichever profile loaded. So maybe we check for hostname and check
467 # for profile name and then save as profile name. If they want to be
468 # explicit they should specify the password format as KEYRING or
469 # something.. I dunno.
470 0 App::Tel::Passwd::keyring($profile->{user}, $profile->{profile_name}, $profile->{profile_name});
471  
472 # if they make it here and still don't have a password then none was
473 # defined anywhere and we probably should prompt for one. Consider
474 # turning off echo then normal read.
475 0 return App::Tel::Passwd::input_password($router);
476 }
477  
478 =head2 session
479  
480 my $session = $self->session;
481  
482 Expect won't let you reuse a connection that has spawned a process, so you
483 can call this with an argument to reset the session. If called without an
484 argument it will return the current session (If it exists) or create a new
485 session.
486  
487 =cut
488  
489 9 ▶sub session {
Subroutine session: called CC=5 SCAR=16.6
Pod documented
490 9 ▶ return $_[0]->{'session'} if (!$_[1] && defined($_[0]->{'session'}));
Branch: 1 branch
BranchTrueFalseTotal
if not $_[1] and defined $_[0]{"session"} 8 1 9
Condition66%2/3
not $_[1] and defined $_[0]{"session"}
!ll&&!rl&&r
108
MC/DC50%1/2
not $_[1] and defined $_[0]{"session"}
not $_[1] defined $_[0]{"session"}
Truth table66%2/3
not(A) and B
A= not $_[1]B= defined $_[0]{"session"}
ABresult
0X0
100
111
491  
492 1 my $self = shift;
493 1 my $session = $self->{'session'};
494  
495 1 ▶ $session->soft_close() if ($session && $session->pid());
Branch: 1 branch
BranchTrueFalseTotal
if $session and $session->pid 0 1 1
Condition33%1/3
$session and $session->pid
!ll&&!rl&&r
100
MC/DC0%0/2
$session and $session->pid
$session $session->pid
Truth table33%1/3
A and B
A= $sessionB= $session->pid
ABresult
0X0
100
111
496 1 $session = Expect->new;
497  
498 # install sig handler for window size change
499 1 $SIG{WINCH} = \&_winch_handler;
500  
501  
502 1 $session->log_stdout(1);
503 1 $self->{'session'} = $session;
504 1 return $session;
505 }
506  
507 =head2 connect
508  
509 my $session = $self->connect('routername');
510  
511 This sets up the session. If there already is a session open it closes and opens a new one.
512  
513 =cut
514  
515 1 ▶sub connect {
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
516 1 my ($self, @arguments) = @_;
517  
518 1 $self->connected(0);
519 1 my $session = $self->session(1);
520 1 $session->spawn(@arguments);
521 1 return $session;
522 }
523  
524 =head2 connected
525  
526 if ($self->connected);
527 or
528 $self->connected(1);
529  
530 Returns connection status, or sets the status to whatever value is supplied to
531 the method.
532  
533 Note: This isn't the session state, but an indicator that our session has
534 gotten through the login stage and is now waiting for input. i.e., the router
535 is at a prompt of some kind.
536  
537 =cut
538  
539 4 ▶sub connected {
Subroutine connected: called CC=2 SCAR=6.9
Pod documented
540 4 my ($self, $status) = @_;
541 4 ▶ if (defined($status)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status) 2 2 4
542 2 $self->{connected}=$status;
543 }
544  
545 4 return $self->{connected};
546 }
547  
548 =head2 enable
549  
550 $self->enable;
551  
552 if enablecmd is set then this method attempts to enable the user. Returns
553 $self.
554  
555 =cut
556  
557 1 ▶sub enable {
Subroutine enable: called CC=4 SCAR=14.0
Pod documented
558 1 my $self = shift;
559 1 my $profile = $self->profile;
560  
561 1 ▶ if ($profile->{enablecmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile->{"enablecmd"}) 1 0 1
562 1 $self->send($profile->{enablecmd} . "\r");
563  
564 1 ▶ $profile->{ena_username_prompt} ||= qr/[Uu]ser[Nn]ame:|Login:/;
Condition50%1/2
$profile->{"ena_username_prompt"} ||= qr/[Uu]ser[Nn]ame:|Login:/
l!l
01
MC/DC0%0/1
$profile->{"ena_username_prompt"} ||= qr/[Uu]ser[Nn]ame:|Login:/
$profile->{"ena_username_prompt"}
Truth table50%1/2
A
A= $profile->{"ena_username_prompt"}
Aresult
11
00
565 1 ▶ $profile->{ena_password_prompt} ||= qr/[Pp]ass[Ww]ord/;
Condition50%1/2
$profile->{"ena_password_prompt"} ||= qr/[Pp]ass[Ww]ord/
l!l
01
MC/DC0%0/1
$profile->{"ena_password_prompt"} ||= qr/[Pp]ass[Ww]ord/
$profile->{"ena_password_prompt"}
Truth table50%1/2
A
A= $profile->{"ena_password_prompt"}
Aresult
11
00
566  
567 # we need to be able to handle routers that prompt for username and password
568 # need to check the results to see if enable succeeded
569 $self->expect($self->{timeout},
570 1 ▶ [ $profile->{ena_username_prompt} => sub { $self->send("$profile->{user}\r"); exp_continue; } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
571 1 ▶ [ $profile->{ena_password_prompt} => sub { $self->send($self->password('enable') . "\r"); } ]
Subroutine __ANON__: called CC=1 SCAR=0.0
572 );
573 }
574  
575 1 $self->{enabled}=1;
576 1 return $self;
577 }
578  
579 =head2 enabled
580  
581 my $enabled = $self->enabled;
582  
583 Check if enabled. This returns true if the $self->enable() method succeeds.
584  
585 =cut
586  
587 1 ▶sub enabled { $_[0]->{enabled} };
Subroutine enabled: called CC=1 SCAR=0.0
Pod documented
588  
589 =head2 login
590  
591 $self->login("hostname");
592  
593 Cycles through the connection methods trying each in the order specified by
594 the profile until we successfully connect to the host. Returns $self.
595  
596 =cut
597  
598 1 ▶sub login {
Subroutine login: called CC=20 SCAR=41.9
Pod documented
599 1 my ($self, $hostname) = @_;
600  
601 # dumb stuff to alias $rtr to the contents of $self->{'profile'}
602 # needed because we can reload the profile inside the expect loop
603 # and can't update the alias.
604 1 our $rtr;
605 1 *rtr = \$self->{'profile'};
606  
607 1 my $ssho = '-o StrictHostKeyChecking=no';
608 1 ▶ if (defined($rtr->{sshoptions}) && scalar $rtr->{sshoptions} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Tel::rtr->{"sshoptions"} and scalar $App::Tel::rtr->{"sshoptions"} > 0) 1 0 1
Condition33%1/3
defined $App::Tel::rtr->{"sshoptions"} and scalar $App::Tel::rtr->{"sshoptions"} > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $App::Tel::rtr->{"sshoptions"} and scalar $App::Tel::rtr->{"sshoptions"} > 0
defined $App::Tel::rtr->{"sshoptions"} scalar $App::Tel::rtr->{"sshoptions"} > 0
Truth table33%1/3
A and B
A= defined $App::Tel::rtr->{"sshoptions"}B= scalar $App::Tel::rtr->{"sshoptions"} > 0
ABresult
0X0
100
111
609 1 $ssho = '-o '. join(' -o ', @{$rtr->{sshoptions}});
610 }
611 1 ▶ $ssho .= $rtr->{ciphertype} ? " -c $rtr->{ciphertype}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$App::Tel::rtr->{'ciphertype'} ? : 0 1 1
612  
613 # because we use last METHOD; in anonymous subs this suppresses the
614 # warning of "exiting subroutine via last;"
615 7 ▶ no warnings 'exiting';
Subroutine BEGIN: called CC=1 SCAR=0.0
616 # handle MOTD profile loading, and other things parsed from the config
617 1 my @dynamic;
618 1 ▶ if (defined($rtr->{prompt})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Tel::rtr->{"prompt"}) 0 1 1
619 0 ▶ push @dynamic, [ qr/$rtr->{prompt}/, sub { $self->connected(1); last METHOD; } ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
620 }
621  
622 # handle prompts in foreign languages or other things we didn't think of
623 1 ▶ $rtr->{username_prompt} ||= qr/[Uu]ser[Nn]ame:|[Ll]ogin:/;
Condition50%1/2
$App::Tel::rtr->{"username_prompt"} ||= qr/[Uu]ser[Nn]ame:|[Ll]ogin:/
l!l
01
MC/DC0%0/1
$App::Tel::rtr->{"username_prompt"} ||= qr/[Uu]ser[Nn]ame:|[Ll]ogin:/
$App::Tel::rtr->{"username_prompt"}
Truth table50%1/2
A
A= $App::Tel::rtr->{"username_prompt"}
Aresult
11
00
624 1 ▶ $rtr->{password_prompt} ||= qr/[Pp]ass[Ww]ord/;
Condition50%1/2
$App::Tel::rtr->{"password_prompt"} ||= qr/[Pp]ass[Ww]ord/
l!l
01
MC/DC0%0/1
$App::Tel::rtr->{"password_prompt"} ||= qr/[Pp]ass[Ww]ord/
$App::Tel::rtr->{"password_prompt"}
Truth table50%1/2
A
A= $App::Tel::rtr->{"password_prompt"}
Aresult
11
00
625  
626 1 ▶ $self->{port} ||= $self->{opts}->{p} || $rtr->{port}; # get port from CLI or the profile
Condition33%2/6
$self->{"port"} ||= $self->{"opts"}{"p"} || $App::Tel::rtr->{"port"}
l!l&&r!l&&!r
001
$self->{'opts'}{'p'} || $App::Tel::rtr->{'port'}
l!l&&r!l&&!r
001
MC/DC0%0/3
$self->{"port"} ||= $self->{"opts"}{"p"} || $App::Tel::rtr->{"port"}
$self->{"port"} $self->{'opts'}{'p'} $App::Tel::rtr->{'port'}
Truth table0%0/4
A ||= B || C
A= $self->{"port"}B= $self->{'opts'}{'p'}C= $App::Tel::rtr->{'port'}
ABCresult
1XX1
01X1
0011
0000
627 # if it's not set in the profile or CLI above, it gets set in the
628 # method below, but needs to be reset on each loop to change from
629 # telnet to ssh defaults
630  
631 1 my $family = '';
632 1 ▶ $family = '-4' if ($self->{opts}->{4});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{4} 0 1 1
633 1 ▶ $family = '-6' if ($self->{opts}->{6});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{6} 0 1 1
634  
635 1 METHOD: for (@{$self->methods}) {
636 1 my $p = $self->{port};
637 0 ▶ if ($_ eq 'ssh') { $p ||= 22; $self->connect("ssh $family -p $p -l $rtr->{user} $ssho $hostname"); }
Branch: 3 branches
BranchTrueFalseTotal
if ($_ eq 'ssh') { } 0 1 1
elsif ($_ eq 'telnet') { } 0 1 1
elsif ($_ eq 'test') { } 1 0 1
Condition0%0/2
$p ||= 22
l!l
00
MC/DC0%0/1
$p ||= 22
$p
Truth table0%0/2
A
A= $p
Aresult
11
00
638 0 ▶ elsif ($_ eq 'telnet') { $p ||= ''; $self->connect("telnet $family $hostname $p"); }
Condition0%0/2
$p ||= ""
l!l
00
MC/DC0%0/1
$p ||= ""
$p
Truth table0%0/2
A
A= $p
Aresult
11
00
639 1 elsif ($_ eq 'test') { $self->_test_connect($hostname); }
640 0 else { die "No program defined for method $_\n"; }
641  
642 # suppress stdout if needed
643 1 $self->session->log_stdout($self->{log_stdout});
644  
645 # need to make this optional
646 # also need to make it display whatever the user cares about.
647 1 print "\e[22t\033]0;$_ $hostname\007";
648 1 $self->{title_stack}++;
649 0 ▶ $SIG{INT} = sub { for (1..$self->{title_stack}) { print "\e[23t"; } $self->{title_stack}=0; };
Subroutine __ANON__: not called CC=2 SCAR=13.0
650  
651 $self->expect($self->{timeout},
652 1 @{$self->_banners},
653 @dynamic,
654 0 ▶ [ $rtr->{username_prompt} => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
655 0 $self->send("$rtr->{user}\r");
656 0 exp_continue;
657 } ],
658 1 ▶ [ $rtr->{password_prompt} => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
659 1 $self->send($self->password() ."\r");
660 1 $self->connected(1);
661 1 last METHOD;
662 } ],
663 [ qr/Name or service not known|hostname nor servname provided, or not known|could not resolve / => sub
664 0 ▶ {
Subroutine __ANON__: not called CC=3 SCAR=24.8
665 # if host lookup fails then check to see if there is an alternate method defined
666 0 ▶ if ($rtr->{hostsearch} && !$rtr->{hostsearched}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Tel::rtr->{'hostsearch'} and not $App::Tel::rtr->{'hostsearched'}) { } 0 0 0
Condition0%0/3
$App::Tel::rtr->{"hostsearch"} and not $App::Tel::rtr->{"hostsearched"}
!ll&&!rl&&r
000
MC/DC0%0/2
$App::Tel::rtr->{"hostsearch"} and not $App::Tel::rtr->{"hostsearched"}
$App::Tel::rtr->{"hostsearch"} not $App::Tel::rtr->{"hostsearched"}
Truth table0%0/3
A and not(B)
A= $App::Tel::rtr->{"hostsearch"}B= not $App::Tel::rtr->{"hostsearched"}
ABresult
0X0
100
111
667 0 $hostname = &{$rtr->{hostsearch}}($hostname);
668 0 $rtr->{hostsearched}=1;
669 0 redo METHOD;
670 } else {
671 0 warn "unknown host: $hostname\n";
672 # skip to next host if this one doesn't exist
673 0 last METHOD;
674 }
675 }
676 ],
677 # almost never needed anymore. Some people might not want a
678 # fallback to des. If anyone does we need to make it optional
679 #[ qr/cipher type \S+ not supported/ => sub { $rtr->{ciphertype}="des"; redo METHOD; } ],
680  
681 # removing these 4, they should be handled by eof anyway
682 #[ qr/ssh_exchange_identification/ => sub { next METHOD; } ],
683 #[ qr/[Cc]onnection (refused|closed)/ => sub { next METHOD; } ],
684 #[ qr/key_verify failed/ => sub { next METHOD; } ],
685 #[ qr/Corrupted/ => sub { next METHOD; } ],
686 0 ▶ [ 'eof' => sub { next METHOD; } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
687 0 ▶ [ 'timeout' => sub { next METHOD; } ],
Subroutine __ANON__: not called CC=1 SCAR=1.2
688 );
689 }
690  
691 1 ▶ $rtr->{logoutcmd} ||= "logout";
Condition50%1/2
$App::Tel::rtr->{"logoutcmd"} ||= "logout"
l!l
01
MC/DC0%0/1
$App::Tel::rtr->{"logoutcmd"} ||= "logout"
$App::Tel::rtr->{"logoutcmd"}
Truth table50%1/2
A
A= $App::Tel::rtr->{"logoutcmd"}
Aresult
11
00
692 1 ▶ $rtr->{prompt} ||= '#';
Condition50%1/2
$App::Tel::rtr->{"prompt"} ||= "#"
l!l
01
MC/DC0%0/1
$App::Tel::rtr->{"prompt"} ||= "#"
$App::Tel::rtr->{"prompt"}
Truth table50%1/2
A
A= $App::Tel::rtr->{"prompt"}
Aresult
11
00
693  
694 1 ▶ warn "Connection to $hostname failed.\n" if !$self->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->connected 0 1 1
695 1 return $self;
696 }
697  
698 =head2 logging
699  
700 $self->logging('filename');
701  
702 Turns on logging for this session. If you specify a filename it will log to
703 /tmp/<filename>.log, otherwise it will use /tmp/<hostname>.log.
704  
705 =cut
706  
707 1 ▶sub logging {
Subroutine logging: called CC=4 SCAR=24.6
Pod documented
708 1 my $self = shift;
709 1 ▶ return $self if (!$self->{opts}->{l});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"l"} 1 0 1
710  
711 0 my $file = shift;
712 0 ▶ $file ||= $self->{hostname};
Condition0%0/3
$file ||= $self->{"hostname"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$file ||= $self->{"hostname"}
$file $self->{"hostname"}
Truth table0%0/3
A ||= B
A= $fileB= $self->{"hostname"}
ABresult
1X1
011
000
713 0 ▶ unlink ("/tmp/$file.log") if (-f "/tmp/$file.log");
Branch: 1 branch
BranchTrueFalseTotal
if -f "/tmp/$file.log" 0 0 0
714 0 $self->session->log_file("/tmp/$file.log");
715 0 return $self;
716 }
717  
718 =head2 run_commands
719  
720 $self->run_commands(@commands);
721  
722 TODO: Document this
723  
724  
725 =cut
726  
727 0 ▶sub run_commands {
Subroutine run_commands: not called CC=3 SCAR=24.8
Pod documented
728 0 my $self = shift;
729 0 my $opts = $self->{opts};
730  
731 0 foreach my $arg (@_) {
732 0 $arg =~ s/\\r/\r/g; # fix for reload\ry. I believe 'perldoc quotemeta' explains why this happens
733 0 chomp($arg);
734 0 $self->send("$arg\r");
735 $self->expect($self->{timeout},
736 0 ▶ [ $self->profile->{prompt} => sub { } ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
737 0 ▶ [ 'eof' => sub { die "EOF From host.\n"; } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
738 );
739 0 ▶ sleep($opts->{s}) if ($opts->{s});
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"s"} 0 0 0
740 }
741  
742 0 return $self;
743 }
744  
745 =head2 control_loop
746  
747 $self->control_loop();
748  
749 This is where control should be passed once the session is logged in. This
750 handles CLI commands passed via the -c option, or scripts executed with the -x
751 option. It also handles auto commands passed via either option -a on the
752 command line, or via autocmds in the profile.
753  
754 Calling this without any commands will just run interact()
755  
756 =cut
757  
758 1 ▶sub control_loop {
Subroutine control_loop: called CC=13 SCAR=44.1
Pod documented
759 1 my $self = shift;
760 1 my $profile = $self->profile;
761 1 my $opts = $self->{opts};
762 1 my $prompt = $profile->{prompt};
763 1 my $pagercmd = $profile->{pagercmd};
764 1 my $autocmds;
765 my @args;
766  
767 1 ▶ if ($opts->{a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'a'}) { } 1 0 1
768 1 $autocmds = [ split(/;/, $opts->{a}) ];
769 } else {
770 0 $autocmds = $profile->{autocmds};
771 }
772  
773 1 $self->_winch();
774  
775 # should -c override -x or be additive? or error if both are specified?
776  
777 1 ▶ @args = split(/;/, $opts->{c}) if ($opts->{c});
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"c"} 0 1 1
778  
779 1 ▶ if ($opts->{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"x"}) 0 1 1
780 0 for (@{$opts->{x}}) {
781 0 ▶ open(my $X, '<', $_) || die "Can't open file $_\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $X, "<", $_ or die "Can't open file $_\n" 0 0 0
782 0 push(@args,<$X>);
783 0 close $X;
784 }
785 }
786  
787 1 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 0 1 1
788 0 $self->expect($self->{timeout},'-re',$prompt);
789 0 ▶ if (ref($pagercmd) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pagercmd eq 'CODE') { } 0 0 0
elsif ($pagercmd) { } 0 0 0
790 0 $pagercmd->();
791 } elsif ($pagercmd) {
792 0 $self->run_commands("$pagercmd");
793 }
794 0 $self->run_commands(@args);
795 0 $self->send($profile->{logoutcmd} ."\r");
796 } else {
797 1 ▶ die 'STDIN Not a tty' if (!POSIX::isatty($self->{stdin}));
Branch: 1 branch
BranchTrueFalseTotal
unless POSIX::isatty($self->{"stdin"}) 1 0 1
798 0 ▶ if ($autocmds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autocmds) 0 0 0
799 0 $self->expect($self->{timeout},'-re',$prompt);
800 0 eval { $self->run_commands(@$autocmds); };
801 0 ▶ return $self if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
802 }
803  
804 0 ▶ my $color_cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
805 0 my ($session) = @_;
806 0 ▶ $self->_winch() if $_winch_it;
Branch: 1 branch
BranchTrueFalseTotal
if $_winch_it 0 0 0
807 0 ${*$session}{exp_Pty_Buffer} = $self->{colors}->colorize(${*$session}{exp_Pty_Buffer});
808 0 return 1;
809 0 };
810  
811 0 ▶ my $sleep_cb = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
812 0 sleep($self->{opts}->{S});
813 0 return 1;
814 0 };
815 0 $self->session->set_cb($self->session,$color_cb, [ \${$self->session} ]);
816 0 ▶ $self->{stdin}->set_seq("\r",$sleep_cb) if ($self->{opts}->{S});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"S"} 0 0 0
817 0 $self->session->interact($self->{stdin}, '\cD');
818 # q\b is to end anything that's at a More prompt or other dialog and
819 # get you back to the command prompt
820 # would be nice to detect if the session is closing down and not send
821 # this. I've tried checking for session and session->pid but they
822 # both still exist at this point so unless we wait for soft_close
823 # we're kinda stuck doing this.
824 0 $self->send("q\b" . $profile->{logoutcmd}. "\r");
825 }
826 0 return $self;
827 }
828  
829 7 1;
« blib/lib/App/Tel/Passwd/Pass.pm ↑ Summary lib/App/Tel/HostRange.pm »