|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
| News | Expect | Recommended Links | Reference | Tutorial | TCL |
| Installation | Regular expressions | History | Humor | Etc |
To install Expect you will also need IO::Stty which will be in the same directory
as you found this package, currently at ftp://ftp.habit.com/pub/perl.
You will also need IO::Tty which can be found in Graham Barr's directory on any
CPAN site. For each of these the usual perl Makefile.PL;make;make install should
work. I haven't implemented a make test for either IO::Stty or Expect. It would
probably be wise to read the docs on IO::Tty before doing a make install with it.
The Expect for Perl module was inspired more by the functionality the Tcl tool provides than any previous Expect-like tool such as Comm.pl or chat2.pl. I've had some comments that people may not have heard of the original Tcl version of Expect, or where documentation (book form) on Expect may be obtained.
The Tcl version of expect is a creation of Don Libes (libes@nist.gov).
The Tcl Expect home page is http://expect.nist.gov/.
Don has written an excellent in-depth tutorial of the Tcl Expect, which is _Exploring
Expect_. It is the O'reilly book with the monkey on the front. Don has several references
to other articles on the Expect web page.
As always, please let me know if there's something you'd like to see documented
that isn't. Please check the included FAQ and tutorial directory first. :-)
An unreliable program can be controlled from a perl program using the Expect.pm module. A description of the unreliable program and the use of the Expect module is presented.I have a program that I need to run a large number of times. This program has a nasty bug in it. When you feed it bad data, it just sits there forever instead of providing a helpful error message. Bad Program!
I can't change the program, but I need to call this program inside a loop in my code. So I am using the perl Expect module to skip over the problem cases and continue with the rest of the runs of the program.
The Expect.pm module is capable of managing this process, so I wrote a few little test programs to help me understand how to accomplish this task. This document includes the tests along with a few words of explanation. For more documentation about the Expect module, search CPAN.
An Unreliable Program
Instead of using my real-world unreliable program, I used this program to simulate it instead. When it succeeds, this program just prints a simple message. When it fails, it just hangs. This matches the behavior of the program that I need to control.#!/usr/bin/perl # # Fri Dec 13 23:10:54 PST 2002 # # Copyright Tom Anderson 2002, All rights reserved. # This program may be copied under the same terms as Perl itself. # Please send modifications to t@tomacorp.com # # # unreliable.pl - Simulate a program that sometimes just hangs # my $VERSION=".01"; use strict; use warnings; use diagnostics; if (rand(10) > 8) { sleep 1 while 1 > 0; } else { print "--------------------------------\n", "It worked this time, no problems\n", "--------------------------------\n"; }
Expect.pm Test Program One
The following test program runs the unreliable program twenty times. If the unreliable program takes longer than five seconds, the attempt to run it is terminated and the test program continues.
#!/usr/bin/perl # # Fri Dec 13 23:10:54 PST 2002 # # Copyright Tom Anderson 2002, All rights reserved. # This program may be copied under the same terms as Perl itself. # Please send modifications to t@tomacorp.com # # timeout.pl - Expect Test Program # use strict; use warnings; use diagnostics; use Expect; my $timeout=5; foreach my $i (1..20) { my $exp = Expect->spawn("./unreliable.pl") or die "Cannot spawn unreliable process $!\n"; $exp->expect($timeout); }
Expect.pm Test Program Two
The next version of the test program adds the feature that a message is printed for the cases when a timeout occurs.
#!/usr/bin/perl # # Fri Dec 13 23:10:54 PST 2002 # # Copyright Tom Anderson 2002, All rights reserved. # This program may be copied under the same terms as Perl itself. # Please send modifications to t@tomacorp.com # # timemsg.pl - Expect Test Program # use strict; use warnings; use diagnostics; use Expect; my $timeout=5; foreach my $i (1..20) { my $exp = Expect->spawn("./unreliable.pl") or die "Cannot spawn unreliable process $!\n"; $exp->expect($timeout, [ timeout => sub { print "Process timed out.\n"; } ] ); }Expect.pm Test Program Three
The next version of the test program adds a check to see if the unreliable program prints "It worked" somewhere in its output. If the test program detects this string, it prints "Status: OK" after the unreliable program runs.#!/usr/bin/perl # # Fri Dec 13 23:10:54 PST 2002 # # Copyright Tom Anderson 2002, All rights reserved. # This program may be copied under the same terms as Perl itself. # Please send modifications to t@tomacorp.com # # timecheck.pl - Expect Test Program # my $VERSION=".01"; use strict; use warnings; use diagnostics; use Expect; my $timeout=5; foreach my $i (1..20) { my $spawn_ok="not OK"; my $exp = Expect->spawn("./unreliable.pl") or die "Cannot spawn unreliable process $!\n"; $exp->expect($timeout, [ 'It worked', sub { $spawn_ok = "OK"; exp_continue; } ], [ timeout => sub { print "Process timed out.\n"; } ] ); print "Status: $spawn_ok\n"; }
12/13/2002
Download:
Expect-1.15.tar.gz

