ibra has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm very new to perl and would really appreciate some help.I have a script which transfers files to multiple remote hosts, without having to enter passwords. In my script the user can pass two arguments. The first argument is the directory or file test.txt in this example below. The second argument is the location on the remote hosts where I'd like the file to be placed. example: perl distributor.pl /home/user/testdir/test.txt /home/user/testdir/remotelocation/
#I have a hash for all the remote hosts my $remotehosts = { "78.example.com" => { user => 'user', host => '0.0.0.78', } "79.example.com" => { user => 'user', host => '0.0.0.79', } } $path = $ARGV[0]; $destination = $ARGV[1]; #the copy scp statement is in a while loop which loops through the key +s in the hash. $scp->scp($path, $remote->{user}.'@'.$remote->{host}.":".$destination)
At the moment when the user enters the location they would like to place the file. If the location doesn't exist a file will still be created. for example: perl distributor.pl /home/user/testdir/test.txt /home/user/test in the second argument if test directory doesn't exist on one of the remote host a file called test will be created. Is there a away to stop this from happening and inform the user that the directory they have typed doesn't exist? Thank you
  • Comment on Can you use Net::SCP to check if a remote dir exist before copying?
  • Download Code

Replies are listed 'Best First'.
Re: Can you use Net::SCP to check if a remote dir exist before copying?
by salva (Canon) on Apr 26, 2011 at 11:02 UTC
    ... use File::Basename; my $file = fileparse($path); $scp->scp($path, "$remote->{user}\@$remote->{host}:$destination/$file" +);
    Or using Net::OpenSSH::Parallel:
    use Net::OpenSSH::Parallel; my $pssh = Net::OpenSSH::Parallel->new; for my $host (keys %$remotehosts) { my $remote = ...; $pssh->add_host($host => "$remote->{user}\@$remote->{host}"); } $pssh->push('*', command => 'test', '-d', $destination); $pssh->push('*', scp_put => $path, $destination); $pssh->run; for my $host (keys %$remotehosts) { $pssh->get_error($host) and print STDERR "Unable to copy file $path +to $host" }