Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to catch/avoid SSHConnectionAborted

by daphnaw (Acolyte)
on Nov 15, 2010 at 12:53 UTC ( [id://871462]=perlquestion: print w/replies, xml ) Need Help??

daphnaw has asked for the wisdom of the Perl Monks concerning the following question:

Hi, My program connects to a remote machine as following:
my $ssh; foreach (1..3) { eval { $ssh = Net::SSH::Expect->new( host => $host, password => $password, user => $user, raw_pty => 1, timeout => 3, binary => $ssh_exec, ); }; last if $ssh; sleep 1; } die "Could not connect to remote host '$host'\n" if not $ssh; my $login_output = $ssh->login;
If the user entered the wrong username and/or password, I'd like to alert this without getting : SSHConnectionAborted at test.pl line 779. I've tried checkig the login_output , wrapping it with 'eval', catching before/after, with no success. The weird thing is that the line where it aborts is after the login:
$ssh->exec("stty raw -echo"); my $resp_uname = $ssh->exec('uname'); #This is line 779
Any ideas?

Replies are listed 'Best First'.
Re: How to catch/avoid SSHConnectionAborted
by salva (Canon) on Nov 15, 2010 at 13:08 UTC
    Just guessing, but Net::SSH::Expect may be unable to detect failed logins: It tries to log once, assumes login was successful and starts sending commands to be executed by the remote shell when actually, the SSH process is still at the login prompt. That will explain why you get the error delayed.

    You can try using other SSH client modules as Net::OpenSSH or Net::SSH2.

      I'm afraid switching to another module is not an option at this stage .. (none of the modules above are installed with our program)
        Try adding the following arguments to the constructor:
        ssh_option => 'o NumberOfPasswordPrompts=1'
        That should avoid the connection error being delayed, though you may also need to run some dummy command after it to let the module detect the closed SSH pipe:
        my $ssh; foreach (1..3) { $ssh = eval { my $try = Net::SSH::Expect->new( host => $host, password => $password, user => $user, raw_pty => 1, timeout => 3, binary => $ssh_exec, ssh_option => 'o NumberOfPasswordPrompts=1' ); $try->exec("stty raw -echo"); $try; }; last if $ssh; sleep 1; }
        What are the contents of the $login_output variable?

        According to the Net::SSH::Expect manpage, it should contain some form of response from the server:

        # 2) logon to the SSH server using those credentials. # test the login output to make sure we had success my $login_output = $ssh->login(); if ($login_output !~ /Welcome/) { die "Login has failed. Login output was $login_output"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://871462]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found