4 use vars qw($VERSION @ISA @EXPORT_OK $scp);
8 use String::ShellQuote;
10 use Net::SSH qw(sshopen3);
14 @EXPORT_OK = qw( scp iscp );
21 Net::SCP - Perl extension for secure copy protocol
26 use Net::SCP qw(scp iscp);
27 scp($source, $destination);
28 iscp($source, $destination); #shows command, asks for confirmation, and
29 #allows user to type a password on tty
32 $scp = Net::SCP->new( "hostname", "username" );
34 $scp = Net::SCP->new( { "host"=>$hostname, "user"=>$username } );
35 $scp->get("filename") or die $scp->{errstr};
36 $scp->put("filename") or die $scp->{errstr};
39 $scp->scp($source, $destination);
42 $scp = Net::SCP->new("hostname");
51 Simple wrappers around ssh and scp commands.
57 =item scp SOURCE, DESTINATION
59 Can be called either as a subroutine or a method; however, the subroutine
60 interface is depriciated.
62 Calls scp in batch mode, with the B<-B> B<-p> B<-q> and B<-r> options.
63 Returns false upon error, with a text error message accessable in
66 Returns false and sets the B<errstr> attribute if there is an error.
71 my $self = ref($_[0]) ? shift : {};
72 my($src, $dest, $interact) = @_;
74 $flags .= 'r' unless &_islocal($src) && ! -d $src;
76 if ( ( defined($interact) && $interact )
77 || ( defined($self->{interact}) && $self->{interact} ) ) {
78 @cmd = ( $scp, $flags, $src, $dest );
79 print join(' ', @cmd), "\n";
81 $self->{errstr} = "User declined";
86 @cmd = ( $scp, $flags, $src, $dest );
88 my($reader, $writer, $error ) =
89 ( new IO::Handle, new IO::Handle, new IO::Handle );
90 $writer->autoflush(1);# $error->autoflush(1);
91 my $pid = open3($writer, $reader, $error, @cmd );
94 my $errstr = join('', <$error>);
95 #chomp(my $errstr = <$error>);
96 $self->{errstr} = $errstr;
103 =item iscp SOURCE, DESTINATION
105 Can be called either as a subroutine or a method; however, the subroutine
106 interface is depriciated.
108 Prints the scp command to be execute, waits for the user to confirm, and
109 (optionally) executes scp, with the B<-p> and B<-r> flags.
111 Returns false and sets the B<errstr> attribute if there is an error.
118 $self->{'interact'} = 1;
126 print "Proceed [y/N]:";
127 my $x = scalar(<STDIN>);
141 =item new HOSTNAME [ USER ] | HASHREF
143 This is the constructor for a new Net::SCP object. You must specify a
144 hostname, and may optionally provide a user. Alternatively, you may pass a
145 hashref of named params, with the following keys:
150 cwd - current working directory on remote server
156 my $class = ref($proto) || $proto;
163 'user' => ( scalar(@_) ? shift : '' ),
168 bless($self, $class);
173 Compatibility method. Optionally sets the user.
178 my($self, $user) = @_;
179 $self->{'user'} = $user if $user;
184 Sets the cwd (used for a subsequent get or put request without a full pathname).
189 my($self, $cwd) = @_;
190 $self->{'cwd'} = $cwd || '/';
193 =item get REMOTE_FILE [, LOCAL_FILE]
195 Uses scp to transfer REMOTE_FILE from the remote host. If a local filename is
196 omitted, uses the basename of the remote file.
201 my($self, $remote, $local) = @_;
202 $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
203 $local ||= basename($remote);
204 my $source = $self->{'host'}. ":$remote";
205 $source = $self->{'user'}. '@'. $source if $self->{'user'};
211 Returns the size in bytes for the given file as stored on the remote server.
212 Returns 0 on error, and sets the B<errstr> attribute. In the case of an actual
213 zero-length file on the remote server, the special value '0e0' is returned,
214 which evaluates to zero when used as a number, but is true.
216 (Implementation note: An ssh connection is established to the remote machine
217 and wc is used to determine the file size.)
222 my($self, $file) = @_;
223 $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
224 my $host = $self->{'host'};
225 $host = $self->{'user'}. '@'. $host if $self->{'user'};
226 my($reader, $writer, $error ) =
227 ( new IO::Handle, new IO::Handle, new IO::Handle );
228 $writer->autoflush(1);# $error->autoflush(1);
229 #sshopen2($host, $reader, $writer, 'wc', '-c ', shell_quote($file) );
231 sshopen3($host, $writer, $reader, $error, 'wc', '-c ', shell_quote($file) );
234 chomp(my $errstr = <$error>);
235 $self->{errstr} = $errstr || "wc exited with status ". $?>>8;
238 chomp( my $size = <$reader> || 0 );
239 if ( $size =~ /^\s*(\d+)/ ) {
242 $self->{errstr} = "unparsable output from remote wc: $size";
248 =item put LOCAL_FILE [, REMOTE_FILE]
250 Uses scp to trasnfer LOCAL_FILE to the remote host. If a remote filename is
251 omitted, uses the basename of the local file.
256 my($self, $local, $remote) = @_;
257 $remote ||= basename($local);
258 $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
259 my $dest = $self->{'host'}. ":$remote";
260 $dest = $self->{'user'}. '@'. $dest if $self->{'user'};
261 warn "scp $local $dest\n";
267 Compatibility method: does nothing; returns true.
275 =head1 FREQUENTLY ASKED QUESTIONS
277 Q: How do you supply a password to connect with ssh within a perl script
278 using the Net::SSH module?
280 A: You don't. Use RSA or DSA keys. See the ssh-keygen(1) manpage.
282 Q: My script is "leaking" ssh processes.
284 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
285 L<IPC::Open3> and L<perlfunc/waitpid>.
289 Ivan Kohler <ivan-netscp_pod@420.am>
290 Anthony Deaver <bishop@projectmagnus.org>
292 Thanks to Jon Gunnip <jon@soundbite.com> for fixing a bug with size().
296 Copyright (c) 2000 Ivan Kohler.
297 Copyright (c) 2000 Silicon Interactive Software Design.
298 Copyright (c) 2000 Freeside Internet Services, LLC
300 This program is free software; you can redistribute it and/or modify it under
301 the same terms as Perl itself.
305 Still has no-OO cruft.
307 In order to work around some problems with commercial SSH2, if the source file
308 is on the local system, and is not a directory, the B<-r> flag is omitted.
310 It's probably better just to use SSH1 or OpenSSH <http://www.openssh.com/>
312 The Net::FTP-style OO stuff is kinda lame. And incomplete.