in reply to Re: ssh using expect
in thread ssh using expect

I Have tried with Net::SSH::Expect ...IF i run my script sometime,i was able to login to host..Sometimes not.. Not sure that i had used Net::SSH::Expect correctly..! Let me know whats wrong in this code..
#! /usr/bin/perl use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host=>"xxx", user=>'yyy', timeout=>10, raw_pty=>1 ); $prompt="[Pp]assword"; $ssh->run_ssh(); $ssh->waitfor('qr/\(yes\/no\)\?$/',2); $ssh->send("yes\n"); $ssh->waitfor('qr/$prompt:\s*$/',5); $ssh->send("password"); my $peshlogin=$ssh->exec("su - root\n"); $ssh->waitfor('qr/$prompt:\s*$/',5); $ssh->send("password\n"); my $peshlogin=$ssh->exec('echo $?' . "\n","4"); print "THE LOGIN is $peshlogin\n"; $ssh->close();

Replies are listed 'Best First'.
Re^3: ssh using expect
by tirwhan (Abbot) on Dec 04, 2007 at 19:10 UTC

    One major thing is wrong: no error checking anywhere. Take a look at the Synopsis again, all those "or die" statements are there for a purpose (change them to "or warn" if you want a softer fail).

    Also, you're mixing calls to $ssh->exec() with $ssh->waitfor, which is wrong. exec reads out the data sent by the server, so by the time waitfor is called it has nothing to ... err... wait for. You should use "send" in conjunction with "waitfor". Again, RTFM


    All dogma is stupid.
      Hi, I need help with the following issue where in interactive menu selection works fine manually but fails with perl ssh expect. When executing manually i get the following:
      Please enter the operation serial number. >1 NO. NE name + NE type name + 1 TB2SEE + HW_SEE + 2 UKTB2SEE + HW_SEE + 3 TB2SLEE + HW_SLEE + 4 UKTB2SLEE + HW_SLEE + ***************Input Tips************* a: To create a tracing task for an NE, enter the NE number. b: To create a tracing task for multiple NEs, enter the NE numbers and + separate every two of them with a plus sign (+). Example: 1+2. c: To create a tracing task for all NEs, enter 0. ************************************** Please enter the NE serial number. >
      but when i run it from perl i get the following:
      main::(iTrace_start_stop.pl:48): $ssh->waitfor('Please\senter\s +the\soperation\sserial\snumber.\s*', 2) or die "prompt 'operation ser +ial number' not found after 2 second"; DB<1> main::(iTrace_start_stop.pl:49): $ssh->send("1"); DB<1> main::(iTrace_start_stop.pl:50): print $ssh->peek(1); DB<1> >1 NO.NE nameNE type name ---- More (Enter to show More) ----
      My code is as follows:
      $ssh->waitfor('Please\senter\sthe\soperation\sserial\snumber.\s*', 2) + or die "prompt 'operation serial number' not found after 2 second"; $ssh->send("1"); print $ssh->peek(1); $ssh->send("2");