Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Issue with Expect

by jffry (Hermit)
on Mar 21, 2012 at 20:05 UTC ( [id://960861]=note: print w/replies, xml ) Need Help??


in reply to Issue with Expect

See if this recent thread offers any help.

The two take-away items from that thread are:

  1. Read the "How come when I automate the passwd program to change passwords for me passwd dies before changing the password sometimes/every time?" question from the Expect.pm FAQ.
  2. Assign the troublesome expect() call to an array and dump the array to help debug. Like this:
use Data::Dumper; my @exp_stat; @exp_stat = expect('-re', 'session:\n$'); print Dumper(\@exp_stat);

Of course, for item 2 to be useful, you'll have to break up your giant expect() call into individual ones. And the meaning of each array element is documented both in the Expect.pm docs and that recent thread.

Replies are listed 'Best First'.
Re^2: Issue with Expect
by azstyx (Acolyte) on Mar 22, 2012 at 01:11 UTC
    Thanks!!!! I will check the info out. Hey is there a better way to code my "giant" Expect clause, I'd sure appreciate any suggestions. I was just following the example I was given by a person who introduced to me Expect.pm. Thanks - I appreciate the advise!
Re^2: Issue with Expect
by azstyx (Acolyte) on Mar 22, 2012 at 15:39 UTC
    So I reviewed the other thread and looked at FAQ. I am using Data Dumper now, still no luck. I wrote a smaller program to ssh to another unix server and I get the same issue, right after i send the user password for login (that works ok, I get my login completed), I cannot send or expect anything else after that procedure completes. Right after the login procedure completes, i try to send a ls command and expect - all i get is timeout:
    screen shot: Sending ls command! Starting EXPECT pattern matching... at /export/dmzsupp/perl5/5.14.2/lib/site_perl/5.14.2/Expect.pm line 5 +61 Expect::expect('Expect=GLOB(0x65ca28)', 6, 'ARRAY(0x9b6460)', +'ARRAY(0x9b6328)', '-re', 'Regexp=REGEXP(0x9990d8)') called at ./fwlo +gin.pl line 42 Error,Timeout while waiting 6 secs for ls command on [sol8server] user +[user]. Error:1:TIMEOUT Continuing $VAR1 = [ undef, '1:TIMEOUT', undef, '', undef, undef ]; The code: print "\nSending ls command!\n\n"; $sshFW->send("ls\r"); @exp_stat = $sshFW->expect($expTimeOut, # Timeout trap [timeout => sub { $expFWStatus = 1; $expFWmatchString = $sshFW->match; print "Error,Timeout while waiting $expTimeOut secs for ls com +mand on [$FWHost] user[$usr]. Error:" . $sshFW->exp_error() . "\n"; + } ], # EOF trap [eof => sub { $expFWStatus = 2; $expFWmatchString = $sshFW->match; print "Error,Premature EOF during ls command for [$FWHost] use +r[$usr]. Error:" . $sshFW->exp_error() . "\n"; } ], '-re', qr /% $/, ); # End expect, got Unix prompt ok print "\nContinuing\n\n"; print Dumper(\@exp_stat);#debug # The above behaves just like the ls command was never sent!
      Why are you terminating sends with "\r" and not "\n"?
      #!/usr/bin/env perl use 5.014; use warnings; use Expect; my $exp = Expect->new; $exp->spawn('ssh tak'); $exp->expect(10, '~$ '); # prompt. using ssh keys so no password $exp->send("ls\n"); $exp->expect(10, '~$ '); $exp->send("exit\n"); $exp->soft_close(); __END__ Output: zengargoyle@tak:~$ ls ... ls output ... zengargoyle@tak:~$ exit logout Connection to tak closed.
      I would guess that the password input is accepting the "\r" as an input terminator (because login/password type input is wonky like that) but the proper input terminator for the type of session input and for general shell commands is "\n".

      Just a guess, unless your Solaris is wonky or running some special shell (not csh/sh/etc) you should be using "\n". I've done a bunch of Expect stuff with Solaris, routers, switches, ... over ssh and never have I used "\r". I always use "\n" and have never had the sort of problem you seem to be having.

        Yes, I was just posting the resolution. You are absolutely correct! I just discovered that Solaris 8 likes to have it's responses and commands terminated with \n and not \r. I actually have used \r for connecting to network appliances and never had a problem. What threw me off was I sent the user login password as "$passw\r" and it worked ok. I have updated all my send commands to use \n instead of \r. All is well now. Thanks!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://960861]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found