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

I am trying to SCP from one machine to multiple machines. I have an identity file to use for performing passwordless authentication. I came across Net::SCP::Expect module and it does the job well for one machine but when it moves on to copying the component to another machine, it fails. Here's the code:
use strict; use warnings; use Cwd; use File::Copy; use Getopt::Long; use Net::SSH::Perl; use Net::SCP::Expect; my ($war_name, $wrapper_name, $host, $user, $war_build_path, $war_full +_path, $war_archive_path, $war_archive_host, $HOME, $scpe, @id_file); usage() if ( @ARGV < 6 or !GetOptions( 'war_name:s' => \$war_name, 'war_path:s' => \$war_build_path, 'wrapper_name:s' => \$wrapper_name, 'host=s' => \$host, 'user=s' => \$user, ) ); if ($war_name && $war_build_path && $host && $user) { printf "Initiating subroutine to copy war on remote machine...\n"; &copy_war; } sub copy_war { $war_full_path = "/home/tom/slave/workspace/Rel_Build/rec_dist/$wa +r_name"; $war_archive_path = "/home/tom"; $war_archive_host = "xx.xxx.xxx.xx"; if ( -f $war_full_path ) { printf "Copying war: $war_name\n"; printf "Target Machine: $host\n"; printf "User: $user\n\n"; $scpe = Net::SCP::Expect->new( identity_file => "/home/tom/.ssh/i +d_file", host => $host, user => $user, ); $scpe->scp("$war_full_path", "$HOME") or die $scpe->{errstr}; printf "War copied on deployment machine. Now moving on to nex +t task of archiving the war...\n"; $scpe = Net::SCP::Expect->new( identity_file => "/home/tom/.ssh/i +d_file", host => $war_archive_host, user => $user, ); $scpe->scp("$war_full_path", "$war_archive_path") or die $scpe +->{errstr}; printf "War archived on master\n"; } else { printf "War not found\n"; } }

Output:

Initiating subroutine to copy war on remote machine... Copying war: remote-web.war Target Machine: xx.xxx.xxx.xx User: tom Use of uninitialized value $HOME in string at /home/tom/deploy.pl line + 59. War copied on deployment machine. Now moving on to next task of archiv +ing the war... at /home/tom/perl/5.18.2/lib/site_perl/5.18.2/Expect.pm line 760. I tried reading Expect.pm file around the error # but couldn't underst +and. Does it has anything to do with the earlier object not getting f +reed up? I couldn't find any method on the CPAN page for this module +to close the connection.

Replies are listed 'Best First'.
Re: Unable to do SCP on multiple machines using Perl's Net::SCP::Expect module
by wjw (Priest) on May 02, 2014 at 15:28 UTC
    Couple of questions:

    • I don't see where $HOME is ever getting set, so your destination is ??
    • Use of uninitialized value $HOME in string at /home/tom/deploy.pl line 59 I take it this is not all your code?
    • to clarify, both the transfer and the archive verified to work on the remote machine?

    I would look at the notes section of Net::SCP::Expect and consider what they say.

    I admit this was a cursory look, and I can't compile right now because Net::SSH::Perl won't install due to a missing gmp.h... . Hope you find something useful there...

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...

      * I don't see where $HOME is ever getting set, so your destination is??

      --> To be honest, I did not really look at it very seriously because the file was getting copied at the desired location i.e., in the home dir of $user. After i changed it to $ENV{'HOME'}, even the first SCP is not working now. Now i get the following:
      Initiating subroutine to copy war on remote machine... Copying war: remote-web.war Target Machine: xx.xxx.xxx.xx User: tom at /home/tom/perl/5.18.2/lib/site_perl/5.18.2/Expect.pm line 760.ar: P +ermission denied

      * Use of uninitialized value $HOME in string at /home/tom/deploy.pl line 59 I take it this is not all your code?

      --> Yes, it indeed isn't all my code. I only pasted the relevant portions just to avoid cluttering.

      * to clarify, both the transfer and the archive verified to work on the remote machine?

      --> Yes, everything is working if i use the command line.
Re: Unable to do SCP on multiple machines using Perl's Net::SCP::Expect module
by salva (Canon) on May 02, 2014 at 22:27 UTC
      Thanks for the suggestion but i have to use identity file and i could not find any option for specifying the same (while creating connection) in either of the modules.
        key_path