Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

storing return value from Expect

by perlknight (Pilgrim)
on Aug 09, 2002 at 00:01 UTC ( [id://188793]=perlquestion: print w/replies, xml ) Need Help??

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

All, I have an expect script which logs into a pix firewall and ping the vpn tunnel, how do I get an output from this:
$ssh->expect(10, [ qr 'wall#', sub { $ssh->send("ping 172.16.10.29\n"); # h +ow do I get the output of this ping into a variable? }], );
I've tried my $bla = $ssh->send("ping 172.16.10.29\n") but it doesn't work. Any idea? thanks.

Replies are listed 'Best First'.
Re: storing return value from Expect
by belkajm (Beadle) on Aug 09, 2002 at 03:00 UTC
    I wasn't able to get it to work using the calling-syntax you were using, which is a pity, as I think is the neatest way to call expect. Anyway, here is what I came up with as a possibility:
    $ssh->expect(10, '-re', 'wall#'); $ssh->send("ping 172.16.10.29\n"); $ssh->expect(10,"\n"); $ssh->expect(1); $bla = $ssh->before();
    Note that the second expect line is only required if echo is on. Even if it is, you can miss out the line if you don't mind grabbing the ping command as part of $bla.
    The timeout value in the last expect line should be set based on how long the ping will take.

    Hope this is helpful,
    Yoda

Re: storing return value from Expect
by thor (Priest) on Aug 09, 2002 at 11:44 UTC
    You want to store the value from your expect, not the send. The following is untested:
    $ssh->send("ping 172.16.10.29\r"); #notice \r here; I think it's prefe +rred over \n in expect my $stuff = $ssh->expect(10, qr 'wall#');
    If this doesn't work, a perldoc Expect might be in order.

    thor

      unfortunately, expect doesn't work this way. according to the docs on sourceforge:

      If called in a scalar context, expect() will return the position of the matched pattern within $match_patterns, or undef if no pattern was matched. This is a position starting from 1, so if you want to know which of an array of @matched_patterns matched you should subtract one from the return value.

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

      Also, please note that \r might not work with some programs when raw mode is activated. Normally expect does cr->lf translation, but this is disabled in raw mode. From a brief read of the docs, i think they are suggesting that \n is actually the better choice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found