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

Hello fellow monks,
I am using the Expect module (v1.15) to automate a simple telnet process that logs in and sends a sample ping. Now I know I can probably use the Net::Telnet module, but I am using this for a purpose. Here is a snippet of my code (logtext is a simple function writing to a logfile).
while($perform_step > 0) { if ($tcp_transx_param{$row_ref->[$i]}{$perform_step}) { logtext "Value of step is $perform_step"; logtext "Sending =>$tcp_transx_param{$row_ref->[$i]}{$perform_ +step}{send}<="; $session->send("$tcp_transx_param{$row_ref->[$i]}{$perform_ste +p}{send}\r"); $session->expect($expect_timeout, '-re', "$tcp_transx_param{$r +ow_ref->[$i]}{$perform_step}{expect}") || warn "Did not match at step + $perform_step for CollectionID $row_ref->[$i]" . $session -> exp_err +or(); logtext "Expecting =>$tcp_transx_param{$row_ref->[$i]}{$perfor +m_step}{expect}<="; if ($session->exp_error()) { logtext "ERROR=>" . $session->exp_error(); logtext "Closing Session"; $session->hard_close(); last; } else { $perform_step++; } } else { $perform_step=0; $session->soft_close(); last; } logtext "Increasing my step to $perform_step"; }
The issue I seem to be having is that the while loop continues regardless of the response. It works fine with the correct password but if I use the wrong password, then it continues and reports failure at the wrong step. Please help.

Replies are listed 'Best First'.
Re: Help using Expect.pm
by gnangia (Scribe) on Dec 03, 2002 at 23:15 UTC
    Did some troubleshooting and the problem seems to be when looking for the string "$", it is not there in the response, but the module log file says it is -
    spawn id(6): Does ` '^M
    match:^M
    pattern #1: -re `$'? YES!!^M
    Before match string: ` '^M
    Match string: `'^M
    After match string: `'^M
    Matchlist: ()^M
    Dec 03 18:09:57 Increasing my step to 5
    Dec 03 18:09:57 Value of step is 5

      I'm pretty certain that the parameter to expect() is a regexp, which means that $ would be the end-of-line marker, instead of a literal $. Try escaping it with a backslash.

      As an aside, it would help if you could include more of the surrounding code. Specifically where you set up the session, and the data that you're actually sending to the send() and expect() calls. It would be best if you reduced it to a "least-common-denominator" snippet that could be run and exhibits the problem.

      bbfu
      Black flowers blossum
      Fearless on my breath

        Thank-you...that was the problem. I had to escape the "$" character because it was interpreting it as the end-of-line marker in lieu of the literal $.