Net::SCP3pm
Perl extension for secure copy protocol
Usage
#procedural interfaceuse Net::SCP qw(scp iscp);scp($source, $destination);iscp($source, $destination); #shows command, asks for confirmation, and#allows user to type a password on tty#OO interface$scp = Net::SCP->new( "hostname", "username" );#with named params$scp = Net::SCP->new( { "host"=>$hostname, "user"=>$username } );$scp->get("filename") or die $scp->{errstr};$scp->put("filename") or die $scp->{errstr};#tmtowtdi$scp = new Net::SCP;$scp->scp($source, $destination);#Net::FTP-style$scp = Net::SCP->new("hostname");$scp->login("user");$scp->cwd("/dir");$scp->size("file");$scp->get("file");
Overview
Simple wrappers around ssh and scp commands.
Subroutines
- scp SOURCE, DESTINATION
-
Can be called either as a subroutine or a method; however, the subroutine interface is deprecated.
Calls scp in batch mode, with the
-B-p-qand-roptions. Returns false upon error, with a text error message accessible in$scp->{errstr}.Returns false and sets the
errstrattribute if there is an error.
- iscp SOURCE, DESTINATION
-
Can be called either as a subroutine or a method; however, the subroutine interface is deprecated.
Prints the scp command to be execute, waits for the user to confirm, and (optionally) executes scp, with the
-pand-rflags.Returns false and sets the
errstrattribute if there is an error.
Methods
- new HOSTNAME [ USER ] | HASHREF
-
This is the constructor for a new Net::SCP object. You must specify a hostname, and may optionally provide a user. Alternatively, you may pass a hashref of named params, with the following keys:
host - hostnameuser - usernameinteractive - boolcwd - current working directory on remote server
- login [USER]
-
Compatibility method. Optionally sets the user.
- cwd CWD
-
Sets the cwd (used for a subsequent get or put request without a full pathname).
- get REMOTE_FILE [, LOCAL_FILE]
-
Uses scp to transfer REMOTE_FILE from the remote host. If a local filename is omitted, uses the basename of the remote file.
- mkdir DIRECTORY
-
Makes a directory on the remote server. Returns false and sets the
errstrattribute on errors.(Implementation note: An ssh connection is established to the remote machine and '/bin/mkdir
-p' is used to create the directory.)
- size FILE
-
Returns the size in bytes for the given file as stored on the remote server. Returns 0 on error, and sets the
errstrattribute. In the case of an actual zero-length file on the remote server, the special value '0e0' is returned, which evaluates to zero when used as a number, but is true.(Implementation note: An ssh connection is established to the remote machine and wc is used to determine the file size.)
- put LOCAL_FILE [, REMOTE_FILE]
-
Uses scp to trasnfer LOCAL_FILE to the remote host. If a remote filename is omitted, uses the basename of the local file.
- binary
-
Compatibility method: does nothing; returns true.
- quit
-
Compatibility method: does nothing; returns true.
Frequently asked questions
Q: How do you supply a password to connect with ssh within a perl script using the Net::SSH module?
A: You don't (at least not with this module). Use RSA or DSA keys. See the
quick help in the next section and the ssh-keygen(1) manpage.
A #2: See Net::SCP::Expect instead.
Q: My script is "leaking" scp processes.
A: See "How do I avoid zombies on a Unix system" in perlfaq8, IPC::Open2, IPC::Open3 and "waitpid" in perlfunc.
Generating and using SSH keys
- 1 Generate keys
-
Type:
ssh-keygen -t rsaAnd do not enter a passphrase unless you wanted to be prompted for one during file copying.
Here is what you will see:
$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/home/User/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/User/.ssh/id_rsa.Your public key has been saved in /home/User/.ssh/id_rsa.pub.The key fingerprint is:5a:cd:2b:0a:cd:d9:15:85:26:79:40:0c:55:2a:f4:23 User@JEFF-CPU
- 2 Copy public to machines you want to upload to
-
"id_rsa.pub"is your public key. Copy it to"~/.ssh"on target machine.Put a copy of the public key file on each machine you want to log into. Name the copy
"authorized_keys"(some implementations name this file"authorized_keys2")Then type:
chmod 600 authorized_keysThen make sure your home dir on the remote machine is not group or world writeable.
Releases
11 releases since Debian 3.1 (Sarge); the text changed 3 times.