Expect.pm - Expect for Perl

1.15

use Expect;
# create an Expect object by spawning another process
my $exp = Expect->spawn($command, @params)
or die "Cannot spawn $command: $!\n";
# or by using an already opened filehandle
my $exp = Expect->exp_init(\*FILEHANDLE);
# if you prefer the OO mindset:
my $exp = new Expect;
$exp->raw_pty(1);
$exp->spawn($command, @parameters)
or die "Cannot spawn $command: $!\n";
# send some string there:
$exp->send("string\n");
# or, for the filehandle mindset:
print $exp "string\n";
# then do some pattern matching with either the simple interface
$patidx = $exp->expect($timeout, @match_patterns);
# or multi-match on several spawned commands with callbacks,
# just like the Tcl version
$exp->expect($timeout,
[ qr/regex1/ => sub { my $exp = shift;
$exp->send("response\n");
exp_continue; } ],
[ "regexp2" , \&callback, @cbparms ],
);
# if no longer needed, do a soft_close to nicely shut down the command
$exp->soft_close();
# or be less patient with
$exp->hard_close();
Expect.pm is built to either spawn a process or take an existing filehandle and interact with it such that normally interactive tasks can be done without operator assistance. This concept makes more sense if you are already familiar with the versatile Tcl version of Expect. The public functions that make up Expect.pm are:
Expect->new()
Expect::interconnect(@objects_to_be_read_from)
Expect::test_handles($timeout, @objects_to_test)
Expect::version($version_requested | undef);
$object->spawn(@command)
$object->clear_accum()
$object->set_accum($value)
$object->debug($debug_level)
$object->exp_internal(0 | 1)
$object->notransfer(0 | 1)
$object->raw_pty(0 | 1)
$object->stty(@stty_modes) # See the IO::Stty docs
$object->slave()
$object->before();
$object->match();
$object->after();
$object->matchlist();
$object->match_number();
$object->error();
$object->command();
$object->exitstatus();
$object->pty_handle();
$object->do_soft_close();
$object->restart_timeout_upon_receive(0 | 1);
$object->interact($other_object, $escape_sequence)
$object->log_group(0 | 1 | undef)
$object->log_user(0 | 1 | undef)
$object->log_file("filename" | $filehandle | \&coderef | undef)
$object->manual_stty(0 | 1 | undef)
$object->match_max($max_buffersize or undef)
$object->pid();
$object->send_slow($delay, @strings_to_send)
$object->set_group(@listen_group_objects | undef)
$object->set_seq($sequence,\&function,\@parameters);
There are several configurable package variables that affect the behavior of Expect. They are:
$Expect::Debug; $Expect::Exp_Internal; $Expect::Log_Group; $Expect::Log_Stdout; $Expect::Manual_Stty; $Expect::Multiline_Matching; $Expect::Do_Soft_Close;

The Expect module is a successor of Comm.pl and a descendent of Chat.pl. It more closely ressembles the Tcl Expect language than its predecessors. It does not contain any of the networking code found in Comm.pl. I suspect this would be obsolete anyway given the advent of IO::Socket and external tools such as netcat.
Expect.pm is an attempt to have more of a switch() & case feeling to make decision processing more fluid. Three separate types of debugging have been implemented to make code production easier.
It is now possible to interconnect multiple file handles (and processes) much like Tcl's Expect. An attempt was made to enable all the features of Tcl's Expect without forcing Tcl on the victim programmer :-) .

