Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How can I show all input and output with Expect.pm

by frozenwithjoy (Priest)
on Oct 06, 2014 at 22:02 UTC ( #1103024=note: print w/replies, xml ) Need Help??


in reply to How can I show all input and output with Expect.pm

I see two main problems.

First, the following lines are in the wrong order for what you want your output to be:

print "$command\n"; $exp->send("$command\n"); $exp->expect(1, '-re','\(\d+\)');

You probably want this order instead:

$exp->send("$command\n"); print "$command\n"; $exp->expect(1, '-re','\(\d+\)');

Second, this:

my $counter = 1; print "($counter) ";

Should just be this:

my $counter = 0;

The extra stuff is just junk since you print and increment the counter in your loop. This change will fix printing 1 and 2 together like you observed here: (1) (2) Here is the second command

EDIT: Hmmm. Looking at your scripts more closely, I'm having second thoughts about this. What if you just print it all from the server script? Like this:

while (<STDIN>) { chomp; $counter++; print "($counter) $_\n"; if ($_ eq "error") {print "Error on command #$counter\n";} if ($_ eq "commit") {print "Committing data\n";} if ($_ eq "exit") {print "Exiting program...\n"; exit;} }

Replies are listed 'Best First'.
Re^2: How can I show all input and output with Expect.pm
by GeorgeAdams (Initiate) on Oct 06, 2014 at 22:35 UTC

    @frozenwithjoy, thanks for the reply. To answer your questions:

    1. It doesn't seem that reversing the order of the commands
      print "$command\n";
      $exp->send("$command\n");
      
      makes any difference either to the STDOUT or logfile output. It still gives:
      (1) (2) Here is the second command
      error
      (3) Error on command #3
      (4) commit
      This is the last command
      Committing data
      (5) (6) exit
      Exiting program...
      
    2. The server script has to print out the "(1) " before it receives any input. When it gets a line, it simply prints out "(2) " , waits for the next line, prints "(3) ", waits for the next line, etc. The code isn't optimized, but that's at least the explanation of why the $counter = 1 and the first print statement comes before the loop.

      That is, if you ran the server script and entered stuff, you'd get:

      (1) This is the first line I typed.  Now I'm hitting <ENTER>
      (2) I'm typing more stuff.
      (3) exit
      Exiting program...
      
    3. Good question. The server script is actually representing what I cannot control in this. It may be a company-wide program I'm trying to interact with, or a Juniper router, or an IDS box, or probably a lot of other things. So unfortunately for this example I have to pretend that I can't modify the server's output. All I can do is interact with the (program, router, server, etc.) myself, see what it outputs when I interact with it manually, then try to reconstruct that exact output when my Expect.pm script interacts with the (program, router, server, etc.) in the same way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2020-09-22 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (129 votes). Check out past polls.

    Notices?