Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Solved: Need Help in running Expect script in Perl

by subhasishn@yahoo.com (Initiate)
on Mar 12, 2012 at 11:47 UTC ( [id://959111]=perlquestion: print w/replies, xml ) Need Help??

subhasishn@yahoo.com has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am new to perl and still in learning phase.

I was trying to write an expect script which invokes an simulator and with few inputs gives me an output.

Here is the prob.. I am able to invoke the simulator, first few expect and send request also work fine. Then simulator throws an output, which is a line with few words and ending with newline. Here I need to send an Enter("\n") to the simulator so that I get the prompt back. Which I am unable to do.

Please find the output of the simulator without script and with script below. Also the script that i wrote.

Without Script

Selecting sproc1 CPU (Press Enter Here) sproc1>loadprg a.out (On prompt gave loadprg command) Endianness of the ELF file:0 .text matched [text segment] flag = 0x3 .ctors matched [data segment] flag = 0x3 .dtors matched [data segment] flag = 0x3 .data matched [data segment] sproc1>run(on prompt gave run command) Run mode selected Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 28 SWBKPT hit for SLOT 1..!! PC-Value : 0x124 Blk Count = 1 Cycles +: 332(Here is the problem.. Unable to give Enter input to get back th +e prompt) sproc1>dump mem 74010 4(On prompt gave dump command) 0x74010:: 0x00000000 sproc1>quit

Output of Simulator with Script

Selecting sproc1 CPU sproc1> sproc1>loadprg a.out Endianness of the ELF file:0 .text matched [text segment] flag = 0x3 .ctors matched [data segment] flag = 0x3 .dtors matched [data segment] flag = 0x3 .data matched [data segment] sproc1>run Run mode selected Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 28 SWBKPT hit for SLOT 1..!! PC-Value : 0x124 Blk Count = 1 Cycles +: 332

Here is the script

use Expect; chdir '/home/...some place.....' or die "Can't change directory : $!"; my $expect = Expect->new; my $command = './simulator/pruthvi.out'; my @parameters = qw(-m 4); my $timeout = 2; #$expect->raw_pty(0); $expect->spawn($command,@parameters ) or die "Cannot spawn : $!\n"; $expect->expect($timeout,"Selecting sproc1 CPU"); $expect->send("\n\n"); $expect->expect($timeout,"sproc1>"); $expect->send("loadprg a.out\n"); $expect->expect("sproc1>"); $expect->send("run\n"); $expect->expect($timeout,"SWBKPT hit for SLOT"); $expect->send("\n\n"); $expect->expect("sproc1>"); $expect->send("dump mem 74010 4 \n"); $expect->expect("sproc1>"); $expect->send("\n");

I tried the same thing in shell script, thats working fine

Here's the shell script

#!/bin/sh #!/usr/bin/expect cd /home/...some place../ expect -c ' set timeout 2 set file [open /home/aaa w] # log_user 1 spawn ./simulator/pruthvi.out -m 4 expect "Selecting sproc1 CPU" send "\n\n" expect "sproc1>" send "loadprg a.out\n" expect "sproc1>" send "run\n" expect "SWBKPT hit for SLOT" send "\n" expect "sproc1>" send "dump mem 74010 4 \r" expect "sproc1>" puts $file "$expect_out(buffer)" send "quit\n" #puts $file "$expect_out(buffer)" expect eof '

I would appreciate if someone could help me out.

Replies are listed 'Best First'.
Re: Need Help in running Expect script in Perl
by sierpinski (Chaplain) on Mar 12, 2012 at 12:51 UTC
    What output/error are you getting in the perl version? At what line exactly does it stop working as expected?

      Thanks for your quick response..

      Its not throwing any error.. the script just comes out due to time out.

Re: Need Help in running Expect script in Perl
by jffry (Hermit) on Mar 12, 2012 at 22:07 UTC

    First, it has been a while since I've used CPAN Expect, so my advice may not be accurate.

    Second, I want to clarify your problem. You are saying that when output gets here:

    SWBKPT hit for SLOT 1..!! PC-Value : 0x124 Blk Count = 1 Cycles +: 332

    ...your Perl script does not seem to send the "\n" so that the console program will continue.

    I see two potential problems. One, you have really short timeouts (2 seconds). Two, you are expecting "SWBKPT hit for SLOT" but some text is still being output after that match. Thus, it is possible the "\n" is being sent before the console program is reading input and the "\n" send is not buffered.

    To help debug, try calling the expect() method in array context, then dump the array to see the status of the expect() call.

    use Data::Dumper; my @exp_stat; @exp_stat = expect "SWBKPT hit for SLOT"; print Dumper(\@exp_stat);

    And read this explanation from the CPAN Expect docs:

    If called in an array context expect() will return ($matched_pattern_position, $error, $successfully_matching_string, $before_match, and $after_match).

    $matched_pattern_position will contain the value that would have been returned if expect() had been called in a scalar context. $error is the error that occurred that caused expect() to return. $error will contain a number followed by a string equivalent expressing the nature of the error. Possible values are undef, indicating no error, '1:TIMEOUT' indicating that $timeout seconds had elapsed without a match, '2:EOF' indicating an eof was read from $object, '3: spawn id($fileno) died' indicating that the process exited before matching and '4:$!' indicating whatever error was set in $ERRNO during the last read on $object's handle. All handles indicated by set_group plus STDOUT will have all data to come out of $object printed to them during expect() if log_group and log_stdout are set.

      I tried debugging as advised

      Here is the output

      Use of uninitialized value $pattern_list[1] in print at try_expect.pl +line 24. 1SWBKPT hit for SLOT 1..!! PC-Value : 0x124 Blk Count = 1 Cycles + : 332 sproc1>loadprg a.out Endianness of the ELF file:0 .text matched [text segment] flag = 0x3 .ctors matched [data segment] flag = 0x3 .dtors matched [data segment] flag = 0x3 .data matched [data segment] sproc1>run Run mode selected Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 32 Buffer Empty = 28 Expect=GLOB(0xa088960)

      Now if am not wrong then $pattern_list1 should give me the value that would have been returned if expect() had been called in a scalar context. But its shows uninitialized. Now how should I set an expect for nothing....Help please

        I'm assuming you're using @pattern_list to hold the return values from expect() like this:

        @pattern_list = expect "SWBKPT hit for SLOT";

        If that is true, then $pattern_list[0] is the "matched pattern position", $pattern_list[1] is the "error" and $pattern_list[2] is the "successfully matching string". However, to debug this, we need all the values, which is why I asked you to use Data::Dumper.

        If you want to print the array values without Data::Dumper, then do something like this:

        my @arr = qw( bob sue jane); print "\@arr = " . join(':', @arr) . "\n";
        Run example:
        my@mybox:~/sandbox $ ./4.pl @arr = bob:sue:jane

        The reason all the values are important is because if $pattern_list[4] ("after match") has a value, then that probably means Expect is sending the "\n" before your console program is reading input.

        Actually, you know what would be a quicker test of that? Insert a sleep() between your expect() and send() like this:

        $expect->expect($timeout,"SWBKPT hit for SLOT"); sleep 2; $expect->send("\n\n");

        Of course, this test assumes that it takes less than 2 seconds between when the console program prints "SWBKPT hit for SLOT" and when it prints "Cycles : 332".

      Thanks for your reply. I appreciate it.

      I thought the same.. so I had tried the script with longer timeout(100 sec) as well didnt work out. Then I replaced the output completely as I was receiving from my simulator. That too didnt work out.

      Is there any work around other than CPAN Expect

      As suggested I will try to debug and try again

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found