undef if the fork was unsuccessful or the command could not be found.
spawn() passes its parameters unchanged to Perls exec(), so look there for detailed
semantics.Note that if spawn cannot exec() the given command, the Expect object is still valid and the next expect() will see "Cannot exec", so you can use that for error handling.
Also note that you cannot reuse an object with an already spawned command, even if that command has exited. Sorry, but you have to allocate a new object...
The '3' setting is new with 1.05, and adds the additional functionality of having the _full_ accumulated buffer printed every time data is read from an Expect object. This was implemented by request. I recommend against using this unless you think you need it as it can create quite a quantity of output under some circumstances..
$object->slave->stty(qw(raw -echo));
Typical values are 'sane', 'raw', and 'raw -echo'. Note that I recommend setting the terminal to 'raw' or 'raw -echo', as this avoids a lot of hassle and gives pipe-like (i.e. transparent) behaviour (without the buffering issue).
expect($timeout,
'-i', [ $obj1, $obj2, ... ],
[ $re_pattern, sub { ...; exp_continue; }, @subparms, ],
[ 'eof', sub { ... } ],
[ 'timeout', sub { ... }, \$subparm1 ],
'-i', [ $objn, ...],
'-ex', $exact_pattern, sub { ... },
$exact_pattern, sub { ...; exp_continue_timeout; },
'-re', $re_pattern, sub { ... },
'-i', \@object_list, @pattern_list,
...);
Simple interface:
Given $timeout in seconds Expect will wait for $object's handle to produce one of the match_patterns. Due to o/s limitations $timeout should be a round number. If $timeout is 0 Expect will check one time to see if $object's handle contains any of the match_patterns. If $timeout is undef Expect will wait forever for a pattern to match.
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).
$matched_pattern_position will contain the value that would have been returned if expect() had been called in a scalar context. $error is the error that occurred that caused expect() to return. $error will contain a number followed by a string equivalent expressing the nature of the error. Possible values are undef, indicating no error, '1:TIMEOUT' indicating that $timeout seconds had elapsed without a match, '2:EOF' indicating an eof was read from $object, '3: spawn id($fileno) died' indicating that the process exited before matching and '4:$!' indicating whatever error was set in $ERRNO during the last read on $object's handle. All handles indicated by set_group plus STDOUT will have all data to come out of $object printed to them during expect() if log_group and log_stdout are set.
Changed from older versions is the regular expression handling. By default now all strings passed to expect() are treated as literals. To match a regular expression pass '-re' as a parameter in front of the pattern you want to match as a regexp.
Example:
$object->expect(15, 'match me exactly','-re','match\s+me\s+exactly');
This change makes it possible to match literals and regular expressions in the same expect() call.
Also new is multiline matching. ^ will now match the beginning of lines. Unfortunately, because perl doesn't use $/ in determining where lines break using $ to find the end of a line frequently doesn't work. This is because your terminal is returning "\r\n" at the end of every line. One way to check for a pattern at the end of a line would be to use \r?$ instead of $.
Example: Spawning telnet to a host, you might look for the escape character. telnet would return to you "\r\nEscape character is '^]'.\r\n". To find this you might use $match='^Escape char.*\.\r?$';
$telnet->expect(10,'-re',$match);
New more Tcl/Expect-like interface:
It's now possible to expect on more than one connection at a time by specifying
'-i' and a single Expect object or a ref to an array containing
Expect objects, e.g.
expect($timeout,
'-i', $exp1, @patterns_1,
'-i', [ $exp2, $exp3 ], @patterns_2_3,
)
Furthermore, patterns can now be specified as array refs containing [$regexp, sub { ...}, @optional_subprams] . When the pattern matches, the subroutine is called with parameters ($matched_expect_obj, @optional_subparms). The subroutine can return the symbol `exp_continue' to continue the expect matching with timeout starting anew or return the symbol `exp_continue_timeout' for continuing expect without resetting the timeout count.
$exp->expect($timeout,
[ qr/username: /i, sub { my $self = shift;
$self->send("$username\n");
exp_continue; }],
[ qr/password: /i, sub { my $self = shift;
$self->send("$password\n");
exp_continue; }],
$shell_prompt);
`expect' is now exported by default.
$exp->restart_timeout_upon_receive(1);
$exp->expect($timeout,
[ timeout => \&report_timeout ],
[ qr/pattern/ => \&handle_pattern],
);
Now the timeout isn't triggered if the command produces any kind of output, i.e. is still alive, but you can act upon patterns in the output.
$exp->set_accum($exp->after);
to their callback, e.g.
$exp->notransfer(1);
$exp->expect($timeout,
# accumulator not truncated, pattern1 will match again
[ "pattern1" => sub { my $self = shift;
...
} ],
# accumulator truncated, pattern2 will not match again
[ "pattern2" => sub { my $self = shift;
...
$self->set_accum($self->after());
} ],
);
This is only a temporary fix until I can rewrite the pattern matching part so it can take that additional -notransfer argument.
For a generic way to interconnect processes, take a look at IPC::Run.
\*FILEHANDLE, $escape_sequence) Expect::interconnect($object , ...).
Default value is on. During creation of $object the setting will match the value
of $Expect::Log_Group, normally 1. $object->log_file("filename", "w");
Returns the logfilehandle.
If called with an undef value, stops logging and closes logfile:
$object->log_file(undef);
If called without argument, returns the logfilehandle:
$fh = $object->log_file();
Can be set to a code ref, which will be called instead of printing to the logfile:
$object->log_file(\&myloggerfunc);
Defaults to 1. Affects whether or not expect() uses the /m flag for
doing regular expression matching. If set to 1 /m is used.
This makes a difference when you are trying to match ^ and $. If
you have this on you can match lines in the middle of a page of output
using ^ and $ instead of it matching the beginning and end of the entire
expression. I think this is handy.

Lee Eakin <leakin@japh.itg.ti.com> has ported the kibitz script from Tcl/Expect to Perl/Expect. You can find it in the examples/ subdir. Thanks Lee!
Historical notes:
There are still a few lines of code dating back to the inspirational Comm.pl and Chat.pl modules without which this would not have been possible. Kudos to Eric Arnold <Eric.Arnold@Sun.com> and Randal 'Nuke your NT box with one line of perl code' Schwartz<merlyn@stonehenge.com> for making these available to the perl public.
As of .98 I think all the old code is toast. No way could this have been done without it though. Special thanks to Graham Barr for helping make sense of the IO::Handle stuff as well as providing the highly recommended IO::Tty module.

Mark Rogaski <rogaski@att.com> wrote:
"I figured that you'd like to know that Expect.pm has been very useful to AT&T Labs over the past couple of years (since I first talked to Austin about design decisions). We use Expect.pm for managing the switches in our network via the telnet interface, and such automation has significantly increased our reliability. So, you can honestly say that one of the largest digital networks in existence (AT&T Frame Relay) uses Expect.pm quite extensively."

This is a growing collection of things that might help. Please send you questions that are not answered here to RGiersig@cpan.org
Expect itself doesn't have real system dependencies, but the underlying IO::Tty needs pseudoterminals. IO::Stty uses POSIX.pm and Fcntl.pm.
I have used it on Solaris, Linux and AIX, others report *BSD and OSF as working. Generally, any modern POSIX Unix should do, but there are exceptions to every rule. Feedback is appreciated.
See IO::Tty for a list of verified systems.
Up to now, the answer was 'No', but this has changed.
You still cannot use ActivePerl, but if you use the Cygwin environment (http://sources.redhat.com), which brings its own perl, and have the latest IO::Tty (v0.05 or later) installed, it should work (feedback appreciated).
The tutorial is hopelessly out of date and needs a serious overhaul. I appologize for this, I have concentrated my efforts mainly on the functionality. Volunteers welcomed.
If you set
$Expect::Exp_Internal = 1;
Expect will tell you very verbosely what it is receiving and sending, what matching it is trying and what it found. You can do this on a per-command base with
$exp->exp_internal(1);
You can also set
$Expect::Debug = 1; # or 2, 3 for more verbose output
or
$exp->debug(1);
which gives you even more output.
Yes, just set
$Expect::Log_Stdout = 0;
to globally disable it or
$exp->log_stdout(0);
for just that command. 'log_user' is provided as an alias so Tcl/Expect user get a DWIM experience... :-)
This is caused by the pty, which has probably 'echo' enabled. A solution would
be to set the pty to raw mode, which in general is cleaner for communication between
two programs (no more unexpected character translations). Unfortunately this would
break a lot of old code that sends "\r" to the program instead of "\n" (translating
this is also handled by the pty), so I won't add this to Expect just like that.
But feel free to experiment with $exp->raw_pty(1).
A: You can send any characters to a process with the print command. To represent a control character in Perl, use \c followed by the letter. For example, control-G can be represented with "\cG" . Note that this will not work if you single-quote your string. So, to send control-C to a process in $exp, do:
print $exp "\cC";
Or, if you prefer:
$exp->send("\cC");
The ability to include control characters in a string like this is provided by Perl, not by Expect.pm . Trying to learn Expect.pm without a thorough grounding in Perl can be very daunting. We suggest you look into some of the excellent Perl learning material, such as the books _Programming Perl_ and _Learning Perl_ by O'Reilly, as well as the extensive online Perl documentation available through the perldoc command.
You could be exiting too fast without giving the spawned program enough time to finish. Try adding $exp->soft_close() to terminate the program gracefully or do an expect() for 'eof'.
Alternatively, try adding a 'sleep 1' after you spawn() the program. It could be that pty creation on your system is just slow (but this is rather improbable if you are using the latest IO-Tty).
You shouldn't use Expect for this. Putting passwords, especially root passwords, into scripts in clear text can mean severe security problems. I strongly recommend using other means. For 'su', consider switching to 'sudo', which gives you root access on a per-command and per-user basis without the need to enter passwords. 'ssh'/'scp' can be set up with RSA authentication without passwords. 'rsh' can use the .rhost mechanism, but I'd strongly suggest to switch to 'ssh'; to mention 'rsh' and 'security' in the same sentence makes an oxymoron.
It will work for 'telnet', though, and there are valid uses for it, but you still might want to consider using 'ssh', as keeping cleartext passwords around is very insecure.
Are you sure there is no other, easier way? As a rule of thumb, Expect is useful
for automating things that expect to talk to a human, where no formal standard applies.
For other tasks that do follow a well-defined protocol, there are often better-suited
modules that already can handle those protocols. Don't try to do HTTP requests by
spawning telnet to port 80, use LWP instead. To automate FTP, take a look at
Net::FTP
or ncftp (http://www.ncftp.org).
You don't use a screwdriver to hammer in your nails either, or do you?
Use
$exp->log_file("filename");
or
$exp->log_file($filehandle);
or even
$exp->log_file(\&log_procedure);
for maximum flexibility.
Note that the logfile is appended to by default, but you can specify an optional mode "w" to truncate the logfile:
$exp->log_file("filename", "w");
To stop logging, just call it with a false argument:
$exp->log_file(undef);
To globally unset multi-line matching for all regexps:
$Expect::Multiline_Matching = 0;
You can do that on a per-regexp basis by stating (?-m) inside the
regexp (you need perl5.00503 or later for that).
You can use the -i parameter to specify a single object or a list of Expect objects. All following patterns will be evaluated against that list.
You can specify -i multiple times to create groups of objects and patterns to match against within the same expect statement.
This works just like in Tcl/Expect.
See the source example below.
Well, pty handling is really a black magic, as it is extremely system dependend. I have extensively revised IO-Tty, so these problems should be gone.
If your system is listed in the "verified" list of IO::Tty, you probably have some non-standard setup, e.g. you compiled your Linux-kernel yourself and disabled ptys. Please ask your friendly sysadmin for help.
If your system is not listed, unpack the latest version of IO::Tty, do a 'perl
Makefile.PL; make; make test; uname -a' and send me the results and
I'll see what I can deduce from that.
[ Are you sure you need Expect for this? How about qx() or open("prog|")? ]
By using expect without any patterns to match.
$process->expect(undef); # Forever until EOF $process->expect($timeout); # For a few seconds $process->expect(0); # Is there anything ready on the handle now?
$read = $process->before();
Find it on CPAN as IO-Tty, which provides both.
What's happening is you are closing the handle before passwd exits. When you close the handle to a process, it is sent a signal (SIGPIPE?) telling it that STDOUT has gone away. The default behavior for processes is to die in this circumstance. Two ways you can make this not happen are:
$process->soft_close();
This will wait 15 seconds for a process to come up with an EOF by itself before killing it.
$process->expect(undef);
This will wait forever for the process to match an empty set of patterns. It will return when the process hits an EOF.
As a rule, you should always expect() the result of your transaction before you continue with processing.
Output is only printed to the logfile/group when Expect reads from the process, during expect(), send_slow() and interconnect(). One way you can force this is to make use of
$process->expect(undef);
and
$process->expect(0);
which will make expect() run with an empty pattern set forever or just for an instant to capture the output of $process. The output is available in the accumulator, so you can grab it using $process->before().
Tty settings are a major pain to keep track of. If you find unexpected behavior such as double-echoing or a frozen session, doublecheck the documentation for default settings. When in doubt, handle them yourself using $exp->stty() and manual_stty() functions. As of .98 you shouldn't have to worry about stty settings getting fouled unless you use interconnect or intentionally change them (like doing -echo to get a password).
If you foul up your terminal's tty settings, kill any hung processes and enter 'stty sane' at a shell prompt. This should make your terminal manageable again.
Note that IO::Tty returns ptys with your systems default setting regarding echoing, CRLF translation etc. and Expect does not change them. I have considered setting the ptys to 'raw' without any translation whatsoever, but this would break a lot of existing things, as '\r' translation would not work anymore. On the other hand, a raw pty works much like a pipe and is more WYGIWYE (what you get is what you expect), so I suggest you set it to 'raw' by yourself:
$exp = new Expect; $exp->raw_pty(1); $exp->spawn(...);
To disable echo:
$exp->slave->stty(qw(-echo));
You have to set the terminal screen size for that. Luckily, IO::Pty already has a method for that, so modify your code to look like this:
my $exp = new Expect;
$exp->slave->clone_winsize_from(\*STDIN);
$exp->spawn("telnet somehost);
Also, some applications need the TERM shell variable set so they know how to move the cursor across the screen. When logging in, the remote shell sends a query (Ctrl-Z I think) and expects the terminal to answer with a string, e.g. 'xterm'. If you really want to go that way (be aware, madness lies at its end), you can handle that and send back the value in $ENV{TERM}. This is only a hand-waving explanation, please figure out the details by yourself.
You have to catch the signal WINCH ("window size changed"), change the terminal size and propagate the signal to the spawned application:
my $exp = new Expect;
$exp->slave->clone_winsize_from(\*STDIN);
$exp->spawn("ssh somehost);
$SIG{WINCH} = \&winch;
sub winch {
$exp->slave->clone_winsize_from(\*STDIN);
kill WINCH => $exp->pid if $exp->pid;
$SIG{WINCH} = \&winch;
}
$exp->interact();
That means you are anal-retentive. :-) [Gotcha there!]
The OS may not be configured to grant additional pty's (pseudo terminals) to non-root users. /usr/sbin/mkpts should be 4755, not 700 for this to work. I don't know about security implications if you do this.
You are probably on one of the systems where the master doesn't get an EOF when the slave closes stdin/out/err.
One possible solution is when you spawn a process, follow it with a unique string that would indicate the process is finished.
$process = Expect->spawn('telnet somehost; echo ____END____');
And then $process->expect($timeout,'____END____','other','patterns');

my $exp = Expect->spawn("telnet localhost")
or die "Cannot spawn telnet: $!\n";;
my $spawn_ok;
$exp->expect($timeout,
[
qr'login: $',
sub {
$spawn_ok = 1;
my $fh = shift;
$fh->send("$username\n");
exp_continue;
}
],
[
'Password: $',
sub {
my $fh = shift;
print $fh "$password\n";
exp_continue;
}
],
[
eof =>
sub {
if ($spawn_ok) {
die "ERROR: premature EOF in login.\n";
} else {
die "ERROR: could not spawn telnet.\n";
}
}
],
[
timeout =>
sub {
die "No login.\n";
}
],
'-re', qr'[#>:] $', #' wait for shell prompt, then exit expect
);
foreach my $cmd (@list_of_commands) {
push @commands, Expect->spawn($cmd);
}
expect($timeout,
'-i', \@commands,
[
qr"pattern", # find this pattern in output of all commands
sub {
my $obj = shift; # object that matched
print $obj "something\n";
exp_continue; # we don't want to terminate the expect call
}
],
'-i', $some_other_command,
[
"some other pattern",
sub {
my ($obj, $parmref) = @_;
# ...
# now we exit the expect command
},
\$parm
],
);
my $exp = new Expect;
$exp->slave->clone_winsize_from(\*STDIN);
$exp->spawn("ssh somehost);
$SIG{WINCH} = \&winch;
sub winch {
$exp->slave->clone_winsize_from(\*STDIN);
kill WINCH => $exp->pid if $exp->pid;
$SIG{WINCH} = \&winch;
}
$exp->interact();

http://sourceforge.net/projects/expectperl/

There are two mailing lists available, expectperl-announce and expectperl-discuss, at
http://lists.sourceforge.net/lists/listinfo/expectperl-announce
and
http://lists.sourceforge.net/lists/listinfo/expectperl-discuss

(c) 1997 Austin Schutz <ASchutz@users.sourceforge.net> (retired)
expect() interface & functionality enhancements (c) 1999-2002 Roland Giersig.
This module is now maintained by Roland Giersig <RGiersig@cpan.org>
Building
<4 Dec 2000, 11:15 Uhr wars, als Nathaniel folgendes schrub:>
< Expect Perl Module >
> Dear cygwin,
>
> I am a fan of the work, and I would like to get the most of my cygwin
> experience.
>
> I am currently developing software that uses the expect perl module under a
> solaris environment. My question is the following. Has anyone ever
> successfully installed the module under cygwin? If yes, is it documented
> anywhere? If no, has anyone ever tried?
>
> Thank you,
> Nathaniel Fournier
> Nuance Communications
Yepp, no problems, you got some?
siebenschlaefer@LORELEY /
$ perl -MCPAN -e shell
cpan shell -- CPAN exploration and modules installation (v1.59_51)
ReadLine support available (try 'install Bundle::CPAN')
cpan>m /Expect/
CPAN: Storable loaded ok
Going to read /home/siebenschlaefer/.cpan/Metadata
Module id = Expect
DESCRIPTION Close relative of Don Libes' Expect in perl
CPAN_USERID RGIERSIG (Roland Giersig <RGiersig@cpan.org>)
CPAN_VERSION 1.10
CPAN_FILE R/RG/RGIERSIG/Expect-1.10.tar.gz
DSLI_STATUS RdpO (released,developer,perl,object-oriented)
INST_FILE (not installed)
cpan> install Expect
Running install for module Expect
Running make for R/RG/RGIERSIG/Expect-1.10.tar.gz
CPAN: MD5 loaded ok
CPAN: Net::FTP loaded ok
Fetching with Net::FTP:
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/R/RG/RGIERSIG/CHECKSUMS
Checksum for /home/siebenschlaefer/.cpan/sources/authors/id/R/RG/RGIERSIG/Expect-1.10.tar.gz ok
Scanning cache /home/siebenschlaefer/.cpan/build for sizes
Expect-1.10/
Expect-1.10/tutorial/
Expect-1.10/tutorial/README
Expect-1.10/tutorial/4.A.top
Expect-1.10/tutorial/5.A.top
Expect-1.10/tutorial/5.B.top
Expect-1.10/tutorial/6.B.modem-init
Expect-1.10/tutorial/6.A.smtp-verify
Expect-1.10/tutorial/2.A.ftp
Expect-1.10/tutorial/1.A.Intro
Expect-1.10/tutorial/2.B.rlogin
Expect-1.10/tutorial/3.A.debugging
Expect-1.10/Expect_FAQ.pod
Expect-1.10/Expect.pm
Expect-1.10/FAQ.old
Expect-1.10/Expect_intro.pod
Expect-1.10/MANIFEST
Expect-1.10/Makefile.PL
Expect-1.10/Changes
Expect-1.10/Expect.pod
Expect-1.10/README
CPAN.pm: Going to build R/RG/RGIERSIG/Expect-1.10.tar.gz
Warning: prerequisite IO::Pty failed to load: Can't locate IO/Pty.pm in @INC (@INC contains: /usr/local/lib/perl5/5.7.0/cygwin-multi /usr/local/lib/perl5/5.7.0 /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi /usr/local/lib/perl5/site_perl
/5.7.0 /usr/local/lib/perl5/site_perl .) at (eval 4) line 3.
Warning: prerequisite IO::Stty failed to load: Can't locate IO/Stty.pm in @INC (@INC contains: /usr/local/lib/perl5/5.7.0/cygwin-multi /usr/local/lib/perl5/5.7.0 /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi /usr/local/lib/perl5/site_pe
rl/5.7.0 /usr/local/lib/perl5/site_perl .) at (eval 5) line 3.
Warning: prerequisite IO::Tty failed to load: Can't locate IO/Tty.pm in @INC (@INC contains: /usr/local/lib/perl5/5.7.0/cygwin-multi /usr/local/lib/perl5/5.7.0 /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi /usr/local/lib/perl5/site_perl
/5.7.0 /usr/local/lib/perl5/site_perl .) at (eval 6) line 3.
Checking if your kit is complete...
Looks good
Writing Makefile for Expect
---- Unsatisfied dependencies detected during [R/RG/RGIERSIG/Expect-1.10.tar.gz] -----
IO::Tty
IO::Stty
IO::Pty
Running make test
Delayed until after prerequisites
Running make install
Delayed until after prerequisites
Running install for module IO::Tty
Running make for G/GB/GBARR/IO-Tty-0.04.tar.gz
Fetching with Net::FTP:
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/G/GB/GBARR/IO-Tty-0.04.tar.gz
Checksum for /home/siebenschlaefer/.cpan/sources/authors/id/G/GB/GBARR/IO-Tty-0.04.tar.gz ok
IO-Tty-0.04/
IO-Tty-0.04/MANIFEST
IO-Tty-0.04/ChangeLog
IO-Tty-0.04/IO-Tty.ppd
IO-Tty-0.04/Makefile.PL
IO-Tty-0.04/Pty.pm
IO-Tty-0.04/COPYING
IO-Tty-0.04/Tty.xs
IO-Tty-0.04/Tty.pm
IO-Tty-0.04/README
IO-Tty-0.04/try
CPAN.pm: Going to build G/GB/GBARR/IO-Tty-0.04.tar.gz
Looking for ttyname()....Found
Checking if your kit is complete...
Looks good
Writing Makefile for IO::Tty
cp Pty.pm blib/lib/IO/Pty.pm
cp Tty.pm blib/lib/IO/Tty.pm
/usr/local/bin/perl -I/usr/local/lib/perl5/5.7.0/cygwin-multi -I/usr/local/lib/perl5/5.7.0 /usr/local/lib/perl5/5.7.0/ExtUtils/xsubpp -typemap /usr/local/lib/perl5/5.7.0/ExtUtils/typemap Tty.xs > Tty.xsc && mv Tty.xsc Tty.c
gcc -c -DPERL_USE_SAFE_PUTENV -fno-strict-aliasing -I/usr/local/include -DUSEIMPORTLIB -O2 -DVERSION=\"0.04\" -DXS_VERSION=\"0.04\" -I/usr/local/lib/perl5/5.7.0/cygwin-multi/CORE -DHAS_TTYNAME Tty.c
Tty.xs: In function `xsignal':
Tty.xs:349: warning: assignment from incompatible pointer type
Tty.xs:354: warning: return from incompatible pointer type
Running Mkbootstrap for IO::Tty ()
chmod 644 Tty.bs
rm -f blib/arch/auto/IO/Tty/Tty.dll
LD_RUN_PATH="" ld2 -o blib/arch/auto/IO/Tty/Tty.dll -s -L/usr/local/lib Tty.o /usr/local/lib/perl5/5.7.0/cygwin-multi/CORE/libperl5_7_0.a
dllwrap --dllname Tty.dll --driver-name gcc --dlltool dlltool --export-all-symbols --as as --output-def libTty.def --output-lib libTty.a \
-s -L/usr/local/lib Tty.o /usr/local/lib/perl5/5.7.0/cygwin-multi/CORE/libperl5_7_0.a
dllwrap: no export definition file provided
dllwrap: creating one, but that may not be what you want
mv Tty.dll libTty.a blib/arch/auto/IO/Tty/
chmod 755 blib/arch/auto/IO/Tty/Tty.dll
cp Tty.bs blib/arch/auto/IO/Tty/Tty.bs
chmod 644 blib/arch/auto/IO/Tty/Tty.bs
Manifying blib/man3/IO.Pty.3
/usr/bin/make -- OK
Running make test
No tests defined for IO::Tty extension.
/usr/bin/make test -- OK
Running make install
Installing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/auto/IO/Tty/libTty.a
Installing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/auto/IO/Tty/Tty.bs
Installing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/auto/IO/Tty/Tty.dll
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/IO/Pty.pm
Installing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/IO/Tty.pm
Installing /usr/local/man/man3/IO.Pty.3
Writing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/auto/IO/Tty/.packlist
Appending installation info to /usr/local/lib/perl5/5.7.0/cygwin-multi/perllocal.pod
/usr/bin/make install -- OK
Running install for module IO::Stty
Running make for A/AU/AUSCHUTZ/IO-Stty-.02.tar.gz
Fetching with Net::FTP:
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/A/AU/AUSCHUTZ/IO-Stty-.02.tar.gz
Fetching with Net::FTP:
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/A/AU/AUSCHUTZ/CHECKSUMS
Checksum for /home/siebenschlaefer/.cpan/sources/authors/id/A/AU/AUSCHUTZ/IO-Stty-.02.tar.gz ok
IO-Stty-.02/
IO-Stty-.02/stty.txt
IO-Stty-.02/stty.pl
IO-Stty-.02/Makefile.PL
IO-Stty-.02/Stty.pm
IO-Stty-.02/README
CPAN.pm: Going to build A/AU/AUSCHUTZ/IO-Stty-.02.tar.gz
Writing Makefile for IO::Stty
cp stty.pl blib/lib/IO/stty.pl
cp Stty.pm blib/lib/IO/Stty.pm
/usr/bin/make -- OK
Running make test
No tests defined for IO::Stty extension.
/usr/bin/make test -- OK
Running make install
Installing /usr/local/lib/perl5/site_perl/5.7.0/IO/stty.pl
Installing /usr/local/lib/perl5/site_perl/5.7.0/IO/Stty.pm
Writing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/auto/IO/Stty/.packlist
Appending installation info to /usr/local/lib/perl5/5.7.0/cygwin-multi/perllocal.pod
/usr/bin/make install -- OK
IO::Pty is up to date.
Running make for R/RG/RGIERSIG/Expect-1.10.tar.gz
Is already unwrapped into directory /home/siebenschlaefer/.cpan/build/Expect-1.10
CPAN.pm: Going to build R/RG/RGIERSIG/Expect-1.10.tar.gz
cp Expect_intro.pod blib/lib/Expect_intro.pod
cp Expect.pod blib/lib/Expect.pod
cp Expect.pm blib/lib/Expect.pm
cp Expect_FAQ.pod blib/lib/Expect_FAQ.pod
Manifying blib/man3/Expect_intro.3
Manifying blib/man3/Expect.3
Manifying blib/man3/Expect_FAQ.3
/usr/bin/make -- OK
Running make test
No tests defined for Expect extension.
/usr/bin/make test -- OK
Running make install
Installing /usr/local/lib/perl5/site_perl/5.7.0/Expect.pm
Installing /usr/local/lib/perl5/site_perl/5.7.0/Expect.pod
Installing /usr/local/lib/perl5/site_perl/5.7.0/Expect_FAQ.pod
Installing /usr/local/lib/perl5/site_perl/5.7.0/Expect_intro.pod
Installing /usr/local/man/man3/Expect.3
Installing /usr/local/man/man3/Expect_FAQ.3
Installing /usr/local/man/man3/Expect_intro.3
Writing /usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi/auto/Expect/.packlist
Appending installation info to /usr/local/lib/perl5/5.7.0/cygwin-multi/perllocal.pod
/usr/bin/make install -- OK
cpan> exit
Lockfile removed.
siebenschlaefer@LORELEY /
$ perl -V
Summary of my perl5 (revision 5.0 version 7 subversion 0) configuration:
Platform:
osname=cygwin, osvers=1.1.6(0.3032), archname=cygwin-multi
uname='cygwin_nt-4.0 loreley 1.1.6(0.3032) 2000-11-21 21:00 i686 unknown '
config_args='-des -Dusedevel -Dusemultiplicity -Duseperlio -Duselargefiles -Dinstallprefix=/usr/local -Dprefix=/usr/local'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
Compiler:
cc='gcc', ccflags ='-DPERL_USE_SAFE_PUTENV -fno-strict-aliasing -I/usr/local/include',
optimize='-O2',
cppflags='-DPERL_USE_SAFE_PUTENV -fno-strict-aliasing -I/usr/local/include'
ccversion='', gccversion='2.95.2-5 19991024 (cygwin experimental)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4
alignbytes=8, usemymalloc=y, prototype=define
Linker and Libraries:
ld='ld2', ldflags =' -s -L/usr/local/lib'
libpth=/usr/local/lib /usr/lib /lib
libs=-lgdbm -ldb -lcrypt -lcygipc
perllibs=-lcrypt -lcygipc
libc=/usr/lib/libc.a, so=dll, useshrplib=true, libperl=libperl5_7_0.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' -s'
cccdlflags=' ', lddlflags=' -s -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: MULTIPLICITY USE_LARGE_FILES PERL_IMPLICIT_CONTEXT
Locally applied patches:
DEVEL7978
Built under cygwin
Compiled at Dec 5 2000 20:35:20
@INC:
/usr/local/lib/perl5/5.7.0/cygwin-multi
/usr/local/lib/perl5/5.7.0
/usr/local/lib/perl5/site_perl/5.7.0/cygwin-multi
/usr/local/lib/perl5/site_perl/5.7.0
/usr/local/lib/perl5/site_perl
.
siebenschlaefer@LORELEY /
$
gph
--
Gerrit Peter Haase
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Last modified: October 10, 2009