blib/lib/Net/SCP.pm

filter: Statementstmt 27.2% 33 / 121 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 40.9% 9 / 22 Podpod 100% 11 / 11 totaltotal 19.7% 53 / 268 CC 39 SCAR 38.6
CC
39
Cov
16%
scp
110.0
size
90.0
mkdir
56.0
CRAP 47.3
↑ Summary
1 package Net::SCP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT_OK $scp $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::SSH qw(sshopen3);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 @ISA = qw(Exporter);
14 1 @EXPORT_OK = qw( scp iscp );
15 1 $VERSION = '0.08';
16  
17 1 $scp = "scp";
18  
19 1 $DEBUG = 0;
20  
21 =head1 NAME
22  
23 Net::SCP - Perl extension for secure copy protocol
24  
25 =head1 SYNOPSIS
26  
27 #procedural interface
28 use Net::SCP qw(scp iscp);
29 scp($source, $destination);
30 iscp($source, $destination); #shows command, asks for confirmation, and
31 #allows user to type a password on tty
32  
33 #OO interface
34 $scp = Net::SCP->new( "hostname", "username" );
35 #with named params
36 $scp = Net::SCP->new( { "host"=>$hostname, "user"=>$username } );
37 $scp->get("filename") or die $scp->{errstr};
38 $scp->put("filename") or die $scp->{errstr};
39 #tmtowtdi
40 $scp = new Net::SCP;
41 $scp->scp($source, $destination);
42  
43 #Net::FTP-style
44 $scp = Net::SCP->new("hostname");
45 $scp->login("user");
46 $scp->cwd("/dir");
47 $scp->size("file");
48 $scp->get("file");
49  
50 =head1 DESCRIPTION
51  
52 Simple wrappers around ssh and scp commands.
53  
54 =head1 SUBROUTINES
55  
56 =over 4
57  
58 =item scp SOURCE, DESTINATION
59  
60 Can be called either as a subroutine or a method; however, the subroutine
61 interface is depriciated.
62  
63 Calls scp in batch mode, with the B<-B> B<-p> B<-q> and B<-r> options.
64 Returns false upon error, with a text error message accessable in
65 $scp->{errstr}.
66  
67 Returns false and sets the B<errstr> attribute if there is an error.
68  
69 =cut
70  
71 0 ▶sub scp {
Subroutine scp: not called CC=10 SCAR=47.0
Pod documented
72 0 ▶ my $self = ref($_[0]) ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
73 0 my($src, $dest, $interact) = @_;
74 0 my $flags = '-p';
75 0 ▶ $flags .= 'r' unless &_islocal($src) && ! -d $src;
Branch: 1 branch
BranchTrueFalseTotal
unless &_islocal($src) and not -d $src 0 0 0
Condition0%0/3
&_islocal($src) and not -d $src
!ll&&!rl&&r
000
MC/DC0%0/2
&_islocal($src) and not -d $src
&_islocal($src) not -d $src
Truth table0%0/3
A and not(B)
A= &_islocal($src)B= not -d $src
ABresult
0X0
100
111
76 0 my @cmd;
77 0 ▶ if ( ( defined($interact) && $interact )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $interact and $interact or defined $self->{'interactive'} and $self->{'interactive'}) { } 0 0 0
Condition0%0/9
defined $interact and $interact or defined $self->{"interactive"} and $self->{"interactive"}
l!l&&r!l&&!r
000
defined $interact and $interact
!ll&&!rl&&r
000
defined $self->{"interactive"} and $self->{"interactive"}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $interact and $interact or defined $self->{"interactive"} and $self->{"interactive"}
defined $interact $interact defined $self->{"interactive"} $self->{"interactive"}
Truth table0%0/7
A and B or C and D
A= defined $interactB= $interactC= defined $self->{"interactive"}D= $self->{"interactive"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
78 || ( defined($self->{interactive}) && $self->{interactive} ) ) {
79 0 @cmd = ( $scp, $flags, $src, $dest );
80 0 print join(' ', @cmd), "\n";
81 0 ▶ unless ( &_yesno ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_yesno) 0 0 0
82 0 $self->{errstr} = "User declined";
83 0 return 0;
84 }
85 } else {
86 0 $flags .= 'qB';
87 0 @cmd = ( $scp, $flags, $src, $dest );
88 }
89 0 my($reader, $writer, $error ) =
90 ( new IO::Handle, new IO::Handle, new IO::Handle );
91 0 $writer->autoflush(1);# $error->autoflush(1);
92 0 local $SIG{CHLD} = 'DEFAULT';
93 0 my $pid = open3($writer, $reader, $error, @cmd );
94 0 waitpid $pid, 0;
95 0 ▶ if ( $? >> 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? >> 8) { } 0 0 0
96 0 my $errstr = join('', <$error>);
97 #chomp(my $errstr = <$error>);
98 0 $self->{errstr} = $errstr;
99 0 0;
100 } else {
101 0 1;
102 }
103 }
104  
105 =item iscp SOURCE, DESTINATION
106  
107 Can be called either as a subroutine or a method; however, the subroutine
108 interface is depriciated.
109  
110 Prints the scp command to be execute, waits for the user to confirm, and
111 (optionally) executes scp, with the B<-p> and B<-r> flags.
112  
113 Returns false and sets the B<errstr> attribute if there is an error.
114  
115 =cut
116  
117 0 ▶sub iscp {
Subroutine iscp: not called CC=2 SCAR=17.9
Pod documented
118 0 ▶ if ( ref($_[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
119 0 my $self = shift;
120 0 $self->{'interactive'} = 1;
121 0 $self->scp(@_);
122 } else {
123 0 scp(@_, 1);
124 }
125 }
126  
127 0 ▶sub _yesno {
Subroutine _yesno: not called CC=1 SCAR=6.9
128 0 print "Proceed [y/N]:";
129 0 my $x = scalar(<STDIN>);
130 0 $x =~ /^y/i;
131 }
132  
133 0 ▶sub _islocal {
Subroutine _islocal: not called CC=1 SCAR=6.9
134 0 shift !~ /^[^:]+:/
135 }
136  
137 =back
138  
139 =head1 METHODS
140  
141 =over 4
142  
143 =item new HOSTNAME [ USER ] | HASHREF
144  
145 This is the constructor for a new Net::SCP object. You must specify a
146 hostname, and may optionally provide a user. Alternatively, you may pass a
147 hashref of named params, with the following keys:
148  
149 host - hostname
150 user - username
151 interactive - bool
152 cwd - current working directory on remote server
153  
154 =cut
155  
156 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
157 0 my $proto = shift;
158 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
159 0 my $self;
160 0 ▶ if ( ref($_[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
161 0 $self = shift;
162 } else {
163 0 ▶ $self = {
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
164 'host' => shift,
165 'user' => ( scalar(@_) ? shift : '' ),
166 'interactive' => 0,
167 'cwd' => '',
168 };
169 }
170 0 bless($self, $class);
171 }
172  
173 =item login [USER]
174  
175 Compatibility method. Optionally sets the user.
176  
177 =cut
178  
179 0 ▶sub login {
Subroutine login: not called CC=2 SCAR=17.9
Pod documented
180 0 my($self, $user) = @_;
181 0 ▶ $self->{'user'} = $user if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
182 }
183  
184 =item cwd CWD
185  
186 Sets the cwd (used for a subsequent get or put request without a full pathname).
187  
188 =cut
189  
190 0 ▶sub cwd {
Subroutine cwd: not called CC=2 SCAR=17.9
Pod documented
191 0 my($self, $cwd) = @_;
192 0 ▶ $self->{'cwd'} = $cwd || '/';
Condition0%0/2
$cwd || '/'
l!l
00
MC/DC0%0/1
$cwd || '/'
$cwd
Truth table0%0/2
A
A= $cwd
Aresult
11
00
193 }
194  
195 =item get REMOTE_FILE [, LOCAL_FILE]
196  
197 Uses scp to transfer REMOTE_FILE from the remote host. If a local filename is
198 omitted, uses the basename of the remote file.
199  
200 =cut
201  
202 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
203 0 my($self, $remote, $local) = @_;
204 0 ▶ $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} and not $remote =~ m[^/] 0 0 0
Condition0%0/3
$self->{"cwd"} and not $remote =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cwd"} and not $remote =~ m[^/]
$self->{"cwd"} not $remote =~ m[^/]
Truth table0%0/3
A and not(B)
A= $self->{"cwd"}B= not $remote =~ m[^/]
ABresult
0X0
100
111
205 0 ▶ $local ||= basename($remote);
Condition0%0/3
$local ||= basename($remote)
l!l&&r!l&&!r
000
MC/DC0%0/2
$local ||= basename($remote)
$local basename($remote)
Truth table0%0/3
A ||= B
A= $localB= basename($remote)
ABresult
1X1
011
000
206 0 my $source = $self->{'host'}. ":$remote";
207 0 ▶ $source = $self->{'user'}. '@'. $source if $self->{'user'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user"} 0 0 0
208 0 $self->scp($source,$local);
209 }
210  
211 =item mkdir DIRECTORY
212  
213 Makes a directory on the remote server. Returns false and sets the B<errstr>
214 attribute on errors.
215  
216 (Implementation note: An ssh connection is established to the remote machine
217 and '/bin/mkdir B<-p>' is used to create the directory.)
218  
219 =cut
220  
221 0 ▶sub mkdir {
Subroutine mkdir: not called CC=7 SCAR=40.3
Pod documented
222 0 my($self, $directory) = @_;
223 $directory = $self->{'cwd'}. "/$directory"
224 0 ▶ if $self->{'cwd'} && $directory !~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} and not $directory =~ m[^/] 0 0 0
Condition0%0/3
$self->{"cwd"} and not $directory =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cwd"} and not $directory =~ m[^/]
$self->{"cwd"} not $directory =~ m[^/]
Truth table0%0/3
A and not(B)
A= $self->{"cwd"}B= not $directory =~ m[^/]
ABresult
0X0
100
111
225 0 my $host = $self->{'host'};
226 0 ▶ $host = $self->{'user'}. '@'. $host if $self->{'user'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user"} 0 0 0
227 0 my($reader, $writer, $error ) =
228 ( new IO::Handle, new IO::Handle, new IO::Handle );
229 0 $writer->autoflush(1);
230 0 my $pid = sshopen3( $host, $writer, $reader, $error,
231 '/bin/mkdir', '-p ', shell_quote($directory) );
232 0 waitpid $pid, 0;
233 0 ▶ if ( $? >> 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? >> 8) 0 0 0
234 0 ▶ chomp(my $errstr = <$error> || '');
Condition0%0/2
readline $error || ''
l!l
00
MC/DC0%0/1
readline $error || ''
readline $error
Truth table0%0/2
A
A= readline $error
Aresult
11
00
235 0 ▶ $self->{errstr} = $errstr || "mkdir exited with status ". ($?>>8);
Condition0%0/2
$errstr || 'mkdir exited with status ' . ($? >> 8)
l!l
00
MC/DC0%0/1
$errstr || 'mkdir exited with status ' . ($? >> 8)
$errstr
Truth table0%0/2
A
A= $errstr
Aresult
11
00
236 0 return 0;
237 }
238 0 1;
239 }
240  
241 =item size FILE
242  
243 Returns the size in bytes for the given file as stored on the remote server.
244 Returns 0 on error, and sets the B<errstr> attribute. In the case of an actual
245 zero-length file on the remote server, the special value '0e0' is returned,
246 which evaluates to zero when used as a number, but is true.
247  
248 (Implementation note: An ssh connection is established to the remote machine
249 and wc is used to determine the file size.)
250  
251 =cut
252  
253 0 ▶sub size {
Subroutine size: not called CC=9 SCAR=45.0
Pod documented
254 0 my($self, $file) = @_;
255 0 ▶ $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} and not $file =~ m[^/] 0 0 0
Condition0%0/3
$self->{"cwd"} and not $file =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cwd"} and not $file =~ m[^/]
$self->{"cwd"} not $file =~ m[^/]
Truth table0%0/3
A and not(B)
A= $self->{"cwd"}B= not $file =~ m[^/]
ABresult
0X0
100
111
256 0 my $host = $self->{'host'};
257 0 ▶ $host = $self->{'user'}. '@'. $host if $self->{'user'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user"} 0 0 0
258 0 my($reader, $writer, $error ) =
259 ( new IO::Handle, new IO::Handle, new IO::Handle );
260 0 $writer->autoflush(1);
261 #sshopen2($host, $reader, $writer, 'wc', '-c ', shell_quote($file) );
262 0 my $pid =
263 sshopen3($host, $writer, $reader, $error, 'wc', '-c ', shell_quote($file) );
264 0 waitpid $pid, 0;
265 0 ▶ if ( $? >> 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? >> 8) { } 0 0 0
266 0 chomp(my $errstr = <$error>);
267 0 ▶ $self->{errstr} = $errstr || "wc exited with status ". $?>>8;
Condition0%0/3
$errstr || 'wc exited with status ' . $? >> 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$errstr || 'wc exited with status ' . $? >> 8
$errstr 'wc exited with status ' . $? >> 8
Truth table0%0/3
A || B
A= $errstrB= 'wc exited with status ' . $? >> 8
ABresult
1X1
011
000
268 0 0;
269 } else {
270 0 ▶ chomp( my $size = <$reader> || 0 );
Condition0%0/2
readline $reader || 0
l!l
00
MC/DC0%0/1
readline $reader || 0
readline $reader
Truth table0%0/2
A
A= readline $reader
Aresult
11
00
271 0 ▶ if ( $size =~ /^\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size =~ /^\s*(\d+)/) { } 0 0 0
272 0 ▶ $1 ? $1 : '0e0';
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
273 } else {
274 0 $self->{errstr} = "unparsable output from remote wc: $size";
275 0 0;
276 }
277 }
278 }
279  
280 =item put LOCAL_FILE [, REMOTE_FILE]
281  
282 Uses scp to trasnfer LOCAL_FILE to the remote host. If a remote filename is
283 omitted, uses the basename of the local file.
284  
285 =cut
286  
287 0 ▶sub put {
Subroutine put: not called CC=6 SCAR=37.4
Pod documented
288 0 my($self, $local, $remote) = @_;
289 0 ▶ $remote ||= basename($local);
Condition0%0/3
$remote ||= basename($local)
l!l&&r!l&&!r
000
MC/DC0%0/2
$remote ||= basename($local)
$remote basename($local)
Truth table0%0/3
A ||= B
A= $remoteB= basename($local)
ABresult
1X1
011
000
290 0 ▶ $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} and not $remote =~ m[^/] 0 0 0
Condition0%0/3
$self->{"cwd"} and not $remote =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cwd"} and not $remote =~ m[^/]
$self->{"cwd"} not $remote =~ m[^/]
Truth table0%0/3
A and not(B)
A= $self->{"cwd"}B= not $remote =~ m[^/]
ABresult
0X0
100
111
291 0 my $dest = $self->{'host'}. ":$remote";
292 0 ▶ $dest = $self->{'user'}. '@'. $dest if $self->{'user'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user"} 0 0 0
293 0 ▶ warn "scp $local $dest\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::SCP::DEBUG 0 0 0
294 0 $self->scp($local, $dest);
295 }
296  
297 =item binary
298  
299 Compatibility method: does nothing; returns true.
300  
301 =cut
302  
303 0 ▶sub binary { 1; }
Subroutine binary: not called CC=1 SCAR=6.9
Pod documented
304  
305 =item quit
306  
307 Compatibility method: does nothing; returns true.
308  
309 =cut
310  
311 0 ▶sub quit { 1; }
Subroutine quit: not called CC=1 SCAR=6.9
Pod documented
312  
313 =back
314  
315 =head1 FREQUENTLY ASKED QUESTIONS
316  
317 Q: How do you supply a password to connect with ssh within a perl script
318 using the Net::SSH module?
319  
320 A: You don't (at least not with this module). Use RSA or DSA keys. See the
321 quick help in the next section and the ssh-keygen(1) manpage.
322  
323 A #2: See L<Net::SCP::Expect> instead.
324  
325 Q: My script is "leaking" scp processes.
326  
327 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
328 L<IPC::Open3> and L<perlfunc/waitpid>.
329  
330 =head1 GENERATING AND USING SSH KEYS
331  
332 =over 4
333  
334 =item 1 Generate keys
335  
336 Type:
337  
338 ssh-keygen -t rsa
339  
340 And do not enter a passphrase unless you wanted to be prompted for
341 one during file copying.
342  
343 Here is what you will see:
344  
345 $ ssh-keygen -t rsa
346 Generating public/private rsa key pair.
347 Enter file in which to save the key (/home/User/.ssh/id_rsa):
348 Enter passphrase (empty for no passphrase):
349  
350 Enter same passphrase again:
351  
352 Your identification has been saved in /home/User/.ssh/id_rsa.
353 Your public key has been saved in /home/User/.ssh/id_rsa.pub.
354 The key fingerprint is:
355 5a:cd:2b:0a:cd:d9:15:85:26:79:40:0c:55:2a:f4:23 User@JEFF-CPU
356  
357  
358 =item 2 Copy public to machines you want to upload to
359  
360 C<id_rsa.pub> is your public key. Copy it to C<~/.ssh> on target machine.
361  
362 Put a copy of the public key file on each machine you want to log into.
363 Name the copy C<authorized_keys> (some implementations name this file
364 C<authorized_keys2>)
365  
366 Then type:
367  
368 chmod 600 authorized_keys
369  
370 Then make sure your home dir on the remote machine is not group or
371 world writeable.
372  
373 =back
374  
375 =head1 AUTHORS
376  
377 Could really use a maintainer with enough time to at least review and apply
378 patches more patches. Or the module should just be deprecated in favor of
379 Net::SFTP::Expect or Net::SFTP::Foreign and made into a simple compatiblity
380 wrapper.
381  
382 Ivan Kohler <ivan-netscp_pod@420.am>
383  
384 Major updates Anthony Deaver <bishop@projectmagnus.org>
385  
386 Thanks to Jon Gunnip <jon@soundbite.com> for fixing a bug with size().
387  
388 Patch for the mkdir method by Anthony Awtrey <tony@awtrey.com>.
389  
390 Thanks to terrence brannon <tbone@directsynergy.com> for the documentation in
391 the GENERATING AND USING SSH KEYS section.
392  
393 =head1 COPYRIGHT
394  
395 Copyright (c) 2000 Ivan Kohler
396 Copyright (c) 2007 Freeside Internet Services, Inc.
397 All rights reserved.
398 This program is free software; you can redistribute it and/or modify it under
399 the same terms as Perl itself.
400  
401 =head1 BUGS
402  
403 Still has no-OO cruft.
404  
405 In order to work around some problems with commercial SSH2, if the source file
406 is on the local system, and is not a directory, the B<-r> flag is omitted.
407 It's probably better just to use OpenSSH <http://www.openssh.com/> which is
408 the de-facto standard these days anyway.
409  
410 The Net::FTP-style OO stuff is kinda lame. And incomplete.
411  
412 iscp doesnt expect you to be logging into the box that you are copying to
413 for the first time. so it's completely clueless about how to handle the
414 whole 'add this file to known hosts' message so it just hangs after the
415 user hits y. (Thanks to John L. Utz III). To avoid this, SSH to the box
416 once first.
417  
418 =head1 SEE ALSO
419  
420 For a perl implementation that does not require the system B<scp> command, see
421 L<Net::SFTP> instead.
422  
423 For a wrapper version that allows you to use passwords, see L<Net::SCP::Expect>
424 instead.
425  
426 For a wrapper version of the newer SFTP protocol, see L<Net::SFTP::Foreign>
427 instead.
428  
429 L<Net::SSH>, L<Net::SSH::Perl>, L<Net::SSH::Expect>, L<Net::SSH2>,
430 L<IPC::PerlSSH>
431  
432 scp(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>
433  
434 =cut
435  
436 1 1;
437  
438  
↑ Summary