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?
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;