1

I am writing a script that will transfer a file from one server to another using SCP or SFTP. It is very important for me to differentiate between different types of errors that occur.

My problem, is that the error code returned from SFTP and (especially) SCP does not seem to distinguish between different types of errors.

For example, when SCP-ing, it seems as if I get an error code of 1, no matter what type of error is actually occurring (ex: perrmission denied, could not connect to host both return error code 1).

For SFTP or SCP, is there a way to reliably determine an error that is occurring; without having to parse through $stderr and extract the error that way?

user1160958
  • 193
  • 1
  • 4
  • 13
  • With the perl module and the ->new method, please note that whether you have a host that cannot be found (STDERR spits out, "ssh: Could not resolve hostname xxxx.yyy.com: Name or service not known") or a Credentials problem (STDERR spits out, "Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)." Your errors above, do not relate to the actual errors (or expected values in Net::SFTP::Foreign::Constants). Although the strings are different, strangely the constants (which you can determine with a printf and %d; in both cases are 37 which relates to the latter of the following erro – Samiam104 Dec 24 '18 at 22:27

1 Answers1

0

Yes, use some scripting language (i.e. Perl, Python, Ruby, etc.) and some SFTP module that can return that information.

For instance:

#!/usr/bin/perl
use Net::SFTP::Foreign;
my $sftp = Net::SFTP::Foreign->new($host);
$sftp->error and die "SFTP failed: " . $sftp->error;
my $sftp->put("foo", "bar");
$sftp->error and die "put failed: " . $sftp->error;
salva
  • 9,943
  • 4
  • 29
  • 57
  • 1
    Thanks! I actually ended up writing a function that gets the stdin, stdout, stderr and return codes of a command over net:ssh. I used net_ssh_object.open_channel, and doing a channel.exec(command). But I will definitely check this way out. – user1160958 Mar 16 '12 at 14:36