Catching errors in SCP or SFTP

让人想犯罪 __ 提交于 2019-12-08 06:45:57

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/8944534/catching-errors-in-scp-or-sftp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!