SCP(3pm)
π SCP β Net::SCP π
okay bestie, sit DOWN. this is the manual page for Net::SCP, the Perl extension for the secure copy protocol, from the 3pm section (that's perl module land, iykyk). it's basically a cute lil' wrapper around ssh and scp so your Perl script can yeet files across the network without you hand-rolling the shell commands. let's get into it π¦
β¨ NAME β¨
Net::SCP β Perl extension for secure copy protocol π
πΈ SYNOPSIS πΈ
okay so here's the vibe, all the ways you can use it:
#procedural interface
use 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");
π DESCRIPTION π
Simple wrappers around ssh and scp commands. that's it, that's the whole thing. no cap. π«Ά
β SUBROUTINES β
scp SOURCE, DESTINATION
so you can call this either as a plain subroutine or as a method β but bestie, heads up: the subroutine interface is deprecated. π
it calls scp in batch mode, with the -B -p -q and -r options. if something goes wrong it returns false, and you can grab the text error message out of $scp->{errstr}.
returns false and sets the errstr attribute if there is an error.
iscp SOURCE, DESTINATION
same deal β callable as a subroutine or as a method, and again the subroutine interface is deprecated. π
this one prints the scp command that's about to be executed, waits for you to confirm, and (optionally) executes scp, with the -p and -r flags. interactive queen behavior β¨
returns false and sets the errstr attribute 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 you may optionally hand over a user. or, alternatively, you can pass a hashref of named params, with these keys:
host - hostname
user - username
interactive - bool
cwd - current working directory on remote server
login [USER]
compatibility method. optionally sets the user. lowkey just vibing there. π
cwd CWD
sets the cwd β which gets used for a subsequent get or put request that doesn't come with a full pathname.
get REMOTE_FILE [, LOCAL_FILE]
uses scp to transfer REMOTE_FILE from the remote host. if you don't give it a local filename, it uses the basename of the remote file. π
mkdir DIRECTORY
makes a directory on the remote server. returns false and sets the errstr attribute 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 errstr attribute. and here's the spicy bit π β 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. yeah. it's giving "technically correct" and we love that for it.
(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 transfer LOCAL_FILE to the remote host. if you don't give it a remote filename, it uses the basename of the local file. π¦
binary
compatibility method: does nothing; returns true. iconic no-op, honestly. π
quit
compatibility method: does nothing; returns true. same energy. π
π¬ 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. π§ no zombie apocalypse today, bestie.
π GENERATING AND USING SSH KEYS π
1. Generate keys
type:
ssh-keygen -t rsa
and do not enter a passphrase unless you actually wanted to be prompted for one during file copying. fr fr, think about it first.
here's what you will see:
$ ssh-keygen -t rsa
Generating 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 the 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_keys
then make sure your home dir on the remote machine is not group or world writeable. π βοΈ seriously, don't skip that part.
π«Ά AUTHORS π«Ά
could really use a maintainer with enough time to at least review and apply patches more patches. or the module should just be deprecated in favor of Net::SFTP::Expect or Net::SFTP::Foreign and made into a simple compatibility wrapper. π (not me reading this and feeling things)
- Ivan Kohler
<ivan-netscp_pod@420.am> - Major updates: Anthony Deaver
<bishop@projectmagnus.org> - thanks to Jon Gunnip
<jon@soundbite.com>for fixing a bug withsize(). - patch for the
mkdirmethod by Anthony Awtrey<tony@awtrey.com>. - thanks to terrence brannon
<tbone@directsynergy.com>for the documentation in the GENERATING AND USING SSH KEYS section. π
π COPYRIGHT π
Copyright (c) 2000 Ivan Kohler Copyright (c) 2007 Freeside Internet Services, Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π BUGS π
still has no-OO cruft. π
in order to work around some problems with commercial SSH2, if the source file is on the local system, and is not a directory, the-rflag is omitted. it's probably better just to use OpenSSHhttp://www.openssh.com/which is the de-facto standard these days anyway.
the Net::FTP-style OO stuff is kinda lame. and incomplete. π
iscp doesn't expect you to be logging into the box that you are copying to for the first time. so it's completely clueless about how to handle the whole 'add this file to known hosts' message so it just hangs after the user hits y. (Thanks to John L. Utz III). to avoid this, SSH to the box once first.
π SEE ALSO π
- for a perl implementation that does not require the system
scpcommand, see Net::SFTP instead. - for a wrapper version that allows you to use passwords, see Net::SCP::Expect instead.
- for a wrapper version of the newer SFTP protocol, see Net::SFTP::Foreign instead.
Net::SSH,Net::SSH::Perl,Net::SSH::Expect,Net::SSH2,IPC::PerlSSHscp(1),ssh(1),IO::File,IPC::Open2,IPC::Open3
and that's the whole page, bestie π this little doc came to you courtesy of perl v5.26.2, dated 2018-07-31. go forth and copy securely, and remember: keys, not passwords. periodt. πβ¨
π¦ where this page comes from
- debian bookworm: libnet-scp-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libnet-scp-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libnet-scp-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libnet-scp-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms