4 use vars qw($VERSION @ISA @EXPORT_OK $scp);
7 use String::ShellQuote;
9 use Net::SSH qw(sshopen3);
12 @EXPORT_OK = qw( scp iscp );
19 Net::SCP - Perl extension for secure copy protocol
24 use Net::SCP qw(scp iscp);
25 scp($source, $destination);
26 iscp($source, $destination); #shows command, asks for confirmation, and
27 #allows user to type a password on tty
30 $scp = Net::SCP->new("hostname");
39 Simple wrappers around ssh and scp commands.
45 =item scp SOURCE, DESTINATION
47 Calls scp in batch mode, with the B<-B> B<-p> B<-q> and B<-r> options.
54 $flags .= 'r' unless &_islocal($src) && ! -d $src;
55 my @cmd = ( $scp, $flags, $src, $dest );
59 =item iscp SOURCE, DESTINATION
61 Prints the scp command to be execute, waits for the user to confirm, and
62 (optionally) executes scp, with the B<-p> and B<-r> flags.
69 $flags .= 'r' unless &_islocal($src) && ! -d $src;
70 my @cmd = ( $scp, $flags, $src, $dest );
71 print join(' ', @cmd), "\n";
78 print "Proceed [y/N]:";
79 my $x = scalar(<STDIN>);
95 This is the constructor for a new Net::SCP object. Additional parameters
102 my $class = ref($proto) || $proto;
108 bless($self, $class);
113 Compatibility method. Optionally sets the user.
118 my($self, $user) = @_;
119 $self->{'user'} = $user;
124 Sets the cwd (used for a subsequent get or put request without a full pathname).
129 my($self, $cwd) = @_;
130 $self->{'cwd'} = $cwd || '/';
133 =item get REMOTE_FILE [, LOCAL_FILE]
135 Uses scp to transfer REMOTE_FILE from the remote host. If a local filename is
136 omitted, uses the basename of the remote file.
141 my($self, $remote, $local) = @_;
142 $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
143 $local ||= basename($remote);
144 my $source = $self->{'host'}. ":$remote";
145 $source = $self->{'user'}. '@'. $source if $self->{'user'};
151 Returns the size in bytes for the given file as stored on the remote server.
153 (Implementation note: An ssh connection is established to the remote machine
154 and wc is used to determine the file size. No distinction is currently made
155 between nonexistant and zero-length files.)
160 my($self, $file) = @_;
161 $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
162 my $host = $self->{'host'};
163 $host = $self->{'user'}. '@'. $host if $self->{'user'};
164 my($reader, $writer, $error ) =
165 ( new IO::Handle, new IO::Handle, new IO::Handle );
166 $writer->autoflush(1);# $error->autoflush(1);
167 #sshopen2($host, $reader, $writer, 'wc', '-c ', shell_quote($file) );
168 sshopen3($host, $writer, $reader, $error, 'wc', '-c ', shell_quote($file) );
169 chomp( my $size = <$reader> || 0 );
170 if ( $size =~ /^\s+(\d+)/ ) {
173 warn "unparsable output from remote wc";
178 =item put LOCAL_FILE [, REMOTE_FILE]
180 Uses scp to trasnfer LOCAL_FILE to the remote host. If a remote filename is
181 omitted, uses the basename of the local file.
186 my($self, $local, $remote) = @_;
187 $remote ||= basename($local);
188 $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
189 my $dest = $self->{'host'}. ":$remote";
190 $dest = $self->{'user'}. '@'. $dest if $self->{'user'};
191 warn "scp $local $dest\n";
197 Compatibility method: does nothing; returns true.
207 Ivan Kohler <ivan-netscp_pod@420.am>
213 In order to work around some problems with commercial SSH2, if the source file
214 is on the local system, and is not a directory, the B<-r> flag is omitted.
216 It's probably better just to use SSH1 or OpenSSH <http://www.openssh.com/>
218 The Net::FTP-style OO stuff is kinda lame. And incomplete.