azstyx has asked for the wisdom of the Perl Monks concerning the following question:

Hello Folks! Env=SUSE 10.2 Linux, Perl 5.14.2, Expect v1.21
I am trying to use Expect to ssh to a Solaris 8 server. The manual dialog for this login is:

>ssh -o StrictHostkeyChecking=no user@sol8server Could not create directory '/appl/user/.ssh'. Failed to add the host to the list of known hosts (/appl/user/.ssh/known_hosts). user@sol8server's password: <== I enter password here Last login: Tue Mar 20 10:01:13 2012 from xxx Could not chdir to home directory /home/user: No such file or directory **** SYSTEM ACTIVITIES MAY BE MONITORED **** <== sol 8 server What is the purpose of this session: <== sol 8 server Collect data. <== my response Thanks, have a fine day and be sure log off after you are done. <== sol 8 server sol8server% <== sol 8 server

my perl code
my $expPty = 1; # pty mode my $expDebug = 3; # Expect debug level $sshUX = Expect->new; # create Expect object $sshUX->debug($expDebug); # debugging $sshUX->log_file($expUXLogFile, "w"); $sshUX->raw_pty($expPty); $sshUX->expect(10, [qr /password: $/ => # got password prompt sub { my $self = shift; $self->send("$passw\r"); print "Sent password!\n";#debug exp_continue; } # end sub ], [qr /session:\n$/ => # got reason prompt sub { my $self = shift; $self->send("Get configs\r"); print "Sent session reason!\n";#debug exp_continue; } # end sub ], # Timeout trap [timeout => sub { $expUXStatus = 1; print PL "Error,Timeout while waiting $expTimeOut secs for login on [$UXHost] user[$usr]. Error:" . $sshUX->exp_error() . "\n"; } ], # EOF trap [eof => sub { $expUXStatus = 2; print PL "Error,Premature EOF during login for [$UXHost] user[$usr]. Error:" . $sshUX->exp_error() . "\n"; } ], '-re', qr /%$/, # wait for device prompt '%' ); # End expect for Login processing print "Finished processing login expect expUXstatus[$expUXStatus]\n"; # debug
The above executes OK for password prompt and it also catches the prompt for the reason for the login ok. Then I send the reason "Get configs\r". This is where it stops working - it appears that it did not get the reason string. Nothing is sent back after I send the reason string - I can see this in the expect logfile:
Could not create directory '/appl/user/.ssh'. Failed to add the host to the list of known hosts (/appl/user/.ssh/known_hosts). user@sol8server's password: Last login: Tue Mar 20 09:45:08 2012 from xxx Could not chdir to home directory /home/user: No such file or directory **** SYSTEM ACTIVITIES MAY BE MONITORED **** What is the purpose of this session:
All I get is a timeout and it appears that the system never responds after i send the "Get Configs" string with carriage return. Ends execution with printing:
Sent password! Sent session reason! Finished processing login expect expUXstatus[2]
I Does anyone have any suggestions? Thanks!

Replies are listed 'Best First'.
Re: Issue with Expect
by jffry (Hermit) on Mar 21, 2012 at 20:05 UTC

    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.

      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!
      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.