blib/lib/Net/SSH/Expect.pm

filter: Statementstmt 16.8% 43 / 256 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 31.1% 14 / 45 Podpod 91.6% 22 / 24 totaltotal 15.1% 79 / 520 CC 79 SCAR 46.9
CC
79
Cov
10%
new
462.0
waitfor
110.0
login
90.0
CRAP 109.3
↑ Summary
1 package Net::SSH::Expect;
2 1 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use fields qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
6 host user password port no_terminal escape_char ssh_option
7 raw_pty exp_internal exp_debug log_file log_stdout restart_timeout_upon_receive
8 timeout terminator expect debug next_line before match after binary
9 1 );
10 1 ▶use Expect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX qw(:signal_h WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 our $VERSION = '1.09';
15
16 # error contants
17 1 ▶use constant ILLEGAL_STATE => "IllegalState";
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant ILLEGAL_STATE_NO_SSH_CONNECTION => "IllegalState: you don't have a valid SSH connection to the server";
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant ILLEGAL_ARGUMENT => "IllegalArgument";
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant SSH_AUTHENTICATION_ERROR => "SSHAuthenticationError";
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant SSH_PROCESS_ERROR => "SSHProcessError";
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant SSH_CONNECTION_ERROR => "SSHConnectionError";
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant SSH_CONNECTION_ABORTED => "SSHConnectionAborted";
Subroutine BEGIN: called CC=1 SCAR=0.0
24
25 0 ▶sub new {
Subroutine new: not called CC=21 SCAR=61.4
Pod undocumented
26 0 my $type = shift;
27 0 my %args = @_;
28 0 ▶ my Net::SSH::Expect $self = fields::new(ref $type || $type);
Condition0%0/3
ref $type || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table0%0/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
29
30 # Options used to configure the SSH command
31 0 ▶ $self->{host} = $args{host}|| undef;
Condition0%0/2
$args{'host'} || undef
l!l
00
MC/DC0%0/1
$args{'host'} || undef
$args{'host'}
Truth table0%0/2
A
A= $args{'host'}
Aresult
11
00
32 0 ▶ $self->{user} = $args{user} || $ENV{'USER'};
Condition0%0/3
$args{'user'} || $ENV{'USER'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'user'} || $ENV{'USER'}
$args{'user'} $ENV{'USER'}
Truth table0%0/3
A || B
A= $args{'user'}B= $ENV{'USER'}
ABresult
1X1
011
000
33 0 ▶ $self->{password} = $args{password} || undef;
Condition0%0/2
$args{'password'} || undef
l!l
00
MC/DC0%0/1
$args{'password'} || undef
$args{'password'}
Truth table0%0/2
A
A= $args{'password'}
Aresult
11
00
34 0 ▶ $self->{port} = $args{port} || undef; # ssh -p
Condition0%0/2
$args{'port'} || undef
l!l
00
MC/DC0%0/1
$args{'port'} || undef
$args{'port'}
Truth table0%0/2
A
A= $args{'port'}
Aresult
11
00
35 0 ▶ $self->{no_terminal} = $args{no_terminal} || 0; # ssh -T
Condition0%0/2
$args{'no_terminal'} || 0
l!l
00
MC/DC0%0/1
$args{'no_terminal'} || 0
$args{'no_terminal'}
Truth table0%0/2
A
A= $args{'no_terminal'}
Aresult
11
00
36 0 ▶ $self->{escape_char} = $args{escape_char} || undef; # ssh -e
Condition0%0/2
$args{'escape_char'} || undef
l!l
00
MC/DC0%0/1
$args{'escape_char'} || undef
$args{'escape_char'}
Truth table0%0/2
A
A= $args{'escape_char'}
Aresult
11
00
37 0 ▶ $self->{ssh_option} = $args{ssh_option} || undef; # arbitrary ssh options
Condition0%0/2
$args{'ssh_option'} || undef
l!l
00
MC/DC0%0/1
$args{'ssh_option'} || undef
$args{'ssh_option'}
Truth table0%0/2
A
A= $args{'ssh_option'}
Aresult
11
00
38 0 ▶ $self->{binary} = $args{binary} || "ssh"; # path to SSH binary.
Condition0%0/2
$args{'binary'} || 'ssh'
l!l
00
MC/DC0%0/1
$args{'binary'} || 'ssh'
$args{'binary'}
Truth table0%0/2
A
A= $args{'binary'}
Aresult
11
00
39
40 # Options used to configure the Expect object
41 0 ▶ $self->{raw_pty} = $args{raw_pty} || 0;
Condition0%0/2
$args{'raw_pty'} || 0
l!l
00
MC/DC0%0/1
$args{'raw_pty'} || 0
$args{'raw_pty'}
Truth table0%0/2
A
A= $args{'raw_pty'}
Aresult
11
00
42 0 ▶ $self->{exp_internal} = $args{exp_internal} || 0;
Condition0%0/2
$args{'exp_internal'} || 0
l!l
00
MC/DC0%0/1
$args{'exp_internal'} || 0
$args{'exp_internal'}
Truth table0%0/2
A
A= $args{'exp_internal'}
Aresult
11
00
43 0 ▶ $self->{exp_debug} = $args{exp_debug} || 0;
Condition0%0/2
$args{'exp_debug'} || 0
l!l
00
MC/DC0%0/1
$args{'exp_debug'} || 0
$args{'exp_debug'}
Truth table0%0/2
A
A= $args{'exp_debug'}
Aresult
11
00
44 0 ▶ $self->{log_file} = $args{log_file} || undef;
Condition0%0/2
$args{'log_file'} || undef
l!l
00
MC/DC0%0/1
$args{'log_file'} || undef
$args{'log_file'}
Truth table0%0/2
A
A= $args{'log_file'}
Aresult
11
00
45 0 ▶ $self->{log_stdout} = $args{log_stdout} || 0;
Condition0%0/2
$args{'log_stdout'} || 0
l!l
00
MC/DC0%0/1
$args{'log_stdout'} || 0
$args{'log_stdout'}
Truth table0%0/2
A
A= $args{'log_stdout'}
Aresult
11
00
46 0 ▶ $self->{restart_timeout_upon_receive} = $args{restart_timeout_upon_receive} || 0;
Condition0%0/2
$args{'restart_timeout_upon_receive'} || 0
l!l
00
MC/DC0%0/1
$args{'restart_timeout_upon_receive'} || 0
$args{'restart_timeout_upon_receive'}
Truth table0%0/2
A
A= $args{'restart_timeout_upon_receive'}
Aresult
11
00
47
48 # Attributes for this module
49 0 ▶ $self->timeout(defined $args{timeout} ? $args{timeout} : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'timeout'} ? : 0 0 0
50 0 ▶ $self->{terminator} = $args{terminator} || "\n";
Condition0%0/2
$args{'terminator'} || "\n"
l!l
00
MC/DC0%0/1
$args{'terminator'} || "\n"
$args{'terminator'}
Truth table0%0/2
A
A= $args{'terminator'}
Aresult
11
00
51 0 $self->{next_line} = "";
52 0 $self->{expect} = undef; # this will hold the Expect instance
53 0 ▶ $self->{debug} = $args{debug} || 0;
Condition0%0/2
$args{'debug'} || 0
l!l
00
MC/DC0%0/1
$args{'debug'} || 0
$args{'debug'}
Truth table0%0/2
A
A= $args{'debug'}
Aresult
11
00
54 0 $self->{before} = "";
55 0 $self->{match} = "";
56 0 $self->{after} = "";
57
58 # validating the user input
59 0 foreach my $key (keys %args) {
60 0 ▶ if (! exists $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 0 0 0
61 0 croak ILLEGAL_ARGUMENT . " attribute '$key' is not a valid constructor argument.";
62 }
63 }
64
65 0 return $self;
66 }
67
68 # boolean run_ssh() - forks the ssh client process opening an ssh connection to the SSH server.
69 #
70 # This method has three roles:
71 # 1) Instantiate a new Expect object configuring it with all the defaults and user-defined
72 # settings.
73 # 2) Define the ssh command line using the defaults and user-defined settings
74 # 3) Fork the ssh process using the spawn() method of the Expect instance we created.
75 # The SSH connection is established on this step using the user account set in the 'user'
76 # constructor attribute. No password is sent here, that happens only in the login() method.
77 #
78 # This method is run internally by the login() method so you don't need to run it yourself
79 # in most of the cases. You'll run this method alone if you had set up public-key authentication
80 # between the ssh client and the ssh server. In this case you only need to call this method
81 # to have an authenticated ssh connection, you won't call login(). Note that when you
82 # use public-key authentication you won't need to set the 'password' constructor attribute
83 # but you still need to define the 'user' attribute.
84 # If you don't know how to setup public-key authentication there's a good guide at
85 # http://sial.org/howto/openssh/publickey-auth/
86 #
87 # returns:
88 # boolean: 1 if the ssh ran OK or 0 otherwise. In case of failures, use $! to do get info.
89 0 ▶sub run_ssh {
Subroutine run_ssh: not called CC=8 SCAR=42.8
Pod documented
90 0 my Net::SSH::Expect $self = shift;
91
92 0 my $user = $self->{user};
93 0 my $host = $self->{host};
94
95 0 ▶ croak(ILLEGAL_STATE . " field 'host' is not set.") unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 0 0
96 0 ▶ croak(ILLEGAL_STATE . " field 'user' is not set.") unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
97
98 0 my $log_file = $self->{log_file};
99 0 my $log_stdout = $self->{log_stdout};
100 0 my $exp_internal = $self->{exp_internal};
101 0 my $exp_debug = $self->{exp_debug};
102 0 my $no_terminal = $self->{no_terminal};
103 0 my $raw_pty = $self->{raw_pty};
104 0 my $escape_char = $self->{escape_char};
105 0 my $ssh_option = $self->{ssh_option};
106 0 my $port = $self->{port};
107 0 my $rtup = $self->{restart_timeout_upon_receive};
108
109 # Gather flags.
110 0 my $flags = "";
111 0 ▶ $flags .= $escape_char ? "-e '$escape_char' " : "-e none ";
Branch: 1 branch
BranchTrueFalseTotal
$escape_char ? : 0 0 0
112 0 ▶ $flags .= "-p $port " if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 0 0
113 0 ▶ $flags .= "-T " if $no_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $no_terminal 0 0 0
114 0 ▶ $flags .= $ssh_option if $ssh_option;
Branch: 1 branch
BranchTrueFalseTotal
if $ssh_option 0 0 0
115
116 # this sets the ssh command line
117 0 my $ssh_string = $self->{binary} . " $flags $user\@$host";
118
119 # creating the Expect object
120 0 my $exp = new Expect();
121
122 # saving this instance
123 0 $self->{expect} = $exp;
124
125 # configuring the expect object
126 0 $exp->log_stdout($log_stdout);
127 0 ▶ $exp->log_file($log_file, "w") if $log_file;
Branch: 1 branch
BranchTrueFalseTotal
if $log_file 0 0 0
128 0 $exp->exp_internal($exp_internal);
129 0 $exp->debug($exp_debug);
130 0 $exp->raw_pty($raw_pty);
131 0 $exp->restart_timeout_upon_receive($rtup);
132 0 my $success = $exp->spawn($ssh_string);
133
134 0 return (defined $success);
135 }
136
137 # string login ([$login_prompt, $password_prompt] [,$test_success]) - authenticates on the ssh server.
138 # This method responds to the authentication prompt sent by the SSH server.
139 # You can customize the "Login:" and "Password:" prompts that must be expected by passing their
140 # patterns as arguments to this method, although this method has default values that work to most
141 # SSH servers out there.
142 # It runs the run_ssh() method only if it wasn't run before(), but it'll die
143 # if run_ssh() returns false.
144 #
145 # param:
146 # $login_prompt: A pattern string used to match the "Login:" prompt. The default
147 # pattern is qr/ogin:\s*$/
148 #
149 # $password_prompt: A pattern string used to match the "Password:" prompt. The default
150 # pattern is qr/[Pp]assword.*?:|[Pp]assphrase.*?:/
151 #
152 # $test_success: 0 | 1. if 1, login will do an extra-test to verify if the password
153 # entered was accepted. The test consists in verifying if, after sending the password,
154 # the "Password" prompt shows up again what would indicate that the password was rejected.
155 # This test is disabled by default.
156 #
157 # OBS: the number of paramaters passed to this method will tell it what parameters are being passed:
158 # 0 parameters: login() : All the default values will be used.
159 # 1 parameter: login(1) : The $test_success parameter is set.
160 # 2 parameters: login("Login:", "Password:") : the $login_prompt and $password_prompt parameters are set.
161 # 3 parameters: login("Login:", "Password;", 1) : the three parameters received values in this order.
162 #
163 # returns:
164 # string: whatever the SSH server wrote in my input stream after loging in. This usually is some
165 # welcome message and/or the remote prompt. You could use this string to do your verification
166 # that the login was successful. The content returned is removed from the input stream.
167 # dies:
168 # IllegalState: if any of 'host' or 'user' or 'password' fields are unset.
169 # SSHProccessError: if run_ssh() failed to spawn the ssh process
170 # SSHConnectionError: if the connection failed for some reason, like invalid 'host' address or network problems.
171 0 ▶sub login {
Subroutine login: not called CC=9 SCAR=45.0
Pod documented
172 0 my Net::SSH::Expect $self = shift;
173
174 # setting the default values for the parameters
175 0 my ($login_prompt, $password_prompt, $test_success) = ( qr/ogin:\s*$/, qr/[Pp]assword.*?:|[Pp]assphrase.*?:/, 0);
176
177 # attributing the user defined values
178 0 ▶ if (@_ == 2 || @_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 or @_ == 3) 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
179 0 $login_prompt = shift;
180 0 $password_prompt = shift;
181 }
182 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
183 0 $test_success = shift;
184 }
185
186 0 my $user = $self->{user};
187 0 my $password = $self->{password};
188 0 my $timeout = $self->{timeout};
189 0 my $t = $self->{terminator};
190
191 0 ▶ croak(ILLEGAL_STATE . " field 'user' is not set.") unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
192 0 ▶ croak(ILLEGAL_STATE . " field 'password' is not set.") unless $password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 0 0 0
193
194 # spawns the ssh process if this wasn't done yet
195 0 ▶ if (! defined($self->{expect})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"expect"}) 0 0 0
196 0 ▶ $self->run_ssh() or croak SSH_PROCESS_ERROR . " Couldn't start ssh: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->run_ssh or croak('SSHProcessError' . " Couldn't start ssh: $!\n") 0 0 0
197 }
198
199 0 my $exp = $self->get_expect();
200
201 # loggin in
202 $self->_sec_expect($timeout,
203 0 ▶ [ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes$t"); exp_continue; } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 ▶ [ $password_prompt => sub { $exp->send("$password$t"); } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
205 0 ▶ [ $login_prompt => sub { $exp->send("$user$t"); exp_continue; } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
206 0 ▶ [ qr/REMOTE HOST IDEN/ => sub { print "FIX: .ssh/known_hosts\n"; exp_continue; } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 [ timeout => sub
208 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 croak SSH_AUTHENTICATION_ERROR . " Login timed out. " .
210 "The input stream currently has the contents bellow: " .
211 $self->peek();
212 }
213 0 ]
214 );
215
216 # verifying if we failed to logon
217 0 ▶ if ($test_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_success) 0 0 0
218 $self->_sec_expect($timeout,
219 [ $password_prompt =>
220 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 my $error = $self->peek();
222 0 croak(SSH_AUTHENTICATION_ERROR . " Error: Bad password [$error]");
223 }
224 0 ]
225 );
226 }
227
228 # swallows any output the server wrote to my input stream after loging in
229 0 return $self->read_all();
230 }
231
232
233
234 # boolean waitfor ($string [, $timeout, $match_type])
235 # This method reads until a pattern or string is found in the input stream.
236 # All the characters before and including the match are removed from the input stream.
237 #
238 # After waitfor returns, use the methods before(), match() and after() to get the data
239 # 'before the match', 'what matched', and 'after the match' respectively.
240 #
241 # If waitfor returns false, whatever content is on input stream can be accessed with
242 # before(). In this case before() will return the same content as peek().
243 #
244 # params:
245 # $string: a string to be matched. It can be a regular expression or a literal string
246 # anb its interpretation as one or other depends on $match_type. Default is
247 # 're', what treats $string as a regular expression.
248 #
249 # $timeout: the timeout in seconds while waiting for $string
250 #
251 # $match_type: match_type affects how $string will be matched:
252 # '-re': means $string is a regular expression.
253 # '-ex': means $string is an "exact match", i.e., will be matched literally.
254 #
255 # returns:
256 # boolean: 1 is returned if string was found, 0 otherwise. When the match fails
257 # waitfor() will only return after waiting $timeout seconds.
258 #
259 # dies:
260 # SSH_CONNECTION_ABORTED if EOF is found (error type 2)
261 # SSH_PROCESS_ERROR if the ssh process has died (error type 3)
262 # SSH_CONNECTION_ERROR if unknown error (type 4) is found
263 0 ▶sub waitfor {
Subroutine waitfor: not called CC=10 SCAR=47.0
Pod documented
264 0 my Net::SSH::Expect $self = shift;
265 0 my $pattern = shift;
266 0 ▶ my $timeout = @_ ? shift : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
267 0 ▶ my $match_type = @_ ? shift : '-re';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
268 0 ▶ croak ( ILLEGAL_ARGUMENT . "match_type '$match_type' is invalid." )
Branch: 1 branch
BranchTrueFalseTotal
unless $match_type eq "-re" or $match_type eq "-ex" 0 0 0
Condition0%0/3
$match_type eq "-re" or $match_type eq "-ex"
l!l&&r!l&&!r
000
MC/DC0%0/2
$match_type eq "-re" or $match_type eq "-ex"
$match_type eq "-re" $match_type eq "-ex"
Truth table0%0/3
A or B
A= $match_type eq "-re"B= $match_type eq "-ex"
ABresult
1X1
011
000
269 unless ($match_type eq '-re' || $match_type eq '-ex');
270
271 0 my ($pos, $error);
272 ($pos, $error, $self->{match}, $self->{before}, $self->{after})
273 0 = $self->_sec_expect($timeout, $match_type, $pattern);
274
275 0 my $debug = $self->{debug};
276
277 # sanity verification
278 # Enforcing that match before and after have correct values
279 0 ▶ if (! defined $pos) { # if the pattern failed to match
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos) 0 0 0
280 # match should be undef
281 0 ▶ if (defined $self->{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"match"}) 0 0 0
282 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
283 0 carp ("The last expect() didn't match but \$exp->match() returned content '". $self->{match} ."'." .
284 " We'll set \$self->{match} to undef explicitly;");
285 }
286 0 $self->{match} = undef;
287 }
288 # after should be undef
289 0 ▶ if (defined $self->{after}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"after"}) 0 0 0
290 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
291 0 carp ("The last expect() didn't match but \$exp->after() returned content '". $self->{after} ."'." .
292 " We'll set \$self->{after} to undef explicitly;");
293 }
294 0 $self->{after} = undef;
295 }
296 }
297
298 0 return (defined $pos);
299 }
300
301 # string before() - returns the "before match" data of the last waitfor() call, or empty string.
302 # if the last waitfor() didn't match, before() will return all the current content on the input
303 # stream, just as if you had called peek() with the same timeout.
304 0 ▶sub before {
Subroutine before: not called CC=1 SCAR=6.9
Pod documented
305 0 my Net::SSH::Expect $self = shift;
306 0 return $self->{before};
307 }
308
309 # string match() - returns the "match" data of the last waitfor() call, or undef if didn't match.
310 0 ▶sub match {
Subroutine match: not called CC=1 SCAR=6.9
Pod documented
311 0 my Net::SSH::Expect $self = shift;
312 0 return $self->{match};
313 }
314
315 # string after() - returns the "after match" data of the last waitfor() call, or undef if didn't match.
316 0 ▶sub after {
Subroutine after: not called CC=1 SCAR=6.9
Pod documented
317 0 my Net::SSH::Expect $self = shift;
318 0 return $self->{after};
319 }
320
321
322 # send ("string") - breaks on through to the other side.
323 0 ▶sub send {
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
324 0 my Net::SSH::Expect $self = shift;
325 0 my $send = shift;
326 0 ▶ croak (ILLEGAL_ARGUMENT . " missing argument 'string'.") unless ($send);
Branch: 1 branch
BranchTrueFalseTotal
unless $send 0 0 0
327 0 my $exp = $self->get_expect();
328 0 my $t = $self->{terminator};
329 0 $exp->send($send . $t);
330 }
331
332 # peek([$timeout]) - returns what is in the input stream without removing anything
333 # params:
334 # $timeout: how many seconds peek() will wait for input
335 # dies:
336 # SSH_CONNECTION_ABORTED if EOF is found (error type 2)
337 # SSH_PROCESS_ERROR if the ssh process has died (error type 3)
338 # SSH_CONNECTION_ERROR if unknown error (type 4) is found
339 0 ▶sub peek {
Subroutine peek: not called CC=2 SCAR=17.9
Pod documented
340 0 my Net::SSH::Expect $self = shift;
341 0 ▶ my $timeout = @_ ? shift : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
342 0 my $exp = $self->get_expect();
343 0 $self->_sec_expect($timeout);
344 0 return $exp->before();
345 }
346
347 # string eat($string)- removes all the head of the input stream until $string inclusive.
348 # eat() will only be able to remove the $string if it's currently present on the
349 # input stream because eat() will wait 0 seconds before removing it.
350 #
351 # Use it associated with peek to eat everything that appears on the input stream:
352 #
353 # while ($chunk = $exp->eat($exp->peak())) {
354 # print $chunk;
355 # }
356 #
357 # Or use the read_all() method that does the above loop for you returning the accumulated
358 # result.
359 #
360 # param:
361 # string: a string currently available on the input stream.
362 # If $string doesn't start in the head, all the content before $string will also
363 # be removed.
364 #
365 # If $string is undef or empty string it will be returned immediately as it.
366 #
367 # returns:
368 # string: the removed content or empty string if there is nothing in the input stream.
369 #
370 # dies:
371 # SSH_CONNECTION_ABORTED if EOF is found (error type 2)
372 # SSH_PROCESS_ERROR if the ssh process has died (error type 3)
373 # SSH_CONNECTION_ERROR if unknown error (type 4) is found
374 #
375 # debbuging features:
376 # The following warnings are printed to STDERR if $exp->debug() == 1:
377 # eat() prints a warning is $string wasn't found in the head of the input stream.
378 # eat() prints a warning is $string was empty or undefined.
379 #
380 0 ▶sub eat {
Subroutine eat: not called CC=7 SCAR=40.3
Pod documented
381 0 my Net::SSH::Expect $self = shift;
382 0 my $string = shift;
383 0 ▶ unless (defined $string && $string ne "") {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string and $string ne "") 0 0 0
Condition0%0/3
defined $string and $string ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $string and $string ne ""
defined $string $string ne ""
Truth table0%0/3
A and B
A= defined $stringB= $string ne ""
ABresult
0X0
100
111
384 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
385 0 carp ("eat(): param \$string is undef or empty string\n");
386 }
387 0 return $string;
388 }
389
390 0 my $exp = $self->get_expect();
391
392 # the top of the input stream that will be removed from there and
393 # returned to the user
394 0 my $top;
395
396 # eat $string from (hopefully) the head of the input stream
397 0 $self->_sec_expect(0, '-ex', $string);
398 0 $top .= $exp->match();
399
400 # if before() returns any content, the $string passed is not in the beginning of the
401 # input stream.
402 0 ▶ if (defined $exp->before() && !($exp->before() eq "") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exp->before and not $exp->before eq "") 0 0 0
Condition0%0/3
defined $exp->before and not $exp->before eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $exp->before and not $exp->before eq ""
defined $exp->before not $exp->before eq ""
Truth table0%0/3
A and not(B)
A= defined $exp->beforeB= not $exp->before eq ""
ABresult
0X0
100
111
403 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
404 0 carp ("eat(): param \$string '$string' was found on the input stream ".
405 "after '". $exp->before() . "'.");
406 }
407 0 $top = $exp->before() . $top;
408 }
409 0 return $top;
410 }
411
412 # string read_all([$timeout]) - reads and remove all the output from the input stream.
413 # The reading/removing process will be interrupted after $timeout seconds of inactivity
414 # on the input stream.
415 0 ▶sub read_all {
Subroutine read_all: not called CC=3 SCAR=24.8
Pod documented
416 0 my Net::SSH::Expect $self = shift;
417 0 ▶ my $timeout = @_ ? shift : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
418 0 my $out;
419 0 ▶ while ($self->_sec_expect($timeout, '-re', qr/[\s\S]+/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_sec_expect($timeout, "-re", qr/[\s\S]+/)) 0 0 0
420 0 $out .= $self->get_expect()->match();
421 }
422 0 return $out;
423 }
424
425
426 # boolean has_line([$timeout]) - tells if there is one more line on the input stream
427 0 ▶sub has_line {
Subroutine has_line: not called CC=2 SCAR=17.9
Pod undocumented
428 0 my Net::SSH::Expect $self = shift;
429 0 ▶ my $timeout = @_ ? shift : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
430 0 $self->{next_line} = $self->read_line($timeout);
431 0 return (defined $self->{next_line});
432 }
433
434 # string read_line([$timeout]) - reads the next line from the input stream
435 # Read a line of text. A line is considered to be terminated by the 'teminator'
436 # character. Default is "\n". Lines can also be ended with "\r" or "\r\n".
437 # Remember to adequate this for your system with the terminator() method.
438 # When there are no more lines available, read_line() returns undef. Note that this doen't mean
439 # there is no data left on input stream since there can be a string not terminated with the
440 # 'terminator' character, notably the remote prompt could be left there when read_line() returns
441 # undef.
442 #
443 # params:
444 # $timeout: the timeout waiting for a line. Defaults to timeout().
445 #
446 # returns:
447 # string: a line on the input stream, without the trailing 'terminator' character.
448 # An empty string indicates that the line read only contained the 'terminator'
449 # character (an empty line)
450 # undef: when there are no more lines on the input stream.
451 #
452 0 ▶sub read_line {
Subroutine read_line: not called CC=3 SCAR=24.8
Pod documented
453 0 my Net::SSH::Expect $self = shift;
454 0 ▶ my $timeout = @_ ? shift : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
455 0 my $t = $self->{terminator};
456 0 my $line = undef;
457 0 ▶ if ( $self->waitfor($t, $timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->waitfor($t, $timeout)) 0 0 0
458 0 $line = $self->before();
459 }
460 0 return $line;
461 }
462
463 # string exec($cmd [,$timeout]) - executes a command, returns the complete output
464 0 ▶sub exec {
Subroutine exec: not called CC=2 SCAR=17.9
Pod documented
465 0 my Net::SSH::Expect $self = shift;
466 0 my $cmd = shift;
467 0 ▶ my $timeout = @_ ? shift : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
468 0 $self->send($cmd);
469 0 return $self->read_all($timeout);
470 }
471
472 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
473 0 my Net::SSH::Expect $self = shift;
474 0 my $exp = $self->get_expect();
475 0 $exp->hard_close();
476 0 return 1;
477 }
478
479
480 # returns
481 # reference: the internal Expect object used to manage the ssh connection.
482 0 ▶sub get_expect {
Subroutine get_expect: not called CC=2 SCAR=17.9
Pod documented
483 0 my Net::SSH::Expect $self = shift;
484 my $exp = defined ($self->{expect}) ? $self->{expect} :
485 0 ▶ croak (ILLEGAL_STATE_NO_SSH_CONNECTION);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'expect'} ? : 0 0 0
486 0 return $exp;
487 }
488
489 # void restart_timeout_upon_receive( 0 | 1 ) - changes the timeout counter behaviour
490 # params:
491 # boolean: if true, sets the timeout to "inactivity timeout", if false
492 # sets it to "absolute timeout".
493 # dies:
494 # IllegalParamenter if argument is not given.
495 0 ▶sub restart_timeout_upon_receive {
Subroutine restart_timeout_upon_receive: not called CC=2 SCAR=17.9
Pod documented
496 0 my Net::SSH::Expect $self = shift;
497 0 ▶ my $value = @_ ? shift : croak (ILLEGAL_ARGUMENT . " missing argument.");
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
498 0 $self->get_expect()->restart_timeout_upon_receive($value);
499 }
500
501 #
502 # Setter methods
503 #
504
505 0 ▶sub host {
Subroutine host: not called CC=2 SCAR=17.9
Pod documented
506 0 my Net::SSH::Expect $self = shift;
507 0 ▶ croak(ILLEGAL_ARGUMENT . " No host supplied to 'host()' method") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
508 0 $self->{host} = shift;
509 }
510
511 0 ▶sub user {
Subroutine user: not called CC=2 SCAR=17.9
Pod documented
512 0 my Net::SSH::Expect $self = shift;
513 0 ▶ croak(ILLEGAL_ARGUMENT . " No user supplied to 'user()' method") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
514 0 $self->{user} =shift;
515 }
516
517 0 ▶sub password{
Subroutine password: not called CC=2 SCAR=17.9
Pod documented
518 0 my Net::SSH::Expect $self = shift;
519 0 ▶ croak(ILLEGAL_ARGUMENT . " No password supplied to 'password()' method") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
520 0 $self->{password} = shift;
521 }
522
523 0 ▶sub port {
Subroutine port: not called CC=5 SCAR=34.0
Pod documented
524 0 my Net::SSH::Expect $self = shift;
525 0 ▶ croak(ILLEGAL_ARGUMENT . " No value passed to 'port()' method") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
526 0 my $port = shift;
527 0 ▶ croak (ILLEGAL_ARGUMENT . " Passed number '$port' is not a valid port number")
Branch: 1 branch
BranchTrueFalseTotal
if not $port =~ /\A\d+\z/ or $port < 1 or $port > 65535 0 0 0
Condition0%0/6
not $port =~ /\A\d+\z/ or $port < 1 or $port > 65535
l!l&&r!l&&!r
000
not $port =~ /\A\d+\z/ or $port < 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not $port =~ /\A\d+\z/ or $port < 1 or $port > 65535
not $port =~ /\A\d+\z/ $port < 1 $port > 65535
Truth table0%0/4
not(A) or B or C
A= not $port =~ /\A\d+\z/B= $port < 1C= $port > 65535
ABCresult
1XX1
01X1
0011
0000
528 if ($port !~ /\A\d+\z/ || $port < 1 || $port > 65535);
529 0 $self->{port} = $port;
530 }
531
532 0 ▶sub terminator {
Subroutine terminator: not called CC=2 SCAR=17.9
Pod documented
533 0 my Net::SSH::Expect $self = shift;
534 0 ▶ $self->{terminator} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
535 0 return $self->{terminator};
536 }
537
538 # boolean debug([0|1]) - gets/sets the $exp->{debug} attribute.
539 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
540 0 my Net::SSH::Expect $self = shift;
541 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
542 0 $self->{debug} = shift;
543 }
544 0 return $self->{debug};
545 }
546
547 # number timeout([$number]) - get/set the default timeout used for every method
548 # that reads data from the input stream.
549 # The only exception is eat() that has its timeout defined as 0.
550 0 ▶sub timeout {
Subroutine timeout: not called CC=4 SCAR=30.0
Pod documented
551 0 my Net::SSH::Expect $self = shift;
552 0 ▶ if (! @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
553 0 return $self->{timeout};
554 }
555 0 my $timeout = shift;
556 0 ▶ if ( $timeout !~ /\A\d+\z/ || $timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $timeout =~ /\A\d+\z/ or $timeout < 0) 0 0 0
Condition0%0/3
not $timeout =~ /\A\d+\z/ or $timeout < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $timeout =~ /\A\d+\z/ or $timeout < 0
not $timeout =~ /\A\d+\z/ $timeout < 0
Truth table0%0/3
not(A) or B
A= not $timeout =~ /\A\d+\z/B= $timeout < 0
ABresult
1X1
011
000
557 0 croak (ILLEGAL_ARGUMENT . " timeout '$timeout' is not a positive number.");
558 }
559 0 $self->{timeout} = $timeout;
560 }
561
562 #
563 # Private Methods
564 #
565
566 # _sec_expect(@params) - secure expect. runs expect with @params and croaks if problems happen
567 # Note: timeout is not considered a problem.
568 # params:
569 # the same parameters as expect() accepts.
570 # returns:
571 # the same as expect() returns
572 # dies:
573 # SSH_CONNECTION_ABORTED if EOF is found (error type 2)
574 # SSH_PROCESS_ERROR if the ssh process has died (error type 3)
575 # SSH_CONNECTION_ERROR if unknown error is found (error type 4)
576 0 ▶sub _sec_expect {
Subroutine _sec_expect: not called CC=7 SCAR=40.3
577 0 my Net::SSH::Expect $self = shift;
578 0 ▶ my @params = @_ ? @_ : die ("\@params cannot be undefined.");
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
579 0 my $exp = $self->get_expect();
580 0 my ($pos, $error, $match, $before, $after) = $exp->expect(@params);
581 0 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
582 0 my $error_first_digit = substr($error, 0, 1);
583 0 ▶ if ($error_first_digit eq '2') {
Branch: 3 branches
BranchTrueFalseTotal
if ($error_first_digit eq '2') { } 0 0 0
elsif ($error_first_digit eq '3') { } 0 0 0
elsif ($error_first_digit eq '4') { } 0 0 0
584 # found eof
585 0 croak (SSH_CONNECTION_ABORTED);
586 } elsif ($error_first_digit eq '3') {
587 # ssh process died
588 0 croak (SSH_PROCESS_ERROR . " The ssh process was terminated.");
589 } elsif ($error_first_digit eq '4') {
590 # unknown reading error
591 0 croak (SSH_CONNECTION_ERROR . " Reading error type 4 found: $error");
592 }
593 }
594 0 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
595 0 return ($pos, $error, $match, $before, $after);
596 } else {
597 0 return $pos;
598 }
599 }
600
601 1 1;
602
603
604
↑ Summary