in reply to How to catch/avoid SSHConnectionAborted

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.

  • Comment on Re: How to catch/avoid SSHConnectionAborted

Replies are listed 'Best First'.
Re^2: How to catch/avoid SSHConnectionAborted
by daphnaw (Acolyte) on Nov 15, 2010 at 14:35 UTC
    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; }
        didnt work :-( It now fails on the login line with the following exception: FIX: .ssh/known_hosts SSHProcessError The ssh process was terminated. at test.pl line 775 However, the following code will catch the exception:
        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; $ssh->exec("stty raw -echo"); my $resp_uname; eval { $resp_uname = $ssh->exec('uname'); }; if ($@) { if ($@ =~ m{SSHConnectionAborted}) { die "Invalid username and/or password for OS user $user on th +e remote server!"; }else { die "Could not connect to remote host '$host' : $@\n"; } }
      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"; }

        The $login_output variable only contains the prompt for password: Password: I think its contents is installation dependent.