HalNineThousand has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Expect Issues
by roboticus (Chancellor) on Dec 31, 2010 at 14:25 UTC | |
by HalNineThousand (Beadle) on Dec 31, 2010 at 16:41 UTC | |
|
Re: Expect Issues
by roboticus (Chancellor) on Dec 31, 2010 at 14:53 UTC | |
by HalNineThousand (Beadle) on Dec 31, 2010 at 16:22 UTC | |
|
Re: Expect Issues
by HalNineThousand (Beadle) on Dec 31, 2010 at 20:44 UTC |