Forum Discussion

SujeetKP_313828's avatar
SujeetKP_313828
Icon for Nimbostratus rankNimbostratus
Mar 15, 2017

Unable to SSH using Perl

hi All,

 

I am trying to connect to F5 load balancer through perl module Net::SSH2. "connect" method is working but I am unable to authenticate, whereas with the same credentials I am able to ssh to the device through putty. Can somebody please help me with this.

 

Code

 

use strict;
use Net::SSH2;

my $ssh=Net::SSH2->new();
if($ssh->connect('xx.xx.xx.xx')){
 print "successfully connected";
 }
$ssh->auth_password('username','password') or $ssh->die_with_error;

Error

 

Authentication failed (username/password) (-18 LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED) at test.pl line 8.
successfully connected

1 Reply

  • Artur_Zdolinsk1's avatar
    Artur_Zdolinsk1
    Historic F5 Account
    • $ssh->auth_password('username','password') or $ssh->die_with_error;
    • $ssh->auth(username => 'username', password => 'password') or $ssh->die_with_error;
    !/usr/bin/perl
    
    
    
    use Net::SSH2;
    
    $host = "192.168.254.10";
    $user = "root";
    $pass = "default";
    
    my $ssh2 = Net::SSH2->new();
    $ssh2->debug(1);
    $ssh2->connect($host) or die $!;
    $ssh2->auth(username => $user, password => $pass) or die "Unable to login \n".$ssh2->die_with_error;
    print "Connected to '$host' as '$user' \n";
    my $chan = $ssh2->channel();
    
    $chan->exec('uptime');
    
     Read remote controller program header
    read_chan($chan);
    
     close channel and disconnect
    $chan->close;
    $ssh2->disconnect;
    
     read_chan: read all input from chan, dump it to STDOUT
    sub read_chan {
      my ($chan) = @_;
    
       we switch to non-blocking and sleep between retries because it
       would fail to read anything with a blocking call... Still trying
       to find out why the blocking call would not read nothing.
      $chan->blocking(0);
    
      my $done;
      while (1) {
        my $in;
        while (my $bytes = $chan->read($in, 1024)) {
          $in =~ s/^/> /gm;
          print $in;
          $done++;
        }
    
         We leave after one sucessful read, this could be improved...
        last if $done;
        select(undef, undef, undef, .25);  sleep 50 ms
      }
      $chan->blocking(1);
    }
    

    Output:

    root@Linux200:~ perl connect.pl
    Connected to '192.168.254.10' as 'root'
    >  16:24:56 up  6:37,  1 user,  load average: 0.00, 0.01, 0.00
    
    

    Debian / perl 5.24.1