Reputation: 51
I am using Net::OpenSSH module to make an ssh connection using ssh key authentication.
my $ssh_handle = Net::OpenSSH->new(user_id@host_name)
I am trying to capture different exception scenarios that may occur during an ssh connection attempt. I am using Try::Tiny for that.
In particular, I am trying to capture scenario where a wrong hostname/IP address is passed in the parameter. In this scenario, Net::OpenSSH module throws the following error in the console "ssh: Could not resolve hostname x.x.x.: Name or service not known".
I know that $ssh_handle->error gives the error message (in case of any error) after a connection attempt. But in case of a wrong hostname/IP scenario, it contains a generic message "unable to establish master SSH connection: master process exited unexpectedly". I believe this is a common error message for many error scenarios. So I can not pin poing "wrong hostname" error.
Is there any way to capture the error string "ssh: Could not resolve hostname x.x.x.: Name or service not known" ?
Upvotes: 1
Views: 639
Reputation: 10234
Redirect the master SSH stderr stream to some file and consult it afterwards:
# untested!
open my $ssherr_fh, '>', 'openssh-err.log' or die $!;
my $ssh = Net::OpenSSH->new(user_id@host_name,
master_stderr_fh => $ssherr_fh);
if ($ssh->error) {
open my $f, '<', 'openssh-err.log';
my @err = <$f>;
print "ssh failed:\n@err\n";
}
Upvotes: 1