in reply to Net::SSH::Perl - stops script execution on authentication failure

I haven't used Net::SSH::Perl, but I have used Net::SSH::Expect to do basically the same thing. Connecting to a bunch of servers to check for failures and do basic configuration management reporting.
Here is a chunk of the code, hope it helps.
my $ssh = Net::SSH::Expect->new ( host => "$serverlist[$host]", user => "$user", raw_pty => 1, restart_timeout_upon_receive => 1, timeout => 6, ssh_option => " -x", ); # Validate that we have a successful ssh connection $login_output = $ssh->run_ssh() or die "Couldn't start ssh pro +cess, error $!\n"; sleep(1); my $ret; my $rc1 = eval{$ret = $ssh->read_all(12);}; unless($rc1) { open(NOCON,">>$NOCONNECT"); print NOCON "$serverlist[$host] - Could not connect -- + skipping.\n"; close(NOCON); $pm->finish(0); # Couldn't connect to this one, skip +to next server in main loop } my $rc = eval{( $ret =~ />\s*|$\s*\z/) or die "where's the rem +ote prompt?";}; if($rc) { if($ret =~ m/[Pp]assword:/) { # print("Server asking for password, key not ins +talled.\n"); open(NOCON,">>$NOCONNECT"); print NOCON "$serverlist[$host] - Asking for p +assword -- skipping.\n"; close(NOCON); $pm->finish(0); # Couldn't connect to this +one, skip to next server in main loop } } $ssh->exec("stty raw -echo"); # Start issuing the commands to check
Also note that I have pm->finish, which is part of Parallel::ForkManager, which allows me to connect to "n" servers at once, and not have to do them in sequence. If you have a lot of servers to check, I'd recommend at least looking into that.

Hope that helps.
  • Comment on Re: Net::SSH::Perl - stops script execution on authentication failure
  • Download Code

Replies are listed 'Best First'.
Re^2: Net::SSH::Perl - stops script execution on authentication failure
by GA_L (Initiate) on Apr 23, 2010 at 18:57 UTC
    Hi guys,

    Thanks a lot for your replies! This is much appreciate!

    I tried the "eval" on the login() and it works fine, it spits an error saying "authentication failure" but it continues to execute which was what I wanted.

    Thanks for the equivalent with expect this is really interesting to see another approach. And yes, I have quite a lot of servers to proceed so I will have a look at the module you suggested.

    Have all a nice WE now :) cheers Gael