Search code examples
pythonperlsshexpect.pm

Perl ssh Password


I am trying to use perl to SSH in to a machine and issue a few commands...

my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user,$pass);
my($stdout, $sterr, $exit) = $ssh->cmd($cmd);
print "$stdout \n"

This is the general idea, but I am still stuck at the prompt for the password. I have also attempted to use python, and also can not get past the prompt for password. Expect, on the other hand, handles the password just fine. Does anyone have any idea what would cause this?

Edit: additional info

I am using putty to a linux machine and then ssh into numerous more linux machines. Using the perl code above, the user name gets set correctly, but I end up having to manually entering the password each time.


Solution

  •   use strict;
      use warnings;
      use Expect;
    
      $exp= Expect->spawn("ssh $host -l $user");
    
      sleep(1);
    
      $exp->expect($timeout,"Password:");
      $exp->send("$pass\r");
    
      $exp->expect($timeout,-re,'>');
      $exp->send("ls -l\r");
    
      $exp->expect($timeout,-re,'>');
      $exp->send("mkdir aDir\r");
    
      $exp->expect($timeout,-re,'>');
      $exp->send("chmod 777 aDir\r");
    
      $exp->expect($timeout,-re,'>');
      $exp->send("exit\r");
    

    left out variable declarations for obvious reasons... Not exact answer to question but a viable work around using only the "Expect Module".