in reply to Net::SCP::Expect Error Handling

Net::SCP::Expect documentation says nothing about error handling, but looking at the source code, you would see that its method scp throws and exception when authentication fails in any way or when the slave scp process exists with a non-zero return code.

The Expect object used internally to handle interaction with the slave scp process is not exposed. I can't see a way to retrieve the output.

Anyway, let my present you Net::OpenSSH which is able to do what you are asking for:

use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host, user => $user, password => $pass); unless ($ssh->scp_get({ stdout_file => './scp-capture', stderr_to_stdout => 1 }, $src_path, $dst_path)) { say "SCP failed: " . $ssh->error; ... }

update: oops! s/stdin/stdout/g !!! There was also a missing parenthesis.

Replies are listed 'Best First'.
Re^2: Net::SCP::Expect Error Handling
by siddhanta (Initiate) on Jan 29, 2016 at 08:43 UTC

    Thanks. Can you please explain the code. especially stdin functionality.

    In the mean time , i have used the eval function to catch the error

    #!/usr/bin/perl use Net::SCP::Expect; my $user = 'user1'; my $pass = 'pass1'; my $host = 'host1'; my $src_path = "/home/sid.txt"; my $dst_path = "/home/datprd/"; my $s = Net::SCP::Expect->new; $s->login($user, $pass); eval{ $s->scp("$host:$src_path",$dst_path);}; print $@;
      Under the hood the scp methods in Net::OpenSSH call the scp command.

      The option stdout_file indicates a file name where to redirect the output from the command. stderr_to_stdout indicates that stderr should be send to the same place as stdout. That combination is equivalent to the shell >./scp-capture 2>&1.

        I have executed the command. and its working fine. It also display the error message. It creates the scp-capture file but does not write anything to it.

        Look like there is some syntax error with your command. Can you please check.