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

Perl Expect

by Anonymous Monk
on Jan 11, 2018 at 09:23 UTC ( #1207106=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi Monks, I am doing device interaction using Perl Expect module. Due to slowness or slow response of the device, expect could not send the commands onto the device and reporting failure. Some times device might close the connection itself. I have couple of queries. Please share you inputs. 1. Is is there a way to check the health of Expect handle (channel) created at both ends (sending/reeving) side. 2. Are there any bash commands that can be enabled on the box to get extra logs about the device interaction using the expect channel. Thanks in Advance

Replies are listed 'Best First'.
Re: Perl Expect
by Laurent_R (Canon) on Jan 11, 2018 at 14:19 UTC

    I have had a somewhat similar problem with expect, but this wasn't in Perl. Basically I had a shell program using expect to retrieve a dozen or so files from a distant server. I had no problem with relatively small files, but very large files would be truncated due to time out. The following statement in the expect part of the shell script solved the problem:

    set timeout -1
    I haven't used the Perl Expect module, at least not recently, but I suppose that you might be able to do something similar with it. HTH.
      ... I suppose you might be able to do something similar...

      I haven't used it, but Expect says, for $object->expect($timeout, @match_patterns), "If $timeout is undef Expect will wait forever for a pattern to match". That indicates your supposition is right.

      If you want to wait longer, but not forever, you can just set the $timeout accordingly... and maybe use $object->restart_timeout_upon_receive(1), which appears re-start the timer every time new data arrives (so if the command is outputting a word every 15 seconds, and can be considered dead if it goes more than 30 seconds, and the word you're looking for is the fifth word, then you could use a timeout of 30 seconds with the ->restart_timeout_upon_receive(1), and it should ... ah, enough of this "should" stuff:

      #!/usr/bin/env perl -l use warnings; use strict; use Expect; foreach my $restart (0, 1) { my $exp = Expect->new(); $exp->restart_timeout_upon_receive($restart); $exp->spawn(qw{perl -le 'foreach(@ARGV) { print $_; sleep 2; } ' w +ait until seeing the fifth word }) or die "cannot spawn: $!\n"; my @return = $exp->expect(4, 'fifth'); print "restart=$restart -> \@return = ", join ', ', map { $_ // '< +undef>' } @return; }
      Yep, that timedout on the first run, but continued through to the 'fifth' on the second run.

Re: Perl Expect
by salva (Abbot) on Jan 12, 2018 at 08:50 UTC
    Explain in more detail your environment and what your are trying to do and how, and show us the code you have produced so far.

    There are several ways to run Expect. There may be other more suitable modules also.

      I run script in Linux environment. I am trying to connect to a box and execute commands, all of sudden the handle gets close/not able to send any command on that expect channel and give error.

      Please find sample code snippet: #!/usr/bin/perl use strict; use Expect; my $session = new Expect; $session->spawn("/volume/labtools/bin/nicetelnet -KE $my_ip"); $session->expect(90, -re, "login:"); print $session "regress\r"; $session->expect(90, -re, "Password:"); print $session "MaRtInI\r"; $session->expect(90, -re, ".*"); print $session "pwd\r"; $session->expect(90, -re, ".*"); $session->close(); <\p>

      Error: Get below error, when script tries to print "pwd\r" cmmand on the expect handle. ERROR: No such file or directory <\p> Thanks in advance

        When posting put code inside <code>...</code> tags, please. Otherwise it is quite hard to read.

        What is that nicetelnet thing? using a too clever telnet client my get in the way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1207106]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2018-02-24 10:49 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.