Sree
Sree

Reputation: 347

SCP files matching pattern using Perl

Is there a way to scp multiple files matching a wildcard pattern using Perl?

say server A has curr directory contains .pdf and .txt files and I want to move just the .pdf files to server B under /home/userid I used Net::SCP, but I see there is no bulk copy as in normal scp command eg. scp *.pdf userid@serverB

>cat test.pl
use Net::SCP;
$scp = Net::SCP->new( "serverB", "userid" );
$scp->put("test.pdf") or die $scp->{errstr};

I want to change to $scp->put("*.pdf") or die $scp->{errstr}; If I change to *.pdf perl throws error

*.pdf: No such file or directory

I already created passwordless login. Is there a way to do bulk copy of matching pattern files ?

Upvotes: 2

Views: 831

Answers (2)

salva
salva

Reputation: 10242

Using Net::OpenSSH:

use Net::OpenSSH;
my $ssh = Net::OpenSSH->new('serverB', user => 'userid');
$ssh->scp_put({glob => 1}, '*.pdf', '.') or die $ssh->error;

Or, if you are running that script in Windows, Net::SSH::Any provides an identical interface:

use Net::SSH::Any;
my $ssh = Net::SSH::Any->new('serverB', user => 'userid');
$ssh->scp_put({glob => 1}, '*.pdf', '.') or die $ssh->error;

Upvotes: 2

ChatterOne
ChatterOne

Reputation: 3541

The * character is a wildcard that is expanded by your shell (and different shells may have different expansion rules), so SSH (or SCP) knows nothing about it.

Also, SSH does not have "integrated" support for listing files in directories. If you want to list the files, you'll have to either go the "ugly way", by doing a remote ls *.pdf (by using Net::SSH::Perl) , or switch to something different like Net::SFTP, which supports an opendir function.

Upvotes: 1

Related Questions