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

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 149 CC 16 SCAR 30.8
CC
16
Cov
0%
put
42.0
connect
30.0
get
30.0
CRAP 21.9
1 package XAS::Lib::SSH::Client;
2  
3 our $VERSION = '0.01';
4  
5 use IO::Select;
6 use Errno 'EAGAIN';
7 use Net::SSH2 ':all';
8  
9 use XAS::Class
10 debug => 0,
11 version => $VERSION,
12 base => 'XAS::Base',
13 accessors => 'ssh chan sock select',
14 mutators => 'attempts',
15 vars => {
16 PARAMS => {
17 -port => { optional => 1, default => 22 },
18 -timeout => { optional => 1, default => 0.2 },
19 -username => { optional => 1, default => undef},
20 -server => { optional => 1, default => 'localhost' },
21 -password => { optional => 1, default => undef, depends => [ '-username' ] },
22 -priv_key => { optional => 1, default => undef, depends => [ '-pub_key', '-username' ] },
23 -pub_key => { optional => 1, default => undef, depends => [ '-priv_key', '-username' ] },
24 },
25 }
26 ;
27  
28 #use Data::Dumper;
29  
30 # ----------------------------------------------------------------------
31 # Public Methods
32 # ----------------------------------------------------------------------
33  
34 sub connect {
35 my $self = shift;
36  
37 my ($errno, $name, $errstr);
38  
39 if ($self->ssh->connect($self->server, $self->port)) {
40  
41 if ($self->pub_key) {
42  
43 unless ($self->ssh->auth_publickey($self->username,
44 $self->pub_key, $self->priv_key, $self->password)) {
45  
46 ($errno, $name, $errstr) = $self->ssh->error();
47 $self->throw_msg(
48 'xas.lib.ssh.client.connect.autherr',
49 'autherr',
50 $name, $errstr
51 );
52  
53 }
54  
55 } else {
56  
57 unless ($self->ssh->auth_password($self->username, $self->password)) {
58  
59 ($errno, $name, $errstr) = $self->ssh->error();
60 $self->throw_msg(
61 'xas.lib.ssh.client.connect.autherr',
62 'autherr',
63 $name, $errstr
64 );
65  
66 }
67  
68 }
69  
70 $self->{sock} = $self->ssh->sock();
71 $self->{chan} = $self->ssh->channel();
72 $self->{select} = IO::Select->new($self->sock);
73  
74 $self->setup();
75  
76 } else {
77  
78 ($errno, $name, $errstr) = $self->ssh->error();
79 $self->throw_msg(
80 'xas.lib.ssh.client.connect.conerr',
81 'conerr',
82 $name, $errstr
83 );
84  
85 }
86  
87 }
88  
89 sub setup {
90 my $self = shift;
91  
92 warn "setup() needs to be overridden\n";
93  
94 }
95  
96 sub disconnect {
97 my $self = shift;
98  
99 if (defined($self->chan)) {
100  
101 $self->chan->send_eof();
102 $self->chan->close();
103  
104 }
105  
106 $self->ssh->disconnect();
107  
108 }
109  
110 sub get {
111 my $self = shift;
112  
113 my $counter = 0;
114 my $output = '';
115 my $working = 1;
116  
117 # Setup non-blocking read. Keep reading until nothing is left.
118 # Return the raw output, if any.
119 #
120 # Patterned after some libssh2 examples and C network programming
121 # "best practices".
122  
123 $self->chan->blocking(0);
124  
125 while ($working) {
126  
127 my $buf;
128  
129 if ($self->chan->read($buf, 512)) {
130  
131 $output .= $buf;
132  
133 } else {
134  
135 my $syserr = $! + 0;
136 my ($errno, $name, $errstr) = $self->ssh->error();
137 if (($errno == LIBSSH2_ERROR_EAGAIN) || ($syserr == EAGAIN)) {
138  
139 $counter++;
140
141 $working = 0 if ($counter > $self->attempts);
142 $self->_waitsocket() if ($counter <= $self->attempts);
143  
144 } else {
145  
146 $self->chan->blocking(1);
147 $self->throw_msg(
148 'xas.lib.ssh.client.get.protoerr',
149 'protoerr',
150 $name, $errstr
151 );
152  
153 }
154  
155 }
156  
157 }
158  
159 $self->chan->blocking(1);
160  
161 return $output;
162  
163 }
164  
165 sub put {
166 my $self = shift;
167 my ($buffer) = validate_pos(@_, 1);
168  
169 my $counter = 0;
170 my $working = 1;
171 my $written = 0;
172 my $bufsize = length($buffer);
173  
174 # Setup non-blocking writes. Keep writting until nothing is left.
175 # Returns the number of bytes written, if any.
176 #
177 # Patterned after some libssh2 examples and C network programming
178 # "best practices".
179  
180 $self->chan->blocking(0);
181  
182 do {
183  
184 if (my $bytes = $self->chan->write($buffer)) {
185  
186 $written += $bytes;
187 $buffer = substr($buffer, $bytes);
188 $working = 0 if ($written >= $bufsize);
189  
190 } else {
191  
192 my ($errno, $name, $errstr) = $self->ssh->error();
193 if ($errno == LIBSSH2_ERROR_EAGAIN) {
194  
195 $counter++;
196  
197 $working = 0 if ($counter > $self->attempts);
198 $self->_waitsocket() if ($counter <= $self->attempts);
199  
200 } else {
201  
202 $self->chan->blocking(1);
203 $self->throw_msg(
204 'xas.lib.ssh.client.put.protoerr',
205 'protoerr',
206 $name, $errstr
207 );
208  
209 }
210  
211 }
212  
213 } while ($working);
214  
215 $self->chan->blocking(1);
216  
217 return $written;
218  
219 }
220  
221 sub DESTROY {
222 my $self = shift;
223  
224 $self->disconnect();
225  
226 }
227  
228 # ----------------------------------------------------------------------
229 # Private Methods
230 # ----------------------------------------------------------------------
231  
232 sub init {
233 my $class = shift;
234  
235 my $self = $class->SUPER::init(@_);
236  
237 $self->{ssh} = Net::SSH2->new();
238  
239 $self->attempts(5); # number of EAGAIN attempts
240  
241 return $self;
242  
243 }
244  
245 sub _waitsocket {
246 my $self = shift;
247  
248 my $to = $self->timeout;
249 my $dir = $self->ssh->block_directions();
250  
251 # If $dir is 1, then input is blocking.
252 # If $dir is 2, then output is blocking.
253 #
254 # Patterned after some libssh2 examples.
255  
256 if ($dir == 1) {
257  
258 $self->select->can_read($to);
259  
260 } else {
261  
262 $self->select->can_write($to);
263  
264 }
265  
266 return $! + 0;
267  
268 }
269  
270 1;
271  
272 __END__
273  
274 =head1 NAME
275  
276 XAS::Lib::SSH::Client - A SSH based client
277  
278 =head1 SYNOPSIS
279  
280 use XAS::Lib::SSH::Client;
281  
282 my $client = XAS::Lib::SSH::Client->new(
283 -server => 'auburn-xen-01',
284 -username => 'root',
285 -password => 'secret',
286 );
287  
288 $client->connect();
289
290 $client->put($data);
291 $data = $client->get();
292  
293 $client->disconnect();
294  
295 =head1 DESCRIPTION
296  
297 The module provides basic network connectivity along with input/output methods
298 using the SSH protocol. It can authenticate using username/password or
299 username/public key/private key/password.
300  
301 =head1 METHODS
302  
303 =head2 new
304  
305 This initializes the object. It takes the following parameters:
306  
307 =over 4
308  
309 =item B<-username>
310  
311 An optional username to use when connecting to the server.
312  
313 =item B<-password>
314  
315 An optional password to use for authentication.
316  
317 =item B<-pub_key>
318  
319 An optional public ssh key file to use.
320  
321 =item B<-priv_key>
322  
323 An optional private ssh key to use.
324  
325 =item B<-server>
326  
327 The server to connect too. Defaults to 'localhost'.
328  
329 =item B<-port>
330  
331 The port to use on the server. It defaults to 22.
332  
333 =item B<-timeout>
334  
335 The number of seconds to timeout writes. It must be compatible with IO::Select.
336 Defaults to 0.2.
337  
338 =back
339  
340 =head2 connect
341  
342 This method makes a connection to the server.
343  
344 =head2 setup
345  
346 This method sets up the channel to be used. It needs to be overridden
347 to be useful.
348  
349 =head2 get
350  
351 This method reads date from the channel. It uses non-blocking reads. It
352 will attempt to read all pending data.
353  
354 =head2 put($buffer)
355  
356 This method will write data to the channel. It uses non-blocking writes.
357 It will attempt to write all the data in the buffer.
358  
359 =over 4
360  
361 =item B<$buffer>
362  
363 The buffer to be written.
364  
365 =back
366  
367 =head2 disconnect
368  
369 This method closes the connection.
370  
371 =head1 MUTATORS
372  
373 =head2 attempts
374  
375 This is used when reading data from the channel. It triggers how many
376 times to attempt reading from the channel when a LIBSSH2_ERROR_EAGAIN
377 error occurs. The default is 5 times.
378  
379 =head1 SEE ALSO
380  
381 =over 4
382  
383 =item L<XAS|XAS>
384  
385 =back
386  
387 =head1 AUTHOR
388  
389 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
390  
391 =head1 COPYRIGHT AND LICENSE
392  
393 Copyright (C) 2014 Kevin L. Esteb
394  
395 This library is free software; you can redistribute it and/or modify
396 it under the same terms as Perl itself, either Perl version 5.8.8 or,
397 at your option, any later version of Perl 5 you may have available.
398  
399 See L<http://dev.perl.org/licenses/> for more information.
400  
401 =cut