Re: [Ssh-sftp-perl-users] Trapping password authentication failures before the perl script die
Brought to you by:
dbrobins
From: Bryan B. <br...@bu...> - 2006-03-06 14:14:26
|
> Hi all, > > I have written a simple script to ssh to a remote box using > Net::SSH::Perl. It > works well enough when I supply the correct user name and password. > > However, if I supply a bad user name or password, the script dies with the > following error: > > Received disconnect message: Too many authentication failures for > bad_user > at /usr/local/lib/perl5/site_perl/5.8.0/Net/SSH/Perl/AuthMgr.pm line > 142 > > This behavior is undesired. I do not want the script to die. I want to be > able to test to see if authorization was successful and handle this error > myself, instead of dying. Pretty easy, just trap the actuall authentication in an eval block. > Here is a stripped down version of my script: > ------------------------------------------------------------------------ -------- > #!/usr/bin/perl > use strict; > use Net::SSH::Perl; > use Net::SSH::Perl::Constants qw( :agent ); > > my ($ip,$user,$passwd) = @ARGV; > > if (!$passwd) { > print "USAGE: ssh_test_login.pl [device ip] [user] [password]\n"; exit; > } > > my $ssh = Net::SSH::Perl->new($ip, protocol => 2, > debug => 1, > PreferredAuthentications => > 'password', > PasswordAuthentication => 'yes', PubkeyAuthentication => 'no', RSAAuthentication => 'no' > ); > > $ssh->register_handler( > SSH_AGENT_FAILURE, > sub { > my($channel, $buffer) = @_; > print "I received this: ", > $buffer->bytes; > } > ); > > > print "==================== login attempt ======================\n"; $ssh->login($user, $passwd); Just change this to: eval { $ssh->login($user, $pass); }; if ( $@ ) { # print error message, move to next host } else { # actually do something } Hope that helps. Bryan http://sourceforge.net/projects/rover |