This is one of those things I've done before and the basics seem simple, but I must be screwing up on something obvious. I've been over the docs on CPAN a number of times. I've used Expect to read from a modem and, years ago, to control a process. Now it won't work for me at all.

I'm testing it on telnet, where I telnet to a system I have set up that doesn't need a username or password, use 'ls', then log out, and even that is not working.

Here's my code:

#!/usr/bin/perl use Expect; our $abi; $res = open_telnet(); if ($res) { print "Failure. Error message: $res\n"; exit; } telnetcmd("ls"); telnetcmd("exit"); exit; sub open_telnet { my ($cmd, $res, $before, @parm); $cmd = "telnet wogglebug"; $abi = Expect->new(); $abi = Expect->spawn($cmd) or die "Can't do expect'"; $res = $abi->expect(5, "/ #", "not known"); if ($res == 1) { return 0; } $before = $abi->before(); if (!$before) { $before = "telnet failed. No reason given"; } return $before; } sub telnetcmd { my ($cmd, $res); $cmd = shift(@_); # print $abi "$cmd\n" or die "Can't write to expect object"; $abi->send("$cmd\n") or die "Can't send to expect object: $abi->er +ror()"; $res = $abi->after(); print "-----------\nAfter: $res\n----------\n"; return $res; }

If I run this as it is, then I always get an error on the 4th line of the telnetcmd routine: "Can't send to expect object: Expect=GLOB(0x8eb92e8)->error() at /threshNet/bin/xm-test line 41." Okay, the obvious is to just use the line before it, the commented out one that prints the command to the process, but even when I do that, I don't get output from the process, either by watching it as it runs, or by using expect->after().

This is just testing, I'm working on controlling another program with Expect, and it seemed to need to use Expect->send_slow() to send commands to that program, so I feel I'll need send() or send_slow() to make it work. But it seems like I must be doing something so obvious I can't find the problem.

There are also a number of other calls to the Expect object that I can't get to work. Expect->pid() doesn't return anything. Except->clear_accum() won't clear the data already in the buffer and Except->before() always returns the data from my first Except->before() call, no matter how many times I've called it.

Any help is appreciated. I suspect once someone points out what I did wrong, I'll be doing a *facedesk* in disbelief I missed something I should have known.


In reply to Expect Issues by HalNineThousand

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.