blib/lib/XAS/Lib/SSH/Client.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/XAS/Lib/Curl/HTTP.pm ↑ Summary blib/lib/XAS.pm »
1 package XAS::Lib::SSH::Client;
2  
3 our $VERSION = '0.01';
4  
5 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Errno 'EAGAIN';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::SSH2 ':all';
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use XAS::Class
10 debug => 0,
11 version => $VERSION,
12 base => 'XAS::Base',
13 mixin => 'XAS::Lib::Mixins::Bufops',
14 accessors => 'ssh chan sock select',
15 mutators => 'attempts',
16 utils => 'trim dotid',
17 import => 'class',
18 vars => {
19 PARAMS => {
20 -port => { optional => 1, default => 22 },
21 -timeout => { optional => 1, default => 0.2 },
22 -username => { optional => 1, default => undef},
23 -host => { optional => 1, default => 'localhost' },
24 -eol => { optional => 1, default => "\015\012" },
25 -password => { optional => 1, default => undef, depends => [ 'username' ] },
26 -priv_key => { optional => 1, default => undef, depends => [ 'pub_key', 'username' ] },
27 -pub_key => { optional => 1, default => undef, depends => [ 'priv_key', 'username' ] },
28 },
29 ERRNO => 0,
30 ERRSTR => '',
31 }
32 ;
33  
34 #use Data::Dumper;
35  
36 # ----------------------------------------------------------------------
37 # Public Methods
38 # ----------------------------------------------------------------------
39  
40 sub connect {
41 my $self = shift;
42  
43 my ($errno, $name, $errstr);
44  
45 $self->class->var('ERRNO', 0);
46 $self->class->var('ERRSTR', '');
47  
48 if ($self->ssh->connect($self->host, $self->port)) {
49  
50 if ($self->pub_key) {
51  
52 unless ($self->ssh->auth_publickey($self->username,
53 $self->pub_key, $self->priv_key)) {
54  
55 ($errno, $name, $errstr) = $self->ssh->error();
56  
57 $self->class->var('ERRNO', $errno);
58 $self->class->var('ERRSTR', $errstr);
59  
60 $self->throw_msg(
61 dotid($self->class) . '.autherr',
62 'ssh_client_autherr',
63 $name, $errstr
64 );
65  
66 }
67  
68 } else {
69  
70 unless ($self->ssh->auth_password($self->username, $self->password)) {
71  
72 ($errno, $name, $errstr) = $self->ssh->error();
73  
74 $self->class->var('ERRNO', $errno);
75 $self->class->var('ERRSTR', $errstr);
76  
77 $self->throw_msg(
78 dotid($self->class) . '.autherr',
79 'ssh_client_autherr',
80 $name, $errstr
81 );
82  
83 }
84  
85 }
86  
87 $self->{sock} = $self->ssh->sock();
88 $self->{chan} = $self->ssh->channel();
89 $self->{select} = IO::Select->new($self->sock);
90  
91 $self->setup();
92  
93 } else {
94  
95 ($errno, $name, $errstr) = $self->ssh->error();
96  
97 $self->class->var('ERRNO', $errno);
98 $self->class->var('ERRSTR', $errstr);
99  
100 $self->throw_msg(
101 dotid($self->class) . '.conerr',
102 'ssh_client_conerr',
103 $name, $errstr
104 );
105  
106 }
107  
108 }
109  
110 sub setup {
111 my $self = shift;
112  
113 }
114  
115 sub pending {
116 my $self = shift;
117  
118 return length($self->{buffer});
119  
120 }
121  
122 sub disconnect {
123 my $self = shift;
124  
125 if (my $chan = $self->chan) {
126  
127 $chan->send_eof();
128 $chan->close();
129  
130 }
131  
132 $self->ssh->disconnect();
133  
134 }
135  
136 sub get {
137 my $self = shift;
138 my ($length) = $self->validate_params(\@_, [
139 { optional => 1, default => 512 }
140 ]);
141  
142 my $state = 1;
143 my $output = '';
144 my $working = 1;
145  
146 # extract $length from buffer. if the buffer size is > $length then
147 # try to refill buffer. If there is nothing to read, the return
148 # the remainder of the buffer.
149 #
150 # Patterned after some libssh2 examples and C network programming
151 # "best practices".
152  
153 if ($self->pending > $length) {
154  
155 $output = $self->buf_slurp(\$self->{buffer}, $length);
156  
157 } else {
158  
159 $self->_fill_buffer();
160  
161 my $l = ($self->pending > $length) ? $length : $self->pending;
162 $output = $self->buf_slurp(\$self->{buffer}, $l);
163  
164 }
165  
166 return $output;
167  
168 }
169  
170 sub gets {
171 my $self = shift;
172  
173 my $buffer;
174 my $output = '';
175  
176 while (my $buf = $self->get()) {
177  
178 $buffer .= $buf;
179  
180 if ($output = $self->buf_get_line(\$buffer, $self->eol)) {
181  
182 $self->{buffer} = $buffer . $self->{buffer};
183 last;
184  
185 }
186  
187 }
188  
189 return trim($output);
190  
191 }
192  
193 sub put {
194 my $self = shift;
195 my ($buffer) = $self->validate_params(\@_, [1]);
196  
197 my $counter = 0;
198 my $working = 1;
199 my $written = 0;
200 my $bufsize = length($buffer);
201  
202 $self->class->var('ERRNO', 0);
203 $self->class->var('ERRSTR', '');
204  
205 # Setup non-blocking writes. Keep writting until nothing is left.
206 # Returns the number of bytes written, if any.
207 #
208 # Patterned after some libssh2 examples and C network programming
209 # "best practices".
210  
211 $self->chan->blocking(0);
212  
213 do {
214  
215 if (my $bytes = $self->chan->write($buffer)) {
216  
217 $written += $bytes;
218 $buffer = substr($buffer, $bytes);
219 $working = 0 if ($written >= $bufsize);
220  
221 } else {
222  
223 my ($errno, $name, $errstr) = $self->ssh->error();
224 if ($errno == LIBSSH2_ERROR_EAGAIN) {
225  
226 $counter++;
227  
228 $working = 0 if ($counter > $self->attempts);
229 $self->_waitsocket() if ($counter <= $self->attempts);
230  
231 } else {
232  
233 $self->chan->blocking(1);
234  
235 $self->class->var('ERRNO', $errno);
236 $self->class->var('ERRSTR', $errstr);
237  
238 $self->throw_msg(
239 dotid($self->class) . '.protoerr',
240 'ssh_client_protoerr',
241 $name, $errstr
242 );
243  
244 }
245  
246 }
247  
248 } while ($working);
249  
250 $self->chan->blocking(1);
251  
252 return $written;
253  
254 }
255  
256 sub puts {
257 my $self = shift;
258 my ($buffer) = $self->validate_params(\@_, [1]);
259  
260 my $output = sprintf("%s%s", trim($buffer), $self->eol);
261 my $written = $self->put($output);
262  
263 return $written;
264  
265 }
266  
267 sub errno {
268 my $class = shift;
269 my ($value) = XAS::Base->validate_params(\@_, [
270 { optional => 1, default => undef }
271 ]);
272  
273 class->var('ERRNO', $value) if (defined($value));
274  
275 return class->var('ERRNO');
276  
277 }
278  
279 sub errstr {
280 my $class = shift;
281 my ($value) = XAS::Base->validate_params(\@_, [
282 { optional => 1, default => undef }
283 ]);
284  
285 class->var('ERRSTR', $value) if (defined($value));
286  
287 return class->var('ERRSTR');
288  
289 }
290  
291 sub DESTROY {
292 my $self = shift;
293  
294 $self->disconnect();
295  
296 }
297  
298 # ----------------------------------------------------------------------
299 # Private Methods
300 # ----------------------------------------------------------------------
301  
302 sub init {
303 my $class = shift;
304  
305 my $self = $class->SUPER::init(@_);
306  
307 $self->{ssh} = Net::SSH2->new();
308 $self->{buffer} = '';
309  
310 $self->attempts(5); # number of EAGAIN attempts
311  
312 return $self;
313  
314 }
315  
316 sub _fill_buffer {
317 my $self = shift;
318  
319 my $read = 0;
320 my $counter = 0;
321 my $working = 1;
322  
323 # Setup non-blocking read. Keep reading until nothing is left.
324 # i.e. the reads timeout.
325  
326 $self->chan->blocking(0);
327  
328 while ($working) {
329  
330 my $buf;
331  
332 if (my $bytes = $self->chan->read($buf, 512)) {
333  
334 $self->{buffer} .= $buf;
335 $read += $bytes;
336  
337 } else {
338  
339 my $syserr = $! + 0;
340 my ($errno, $name, $errstr) = $self->ssh->error();
341  
342 if (($errno == LIBSSH2_ERROR_EAGAIN) || ($syserr == EAGAIN)) {
343  
344 $counter++;
345
346 $working = 0 if ($counter > $self->attempts);
347 $self->_waitsocket() if ($counter <= $self->attempts);
348  
349 } else {
350  
351 $self->chan->blocking(1);
352  
353 $self->class->var('ERRNO', $errno);
354 $self->class->var('ERRSTR', $errstr);
355  
356 $self->throw_msg(
357 dotid($self->class) . '.protoerr',
358 'ssh_client_protoerr',
359 $name, $errstr
360 );
361  
362 }
363  
364 }
365  
366 }
367  
368 $self->chan->blocking(1);
369  
370 return $read;
371  
372 }
373
374 sub _waitsocket {
375 my $self = shift;
376  
377 my $to = $self->timeout;
378 my $dir = $self->ssh->block_directions();
379  
380 # If $dir is 1, then input is blocking.
381 # If $dir is 2, then output is blocking.
382 #
383 # Patterned after some libssh2 examples.
384  
385 if ($dir == 1) {
386  
387 $self->select->can_read($to);
388  
389 } else {
390  
391 $self->select->can_write($to);
392  
393 }
394  
395 return $! + 0;
396  
397 }
398  
399 1;
400  
401 __END__
402  
403 =head1 NAME
404  
405 XAS::Lib::SSH::Client - A SSH based client
406  
407 =head1 SYNOPSIS
408  
409 use XAS::Lib::SSH::Client;
410  
411 my $client = XAS::Lib::SSH::Client->new(
412 -host => 'auburn-xen-01',
413 -username => 'root',
414 -password => 'secret',
415 );
416  
417 $client->connect();
418
419 $client->put($data);
420 $data = $client->get();
421  
422 $client->disconnect();
423  
424 =head1 DESCRIPTION
425  
426 The module provides basic network connectivity along with input/output methods
427 using the SSH protocol. It can authenticate using username/password or
428 username/public key/private key/password.
429  
430 =head1 METHODS
431  
432 =head2 new
433  
434 This initializes the object. It takes the following parameters:
435  
436 =over 4
437  
438 =item B<-username>
439  
440 An optional username to use when connecting to the server.
441  
442 =item B<-password>
443  
444 An optional password to use for authentication.
445  
446 =item B<-pub_key>
447  
448 An optional public ssh key file to use.
449  
450 =item B<-priv_key>
451  
452 An optional private ssh key to use.
453  
454 =item B<-host>
455  
456 The server to connect too. Defaults to 'localhost'.
457  
458 =item B<-port>
459  
460 The port to use on the server. It defaults to 22.
461  
462 =item B<-timeout>
463  
464 The number of seconds to timeout writes. It must be compatible with IO::Select.
465 Defaults to 0.2.
466  
467 =item B<-eol>
468  
469 The EOL to use, defaults to "\015\012".
470  
471 =back
472  
473 =head2 connect
474  
475 This method makes a connection to the server.
476  
477 =head2 setup
478  
479 This method sets up the channel to be used. It needs to be overridden
480 to be useful.
481  
482 =head2 get($length)
483  
484 This block reads data from the channel. A buffer is returned when it reaches
485 $length or timeout, whichever is first.
486  
487 =over 4
488  
489 =item B<$length>
490  
491 An optional length for the buffer. Defaults to 512 bytes.
492  
493 =back
494  
495 =head2 gets
496  
497 This reads a buffer delimited by the eol from the channel.
498  
499 =head2 errno
500  
501 A class method to return the SSH error number.
502  
503 =head2 errstr
504  
505 A class method to return the SSH error string.
506  
507 =head2 put($buffer)
508  
509 This method will write a buffer to the channel. Returns the number of
510 bytes written.
511  
512 =over 4
513  
514 =item B<$buffer>
515  
516 The buffer to be written.
517  
518 =back
519  
520 =head2 puts($buffer)
521  
522 This writes a buffer that is terminated with eol to the channel. Returns the
523 number of bytes written.
524  
525 =over 4
526  
527 =item B<$buffer>
528  
529 The buffer to send over the socket.
530  
531 =back
532  
533 =head2 disconnect
534  
535 This method closes the connection.
536  
537 =head1 MUTATORS
538  
539 =head2 attempts
540  
541 This is used when reading data from the channel. It triggers how many
542 times to attempt reading from the channel when a LIBSSH2_ERROR_EAGAIN
543 error occurs. The default is 5 times.
544  
545 =head1 SEE ALSO
546  
547 =over 4
548  
549 =item L<XAS|XAS>
550  
551 =back
552  
553 =head1 AUTHOR
554  
555 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
556  
557 =head1 COPYRIGHT AND LICENSE
558  
559 Copyright (C) 2014 Kevin L. Esteb
560  
561 This is free software; you can redistribute it and/or modify it under
562 the terms of the Artistic License 2.0. For details, see the full text
563 of the license at http://www.perlfoundation.org/artistic_license_2_0.
564  
565 =cut
566  
« blib/lib/XAS/Lib/Curl/HTTP.pm ↑ Summary blib/lib/XAS.pm »