Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
more useful options
 
PerlMonks  

Perl Net::SSH2 unable to connect

by kabachaa (Novice)
on Feb 26, 2013 at 19:48 UTC ( #1020749=perlquestion: print w/ replies, xml ) Need Help??
kabachaa has asked for the wisdom of the Perl Monks concerning the following question:

Perl Net::SSH2 Connect not able to authenticate I have small script to connect to server and then run my Perl script on it but I keep getting this error message. I can connect to the same machine when I run a command - SSH username@hostname using the same credentials. I am new to Perl and Linux so not sure how to debug. Error message : ssh auth failed. Net::SSH2::DESTROY object 0x16393c80 Script:
#!/usr/bin/perl use warnings; use strict; use lib qw(/usr/lib/perl5/site_perl/5.8.8/Net/SSH/Perl); use Net::SSH2; my $host = "server"; my $user = "user"; # your account my $pass = "pasword"; # your password my $cmd; my $ssh2 = Net::SSH2->new(); $ssh2->debug(1); if ($ssh2->connect($host)) { #if ($ssh2->auth_password($user,$pass)) { if ($ssh2->auth_keyboard($user,$pass)) { print "\n Executing command...\n"; $cmd = "ls"; print " ==> Running $cmd\n"; if(is_sshalive($ssh2) == 1) { print "\nSSH connection died"; exit 1; } else { run_testsuite($cmd, $ssh2); } } else { warn "ssh auth failed.\n"; exit 1; } } else { warn "Unable to connect Host $host \n"; exit 1; } print "test passed done 0\n";

Comment on Perl Net::SSH2 unable to connect
Download Code
Re: Perl Net::SSH2 unable to connect
by McA (Deacon) on Feb 26, 2013 at 21:04 UTC

    Hi,

    just had a quick look at this. When using

    $ssh2->auth_password($user,$pass);
    authentication worked on my side.

    I haven't looked a the subtle differences of auth_password and auth_keyboard.

    Best regards
    McA

Re: Perl Net::SSH2 unable to connect
by VinsWorldcom (Priest) on Feb 26, 2013 at 21:07 UTC

    The line:

    if ($ssh2->auth_keyboard($user,$pass)) {

    doesn't work for me either. I get the same error with correct credentials.

    Try using your commented out auth_password() instead of auth_keyboard() and it worked for me:

    VinsWorldcom@C:\Users\VinsWorldcom\tmp> grep auth_ test.pl #if ($ssh2->auth_password($user,$pass)) { if ($ssh2->auth_keyboard($user,$pass)) { VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl test.pl ssh auth failed. Net::SSH2::DESTROY object 0x2398d78 VinsWorldcom@C:\Users\VinsWorldcom\tmp> grep auth_ test.pl if ($ssh2->auth_password($user,$pass)) { #if ($ssh2->auth_keyboard($user,$pass)) { VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl Executing command... ==> Running ls Undefined subroutine &main::is_sshalive called at C:\Users\vincen_m\tm +p\test.pl line 18. Net::SSH2::DESTROY object 0x2398d78

    Note: The error in the second output has to do with is_sshalive() not being a valid sub - not sure where you've defined that.

      Oh looks like that worked thanks both McA and VinsWorldcom

      Another question I had was there is a method

       $ssh2->error  

      I guess it returns an error code what do these code mean. I get an error code like (0, 13, 7 etc)

      Last question I have is that if I am connected to $ssh2 until I say $ssh2->disconnect i would remain connected and I can do continue running my script. Thanks !

      From the perldoc:

      VinsWorldcom@C:\Users\VinsWorldcom> perldoc Net::SSH2 [...] error Returns the last error code; returns false if no error. In list cont +ext, returns (code, error name, error string). [...]

      I would try calling the error() accessor like such (untested):

      use Net::SSH2; [...] my $ssh2 = Net::SSH2->new(); [...] my ($code, $name, $string) = $ssh2->error; print "Error ($code): $name - $string\n";
Re: Perl Net::SSH2 unable to connect
by j0se (Pilgrim) on Feb 26, 2013 at 23:07 UTC

    After quickly going through the documentation, I'd try auth_list method to get the available authentication methods supported by the server and then use one of them. Also to get the error description (not just the error code), use the error method in a list context, ex. print $ssh2->error;.

    We are what we repeatedly do. Excellence, then, is not an act but a habit. -- Will Durant (Aristotle)

Re: Perl Net::SSH2 unable to connect
by salva (Monsignor) on Feb 27, 2013 at 09:03 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-04-17 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (443 votes), past polls