Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl Expect is not sending multiple commands

by essej1 (Novice)
on Sep 17, 2012 at 19:32 UTC ( [id://994085]=perlquestion: print w/replies, xml ) Need Help??

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

OK, I've got Perl Expect to sort of work. I can login via ssh and send SOME commands. The issue is that after sending a few commands subsequent prints aren't being sent. For example (code snippet - after the login completes)

$exp->expect(10, $ssh_host, sub { print $exp "term length 0\n"; } ); $exp->expect(10, $ssh_host, sub { print $exp "sh clock\n"; } ); $exp->expect(10, $ssh_host, sub { print $log_fh "\n==> A\n"; # print to log to show I'm there print $exp "\nsh ver\n"; print $log_fh "\n==> B\n"; } ); $exp->exp_internal(0); $exp->expect(10, $ssh_host, sub { print $log_fh "\n==> Z\n"; # print to log to show I'm there print $exp "exit\n"; print $log_fh "\n==> Y\n"; }

When I look at the log I see the '==>' prints but the second send command (print $exp "<what ever>" isn't being actually sent. I can change the order ('sh ver' first, 'sh clock' second and I get the same thing. The second command just doesn't seem to be sent. Is there some I'm missing?

Paradise: Florida, full tank of gas, no appointments.

Replies are listed 'Best First'.
Re: Perl Expect is not sending multiple commands
by Argel (Prior) on Sep 17, 2012 at 20:51 UTC
    Could you be Suffering from Buffering for the log file? Could you have the network device syslog the commands being executed instead of trying to rely on logging them in your script instead?

    FWIW, here's what I used to do in 7+ year old script:

    print $expect_instance "show system\r"; ( $which, $why, $match, $before, $after ) = $expect_instance->expect( +$Timeout, '#'); if( ! $which ) { mylog( $dev, 'sho_sys', $why ); return; } $sho_sys = $before;
    Not familiar with the sub method you are using, but the last time I used Perl Expect was several years ago. :(
     

    Elda Taluta; Sarks Sark; Ark Arks
    My deviantART gallery

      Well chalk this up to not paying attention to the output. Expect was working as expected (bad pun intended). The issue turned out to be that the 'sh ver' command contains the device name. I was looking for the device name in the expect statement. Therefore, the expect was stopping mid output. I used your example of getting the various return items to get the last character in the prompt, which I concatenated to the device name for future prompts. I had to wait until the password was successful to get that bit of information (the $after). This matters as some devices use '#' and some use '>'.

      Paradise: Florida, full tank of gas, no appointments.
        Usually '#' indicates you are in enable mode and '>' indicates you are in normal mode (i.e. not all commands are available).

        I think you can do a regex like below (untested), though there is probably a cleaner/better way to do it.

        ($which,$why,$match,$before,$after) = $expect_instance->expect($Timeou +t, '-re', "(#)|(>)");

        Elda Taluta; Sarks Sark; Ark Arks
        My deviantART gallery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found