4 use vars qw($VERSION @ISA @EXPORT_OK $scp $DEBUG);
8 use String::ShellQuote;
10 use Net::SSH qw(sshopen3);
14 @EXPORT_OK = qw( scp iscp );
23 Net::SCP - Perl extension for secure copy protocol
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
34 $scp = Net::SCP->new( "hostname", "username" );
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};
41 $scp->scp($source, $destination);
44 $scp = Net::SCP->new("hostname");
52 Simple wrappers around ssh and scp commands.
58 =item scp SOURCE, DESTINATION
60 Can be called either as a subroutine or a method; however, the subroutine
61 interface is depriciated.
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
67 Returns false and sets the B<errstr> attribute if there is an error.
72 my $self = ref($_[0]) ? shift : {};
73 my($src, $dest, $interact) = @_;
75 $flags .= 'r' unless &_islocal($src) && ! -d $src;
77 if ( ( defined($interact) && $interact )
78 || ( defined($self->{interactive}) && $self->{interactive} ) ) {
79 @cmd = ( $scp, $flags, $src, $dest );
80 print join(' ', @cmd), "\n";
82 $self->{errstr} = "User declined";
87 @cmd = ( $scp, $flags, $src, $dest );
89 my($reader, $writer, $error ) =
90 ( new IO::Handle, new IO::Handle, new IO::Handle );
91 $writer->autoflush(1);# $error->autoflush(1);
92 local $SIG{CHLD} = 'DEFAULT';
93 my $pid = open3($writer, $reader, $error, @cmd );
96 my $errstr = join('', <$error>);
97 #chomp(my $errstr = <$error>);
98 $self->{errstr} = $errstr;
105 =item iscp SOURCE, DESTINATION
107 Can be called either as a subroutine or a method; however, the subroutine
108 interface is depriciated.
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.
113 Returns false and sets the B<errstr> attribute if there is an error.
120 $self->{'interactive'} = 1;
128 print "Proceed [y/N]:";
129 my $x = scalar(<STDIN>);
143 =item new HOSTNAME [ USER ] | HASHREF
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:
152 cwd - current working directory on remote server
158 my $class = ref($proto) || $proto;
165 'user' => ( scalar(@_) ? shift : '' ),
170 bless($self, $class);
175 Compatibility method. Optionally sets the user.
180 my($self, $user) = @_;
181 $self->{'user'} = $user if $user;
186 Sets the cwd (used for a subsequent get or put request without a full pathname).
191 my($self, $cwd) = @_;
192 $self->{'cwd'} = $cwd || '/';
195 =item get REMOTE_FILE [, LOCAL_FILE]
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.
203 my($self, $remote, $local) = @_;
204 $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
205 $local ||= basename($remote);
206 my $source = $self->{'host'}. ":$remote";
207 $source = $self->{'user'}. '@'. $source if $self->{'user'};
208 $self->scp($source,$local);
211 =item mkdir DIRECTORY
213 Makes a directory on the remote server. Returns false and sets the B<errstr>
216 (Implementation note: An ssh connection is established to the remote machine
217 and '/bin/mkdir B<-p>' is used to create the directory.)
222 my($self, $directory) = @_;
223 $directory = $self->{'cwd'}. "/$directory"
224 if $self->{'cwd'} && $directory !~ /^\//;
225 my $host = $self->{'host'};
226 $host = $self->{'user'}. '@'. $host if $self->{'user'};
227 my($reader, $writer, $error ) =
228 ( new IO::Handle, new IO::Handle, new IO::Handle );
229 $writer->autoflush(1);
230 my $pid = sshopen3( $host, $writer, $reader, $error,
231 '/bin/mkdir', '-p ', shell_quote($directory) );
234 chomp(my $errstr = <$error> || '');
235 $self->{errstr} = $errstr || "mkdir exited with status ". ($?>>8);
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.
248 (Implementation note: An ssh connection is established to the remote machine
249 and wc is used to determine the file size.)
254 my($self, $file) = @_;
255 $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
256 my $host = $self->{'host'};
257 $host = $self->{'user'}. '@'. $host if $self->{'user'};
258 my($reader, $writer, $error ) =
259 ( new IO::Handle, new IO::Handle, new IO::Handle );
260 $writer->autoflush(1);
261 #sshopen2($host, $reader, $writer, 'wc', '-c ', shell_quote($file) );
263 sshopen3($host, $writer, $reader, $error, 'wc', '-c ', shell_quote($file) );
266 chomp(my $errstr = <$error>);
267 $self->{errstr} = $errstr || "wc exited with status ". $?>>8;
270 chomp( my $size = <$reader> || 0 );
271 if ( $size =~ /^\s*(\d+)/ ) {
274 $self->{errstr} = "unparsable output from remote wc: $size";
280 =item put LOCAL_FILE [, REMOTE_FILE]
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.
288 my($self, $local, $remote) = @_;
289 $remote ||= basename($local);
290 $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
291 my $dest = $self->{'host'}. ":$remote";
292 $dest = $self->{'user'}. '@'. $dest if $self->{'user'};
293 warn "scp $local $dest\n" if $DEBUG;
294 $self->scp($local, $dest);
299 Compatibility method: does nothing; returns true.
307 Compatibility method: does nothing; returns true.
315 =head1 FREQUENTLY ASKED QUESTIONS
317 Q: How do you supply a password to connect with ssh within a perl script
318 using the Net::SSH module?
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.
323 A #2: See L<Net::SCP::Expect> instead.
325 Q: My script is "leaking" scp processes.
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>.
330 =head1 GENERATING AND USING SSH KEYS
334 =item 1 Generate keys
340 And do not enter a passphrase unless you wanted to be prompted for
341 one during file copying.
343 Here is what you will see:
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):
350 Enter same passphrase again:
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
358 =item 2 Copy public to machines you want to upload to
360 C<id_rsa.pub> is your public key. Copy it to C<~/.ssh> on target machine.
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
368 chmod 600 authorized_keys
370 Then make sure your home dir on the remote machine is not group or
377 Could really use a maintainer with enough time to at least review and apply
378 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
382 Ivan Kohler <ivan-netscp_pod@420.am>
384 Major updates Anthony Deaver <bishop@projectmagnus.org>
386 Thanks to Jon Gunnip <jon@soundbite.com> for fixing a bug with size().
388 Patch for the mkdir method by Anthony Awtrey <tony@awtrey.com>.
390 Thanks to terrence brannon <tbone@directsynergy.com> for the documentation in
391 the GENERATING AND USING SSH KEYS section.
395 Copyright (c) 2000 Ivan Kohler
396 Copyright (c) 2007 Freeside Internet Services, Inc.
398 This program is free software; you can redistribute it and/or modify it under
399 the same terms as Perl itself.
403 Still has no-OO cruft.
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.
410 The Net::FTP-style OO stuff is kinda lame. And incomplete.
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
420 For a perl implementation that does not require the system B<scp> command, see
421 L<Net::SFTP> instead.
423 For a wrapper version that allows you to use passwords, see L<Net::SCP::Expect>
426 For a wrapper version of the newer SFTP protocol, see L<Net::SFTP::Foreign>
429 L<Net::SSH>, L<Net::SSH::Perl>, L<Net::SSH::Expect>, L<Net::SSH2>,
432 scp(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>