3

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.

pynexj
  • 19,215
  • 5
  • 38
  • 56
Tom
  • 312
  • 5
  • 17
  • I have actually came up with the solution for a work around to the password issue by using "use expect" in perl and having that handle that for me. Also, I know for security reasons this is sub par, but It is not by choice. – Tom Jun 05 '15 at 17:55
  • have you heard of pexpect? – Padraic Cunningham Jun 05 '15 at 17:55
  • 2
    If at all possible, you should set up SSH keys instead of using a password. – ThisSuitIsBlackNot Jun 05 '15 at 18:04
  • @Tom i think you should use SSH keys. They will not ask for anything and you will not stuck on anything. – shivams Jun 05 '15 at 18:59
  • I will look into the keys, but for this project they were not really an option. Why I couldn't get past the password portion with out using some form of Except is really the question Im stuck on. – Tom Jun 05 '15 at 19:23
  • Can you expand the sentence `but I am still stuck at the prompt for the password`, what that it means exactly? are you seeing any prompt on the console or something? Also, which operating system is running on the remote side? – salva Jun 08 '15 at 07:45
  • There is something completely wrong with your question: what you say doesn't make sense. Describe in detail the steps you take and what happens every time because probably the issue is that SSH doesn't work the way you thing it does. – salva Jun 09 '15 at 10:03
  • 1. putty in to a linux machine where script is located. 2. run script. 3. Will be promtped for password... 4. wait for a while to see if there is any time lag. 5. Maunually enter password wrong 3 times so it will prompt for password in form "username@hostname password: " 6. verify username was set correctly via -l flag, not the default username. – Tom Jun 09 '15 at 22:37
  • Can you from the second machine connect to the third one using the binary ssh client in verbose mode (`ssh -vvv user@host3`) and post the output? – salva Jun 10 '15 at 13:01
  • Is there a more specific section of that you are looking for? It is numerous pages of output – Tom Jun 10 '15 at 14:55
  • The thing is that when you pass a password to Net::SSH::Perl it nevers prompts for one at the console... wait!, can you check that `$pass` is defined? Have you enabled the `strict` pragma in your code? – salva Jun 11 '15 at 06:45
  • Yes "use strict" and "use warnings" enabled. – Tom Jun 11 '15 at 15:42

1 Answers1

0
  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".

Tom
  • 312
  • 5
  • 17