Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man3 / Expect.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Expect 3"
132.TH Expect 3 "2006-07-19" "perl v5.8.8" "User Contributed Perl Documentation"
133.SH "NAME"
134Expect.pm \- Expect for Perl
135.SH "VERSION"
136.IX Header "VERSION"
1371.20
138.SH "SYNOPSIS"
139.IX Header "SYNOPSIS"
140.Vb 1
141\& use Expect;
142.Ve
143.PP
144.Vb 3
145\& # create an Expect object by spawning another process
146\& my $exp = Expect->spawn($command, @params)
147\& or die "Cannot spawn $command: $!\en";
148.Ve
149.PP
150.Vb 2
151\& # or by using an already opened filehandle (e.g. from Net::Telnet)
152\& my $exp = Expect->exp_init(\e*FILEHANDLE);
153.Ve
154.PP
155.Vb 5
156\& # if you prefer the OO mindset:
157\& my $exp = new Expect;
158\& $exp->raw_pty(1);
159\& $exp->spawn($command, @parameters)
160\& or die "Cannot spawn $command: $!\en";
161.Ve
162.PP
163.Vb 2
164\& # send some string there:
165\& $exp->send("string\en");
166.Ve
167.PP
168.Vb 2
169\& # or, for the filehandle mindset:
170\& print $exp "string\en";
171.Ve
172.PP
173.Vb 2
174\& # then do some pattern matching with either the simple interface
175\& $patidx = $exp->expect($timeout, @match_patterns);
176.Ve
177.PP
178.Vb 8
179\& # or multi-match on several spawned commands with callbacks,
180\& # just like the Tcl version
181\& $exp->expect($timeout,
182\& [ qr/regex1/ => sub { my $exp = shift;
183\& $exp->send("response\en");
184\& exp_continue; } ],
185\& [ "regexp2" , \e&callback, @cbparms ],
186\& );
187.Ve
188.PP
189.Vb 2
190\& # if no longer needed, do a soft_close to nicely shut down the command
191\& $exp->soft_close();
192.Ve
193.PP
194.Vb 2
195\& # or be less patient with
196\& $exp->hard_close();
197.Ve
198.PP
199Expect.pm is built to either spawn a process or take an existing filehandle
200and interact with it such that normally interactive tasks can be done
201without operator assistance. This concept makes more sense if you are
202already familiar with the versatile Tcl version of Expect.
203The public functions that make up Expect.pm are:
204.PP
205.Vb 34
206\& Expect->new()
207\& Expect::interconnect(@objects_to_be_read_from)
208\& Expect::test_handles($timeout, @objects_to_test)
209\& Expect::version($version_requested | undef);
210\& $object->spawn(@command)
211\& $object->clear_accum()
212\& $object->set_accum($value)
213\& $object->debug($debug_level)
214\& $object->exp_internal(0 | 1)
215\& $object->notransfer(0 | 1)
216\& $object->raw_pty(0 | 1)
217\& $object->stty(@stty_modes) # See the IO::Stty docs
218\& $object->slave()
219\& $object->before();
220\& $object->match();
221\& $object->after();
222\& $object->matchlist();
223\& $object->match_number();
224\& $object->error();
225\& $object->command();
226\& $object->exitstatus();
227\& $object->pty_handle();
228\& $object->do_soft_close();
229\& $object->restart_timeout_upon_receive(0 | 1);
230\& $object->interact($other_object, $escape_sequence)
231\& $object->log_group(0 | 1 | undef)
232\& $object->log_user(0 | 1 | undef)
233\& $object->log_file("filename" | $filehandle | \e&coderef | undef)
234\& $object->manual_stty(0 | 1 | undef)
235\& $object->match_max($max_buffersize or undef)
236\& $object->pid();
237\& $object->send_slow($delay, @strings_to_send)
238\& $object->set_group(@listen_group_objects | undef)
239\& $object->set_seq($sequence,\e&function,\e@parameters);
240.Ve
241.PP
242There are several configurable package variables that affect the behavior of Expect. They are:
243.PP
244.Vb 7
245\& $Expect::Debug;
246\& $Expect::Exp_Internal;
247\& $Expect::Log_Group;
248\& $Expect::Log_Stdout;
249\& $Expect::Manual_Stty;
250\& $Expect::Multiline_Matching;
251\& $Expect::Do_Soft_Close;
252.Ve
253.SH "DESCRIPTION"
254.IX Header "DESCRIPTION"
255The Expect module is a successor of Comm.pl and a descendent of Chat.pl. It
256more closely ressembles the Tcl Expect language than its predecessors. It
257does not contain any of the networking code found in Comm.pl. I suspect this
258would be obsolete anyway given the advent of IO::Socket and external tools
259such as netcat.
260.PP
261Expect.pm is an attempt to have more of a \fIswitch()\fR & case feeling to make
262decision processing more fluid. Three separate types of debugging have
263been implemented to make code production easier.
264.PP
265It is now possible to interconnect multiple file handles (and processes) much
266like Tcl's Expect. An attempt was made to enable all the features of Tcl's
267Expect without forcing Tcl on the victim programmer :\-) .
268.PP
269Please, before you consider using Expect, read the FAQs about
270\&\*(L"I want to automate password entry for su/ssh/scp/rsh/...\*(R" and
271\&\*(L"I want to use Expect to automate [anything with a buzzword]...\*(R"
272.SH "USAGE"
273.IX Header "USAGE"
274.IP "new Expect ()" 4
275.IX Item "new Expect ()"
276Creates a new Expect object, i.e. a pty. You can change parameters on
277it before actually spawning a command. This is important if you want
278to modify the terminal settings for the slave. See \fIslave()\fR below.
279The object returned is actually a reblessed IO::Pty filehandle, so see
280there for additional methods.
281.IP "Expect\->exp_init(\e*FILEHANDLE) \fIor\fR" 4
282.IX Item "Expect->exp_init(*FILEHANDLE) or"
283.PD 0
284.IP "Expect\->init(\e*FILEHANDLE)" 4
285.IX Item "Expect->init(*FILEHANDLE)"
286.PD
287Initializes \f(CW$new_handle_object\fR for use with other Expect functions. It must
288be passed a \fB_reference_\fR to \s-1FILEHANDLE\s0 if you want it to work properly.
289IO::File objects are preferable. Returns a reference to the newly created
290object.
291.ie n .IP "Expect\->spawn($command, @parameters\fR) \fIor" 4
292.el .IP "Expect\->spawn($command, \f(CW@parameters\fR) \fIor\fR" 4
293.IX Item "Expect->spawn($command, @parameters) or"
294.PD 0
295.ie n .IP "$object\->spawn($command, @parameters\fR) \fIor" 4
296.el .IP "$object\->spawn($command, \f(CW@parameters\fR) \fIor\fR" 4
297.IX Item "$object->spawn($command, @parameters) or"
298.ie n .IP "new Expect ($command, @parameters)" 4
299.el .IP "new Expect ($command, \f(CW@parameters\fR)" 4
300.IX Item "new Expect ($command, @parameters)"
301.PD
302Forks and execs \f(CW$command\fR. Returns an Expect object upon success or
303\&\f(CW\*(C`undef\*(C'\fR if the fork was unsuccessful or the command could not be
304found. \fIspawn()\fR passes its parameters unchanged to Perls \fIexec()\fR, so
305look there for detailed semantics.
306.Sp
307Note that if spawn cannot \fIexec()\fR the given command, the Expect object
308is still valid and the next \fIexpect()\fR will see \*(L"Cannot exec\*(R", so you
309can use that for error handling.
310.Sp
311Also note that you cannot reuse an object with an already spawned
312command, even if that command has exited. Sorry, but you have to
313allocate a new object...
314.IP "$object\->debug(0 | 1 | 2 | 3 | undef)" 4
315.IX Item "$object->debug(0 | 1 | 2 | 3 | undef)"
316Sets debug level for \f(CW$object\fR. 1 refers to general debugging
317information, 2 refers to verbose debugging and 0 refers to no
318debugging. If you call \fIdebug()\fR with no parameters it will return the
319current debugging level. When the object is created the debugging
320level will match that \f(CW$Expect::Debug\fR, normally 0.
321.Sp
322The '3' setting is new with 1.05, and adds the additional
323functionality of having the _full_ accumulated buffer printed every
324time data is read from an Expect object. This was implemented by
325request. I recommend against using this unless you think you need it
326as it can create quite a quantity of output under some circumstances..
327.IP "$object\->exp_internal(1 | 0)" 4
328.IX Item "$object->exp_internal(1 | 0)"
329Sets/unsets 'exp_internal' debugging. This is similar in nature to its Tcl
330counterpart. It is extremely valuable when debugging \fIexpect()\fR sequences.
331When the object is created the exp_internal setting will match the value of
332\&\f(CW$Expect::Exp_Internal\fR, normally 0. Returns the current setting if called
333without parameters. It is highly recommended that you make use of the
334debugging features lest you have angry code.
335.IP "$object\->raw_pty(1 | 0)" 4
336.IX Item "$object->raw_pty(1 | 0)"
337Set pty to raw mode before spawning. This disables echoing, \s-1CR\-\s0>\s-1LF\s0
338translation and an ugly hack for broken Solaris TTYs (which send
339<space><backspace> to slow things down) and thus gives a more
340pipe-like behaviour (which is important if you want to transfer binary
341content). Note that this must be set \fIbefore\fR spawning the program.
342.IP "$object\->stty(qw(mode1 mode2...))" 4
343.IX Item "$object->stty(qw(mode1 mode2...))"
344Sets the tty mode for \f(CW$object\fR's associated terminal to the given
345modes. Note that on many systems the master side of the pty is not a
346tty, so you have to modify the slave pty instead, see next item. This
347needs IO::Stty installed, which is no longer required.
348.IP "$object\->\fIslave()\fR" 4
349.IX Item "$object->slave()"
350Returns a filehandle to the slave part of the pty. Very useful in modifying
351the terminal settings:
352.Sp
353.Vb 1
354\& $object->slave->stty(qw(raw -echo));
355.Ve
356.Sp
357Typical values are 'sane', 'raw', and 'raw \-echo'. Note that I
358recommend setting the terminal to 'raw' or 'raw \-echo', as this avoids
359a lot of hassle and gives pipe-like (i.e. transparent) behaviour
360(without the buffering issue).
361.IP "$object\->print(@strings) \fIor\fR" 4
362.IX Item "$object->print(@strings) or"
363.PD 0
364.IP "$object\->send(@strings)" 4
365.IX Item "$object->send(@strings)"
366.PD
367Sends the given strings to the spawned command. Note that the strings
368are not logged in the logfile (see print_log_file) but will probably
369be echoed back by the pty, depending on pty settings (default is echo)
370and thus end up there anyway. This must also be taken into account
371when \fIexpect()\fRing for an answer: the next string will be the command
372just sent. I suggest setting the pty to raw, which disables echo and
373makes the pty transparently act like a bidirectional pipe.
374.ie n .IP "$object\->expect($timeout, @match_patterns)" 4
375.el .IP "$object\->expect($timeout, \f(CW@match_patterns\fR)" 4
376.IX Item "$object->expect($timeout, @match_patterns)"
377or, more like Tcl/Expect,
378.Sp
379.Vb 11
380\& expect($timeout,
381\& '-i', [ $obj1, $obj2, ... ],
382\& [ $re_pattern, sub { ...; exp_continue; }, @subparms, ],
383\& [ 'eof', sub { ... } ],
384\& [ 'timeout', sub { ... }, \e$subparm1 ],
385\& '-i', [ $objn, ...],
386\& '-ex', $exact_pattern, sub { ... },
387\& $exact_pattern, sub { ...; exp_continue_timeout; },
388\& '-re', $re_pattern, sub { ... },
389\& '-i', \e@object_list, @pattern_list,
390\& ...);
391.Ve
392.Sp
393\&\fISimple interface:\fR
394.Sp
395Given \f(CW$timeout\fR in seconds Expect will wait for \f(CW$object\fR's handle to produce
396one of the match_patterns, which are matched exactly by default. If you
397want a regexp match, prefix the pattern with '\-re'.
398.Sp
399Due to o/s limitations \f(CW$timeout\fR should be a round number. If \f(CW$timeout\fR
400is 0 Expect will check one time to see if \f(CW$object\fR's handle contains
401any of the match_patterns. If \f(CW$timeout\fR is undef Expect
402will wait forever for a pattern to match.
403.Sp
404If called in a scalar context, \fIexpect()\fR will return the position of
405the matched pattern within \f(CW$match_patterns\fR, or undef if no pattern was
406matched. This is a position starting from 1, so if you want to know
407which of an array of \f(CW@matched_patterns\fR matched you should subtract one
408from the return value.
409.Sp
410If called in an array context \fIexpect()\fR will return
411($matched_pattern_position, \f(CW$error\fR, \f(CW$successfully_matching_string\fR,
412\&\f(CW$before_match\fR, and \f(CW$after_match\fR).
413.Sp
414$matched_pattern_position will contain the value that would have been
415returned if \fIexpect()\fR had been called in a scalar context. \f(CW$error\fR is
416the error that occurred that caused \fIexpect()\fR to return. \f(CW$error\fR will
417contain a number followed by a string equivalent expressing the nature
418of the error. Possible values are undef, indicating no error,
419\&'1:TIMEOUT' indicating that \f(CW$timeout\fR seconds had elapsed without a
420match, '2:EOF' indicating an eof was read from \f(CW$object\fR, '3: spawn
421id($fileno) died' indicating that the process exited before matching
422and '4:$!' indicating whatever error was set in \f(CW$ERRNO\fR during the last
423read on \f(CW$object\fR's handle. All handles indicated by set_group plus
424\&\s-1STDOUT\s0 will have all data to come out of \f(CW$object\fR printed to them
425during \fIexpect()\fR if log_group and log_stdout are set.
426.Sp
427Changed from older versions is the regular expression handling. By
428default now all strings passed to \fIexpect()\fR are treated as literals. To
429match a regular expression pass '\-re' as a parameter in front of the
430pattern you want to match as a regexp.
431.Sp
432Example:
433.Sp
434.Vb 1
435\& $object->expect(15, 'match me exactly','-re','match\es+me\es+exactly');
436.Ve
437.Sp
438This change makes it possible to match literals and regular expressions
439in the same \fIexpect()\fR call.
440.Sp
441Also new is multiline matching. ^ will now match the beginning of
442lines. Unfortunately, because perl doesn't use $/ in determining where
443lines break using $ to find the end of a line frequently doesn't work. This
444is because your terminal is returning \*(L"\er\en\*(R" at the end of every line. One
445way to check for a pattern at the end of a line would be to use \er?$ instead
446of $.
447.Sp
448Example: Spawning telnet to a host, you might look for the escape
449character. telnet would return to you \*(L"\er\enEscape character is
450\&'^]'.\er\en\*(R". To find this you might use \f(CW$match\fR='^Escape char.*\e.\er?$';
451.Sp
452.Vb 1
453\& $telnet->expect(10,'-re',$match);
454.Ve
455.Sp
456\&\fINew more Tcl/Expect\-like interface:\fR
457.Sp
458It's now possible to expect on more than one connection at a time by
459specifying '\f(CW\*(C`\-i\*(C'\fR' and a single Expect object or a ref to an array
460containing Expect objects, e.g.
461.Sp
462.Vb 4
463\& expect($timeout,
464\& '-i', $exp1, @patterns_1,
465\& '-i', [ $exp2, $exp3 ], @patterns_2_3,
466\& )
467.Ve
468.Sp
469Furthermore, patterns can now be specified as array refs containing
470[$regexp, sub { ...}, \f(CW@optional_subprams\fR] . When the pattern matches,
471the subroutine is called with parameters ($matched_expect_obj,
472\&\f(CW@optional_subparms\fR). The subroutine can return the symbol
473`exp_continue' to continue the expect matching with timeout starting
474anew or return the symbol `exp_continue_timeout' for continuing expect
475without resetting the timeout count.
476.Sp
477.Vb 8
478\& $exp->expect($timeout,
479\& [ qr/username: /i, sub { my $self = shift;
480\& $self->send("$username\en");
481\& exp_continue; }],
482\& [ qr/password: /i, sub { my $self = shift;
483\& $self->send("$password\en");
484\& exp_continue; }],
485\& $shell_prompt);
486.Ve
487.Sp
488`expect' is now exported by default.
489.IP "$object\->\fIexp_before()\fR \fIor\fR" 4
490.IX Item "$object->exp_before() or"
491.PD 0
492.IP "$object\->\fIbefore()\fR" 4
493.IX Item "$object->before()"
494.PD
495\&\fIbefore()\fR returns the 'before' part of the last \fIexpect()\fR call. If the last
496\&\fIexpect()\fR call didn't match anything, \fIexp_before()\fR will return the entire
497output of the object accumulated before the \fIexpect()\fR call finished.
498.Sp
499Note that this is something different than Tcl Expects \fIbefore()\fR!!
500.IP "$object\->\fIexp_after()\fR \fIor\fR" 4
501.IX Item "$object->exp_after() or"
502.PD 0
503.IP "$object\->\fIafter()\fR" 4
504.IX Item "$object->after()"
505.PD
506returns the 'after' part of the last \fIexpect()\fR call. If the last
507\&\fIexpect()\fR call didn't match anything, \fIexp_after()\fR will return \fIundef()\fR.
508.IP "$object\->\fIexp_match()\fR \fIor\fR" 4
509.IX Item "$object->exp_match() or"
510.PD 0
511.IP "$object\->\fImatch()\fR" 4
512.IX Item "$object->match()"
513.PD
514returns the string matched by the last \fIexpect()\fR call, undef if
515no string was matched.
516.IP "$object\->\fIexp_match_number()\fR \fIor\fR" 4
517.IX Item "$object->exp_match_number() or"
518.PD 0
519.IP "$object\->\fImatch_number()\fR" 4
520.IX Item "$object->match_number()"
521.PD
522\&\fIexp_match_number()\fR returns the number of the pattern matched by the last
523\&\fIexpect()\fR call. Keep in mind that the first pattern in a list of patterns is 1,
524not 0. Returns undef if no pattern was matched.
525.IP "$object\->\fIexp_matchlist()\fR \fIor\fR" 4
526.IX Item "$object->exp_matchlist() or"
527.PD 0
528.IP "$object\->\fImatchlist()\fR" 4
529.IX Item "$object->matchlist()"
530.PD
531\&\fIexp_matchlist()\fR returns a list of matched substrings from the brackets
532() inside the regexp that last matched. ($object\->matchlist)[0]
533thus corresponds to \f(CW$1\fR, ($object\->matchlist)[1] to \f(CW$2\fR, etc.
534.IP "$object\->\fIexp_error()\fR \fIor\fR" 4
535.IX Item "$object->exp_error() or"
536.PD 0
537.IP "$object\->\fIerror()\fR" 4
538.IX Item "$object->error()"
539.PD
540\&\fIexp_error()\fR returns the error generated by the last \fIexpect()\fR call if
541no pattern was matched. It is typically useful to examine the value returned by
542\&\fIbefore()\fR to find out what the output of the object was in determining
543why it didn't match any of the patterns.
544.IP "$object\->\fIclear_accum()\fR" 4
545.IX Item "$object->clear_accum()"
546Clear the contents of the accumulator for \f(CW$object\fR. This gets rid of
547any residual contents of a handle after \fIexpect()\fR or \fIsend_slow()\fR such
548that the next \fIexpect()\fR call will only see new data from \f(CW$object\fR. The
549contents of the accumulator are returned.
550.IP "$object\->set_accum($value)" 4
551.IX Item "$object->set_accum($value)"
552Sets the content of the accumulator for \f(CW$object\fR to \f(CW$value\fR. The
553previous content of the accumulator is returned.
554.IP "$object\->\fIexp_command()\fR \fIor\fR" 4
555.IX Item "$object->exp_command() or"
556.PD 0
557.IP "$object\->\fIcommand()\fR" 4
558.IX Item "$object->command()"
559.PD
560\&\fIexp_command()\fR returns the string that was used to spawn the command. Helpful
561for debugging and for reused patternmatch subroutines.
562.IP "$object\->\fIexp_exitstatus()\fR \fIor\fR" 4
563.IX Item "$object->exp_exitstatus() or"
564.PD 0
565.IP "$object\->\fIexitstatus()\fR" 4
566.IX Item "$object->exitstatus()"
567.PD
568Returns the exit status of \f(CW$object\fR (if it already exited).
569.IP "$object\->\fIexp_pty_handle()\fR \fIor\fR" 4
570.IX Item "$object->exp_pty_handle() or"
571.PD 0
572.IP "$object\->\fIpty_handle()\fR" 4
573.IX Item "$object->pty_handle()"
574.PD
575Returns a string representation of the attached pty, for example:
576`spawn \fIid\fR\|(5)' (pty has fileno 5), `handle \fIid\fR\|(7)' (pty was initialized
577from fileno 7) or `\s-1STDIN\s0'. Useful for debugging.
578.IP "$object\->restart_timeout_upon_receive(0 | 1)" 4
579.IX Item "$object->restart_timeout_upon_receive(0 | 1)"
580If this is set to 1, the expect timeout is retriggered whenever something
581is received from the spawned command. This allows to perform some
582aliveness testing and still expect for patterns.
583.Sp
584.Vb 5
585\& $exp->restart_timeout_upon_receive(1);
586\& $exp->expect($timeout,
587\& [ timeout => \e&report_timeout ],
588\& [ qr/pattern/ => \e&handle_pattern],
589\& );
590.Ve
591.Sp
592Now the timeout isn't triggered if the command produces any kind of output,
593i.e. is still alive, but you can act upon patterns in the output.
594.IP "$object\->notransfer(1 | 0)" 4
595.IX Item "$object->notransfer(1 | 0)"
596Do not truncate the content of the accumulator after a match.
597Normally, the accumulator is set to the remains that come after the
598matched string. Note that this setting is per object and not per
599pattern, so if you want to have normal acting patterns that truncate
600the accumulator, you have to add a
601.Sp
602.Vb 1
603\& $exp->set_accum($exp->after);
604.Ve
605.Sp
606to their callback, e.g.
607.Sp
608.Vb 12
609\& $exp->notransfer(1);
610\& $exp->expect($timeout,
611\& # accumulator not truncated, pattern1 will match again
612\& [ "pattern1" => sub { my $self = shift;
613\& ...
614\& } ],
615\& # accumulator truncated, pattern2 will not match again
616\& [ "pattern2" => sub { my $self = shift;
617\& ...
618\& $self->set_accum($self->after());
619\& } ],
620\& );
621.Ve
622.Sp
623This is only a temporary fix until I can rewrite the pattern matching
624part so it can take that additional \-notransfer argument.
625.IP "Expect::interconnect(@objects);" 4
626.IX Item "Expect::interconnect(@objects);"
627Read from \f(CW@objects\fR and print to their \f(CW@listen_groups\fR until an escape sequence
628is matched from one of \f(CW@objects\fR and the associated function returns 0 or undef.
629The special escape sequence '\s-1EOF\s0' is matched when an object's handle returns
630an end of file. Note that it is not necessary to include objects that only
631accept data in \f(CW@objects\fR since the escape sequence is _read_ from an object.
632Further note that the listen_group for a write-only object is always empty.
633Why would you want to have objects listening to \s-1STDOUT\s0 (for example)?
634By default every member of \f(CW@objects\fR _as well as every member of its listen
635group_ will be set to 'raw \-echo' for the duration of interconnection.
636Setting \f(CW$object\fR\->\fImanual_stty()\fR will stop this behavior per object.
637The original tty settings will be restored as interconnect exits.
638.Sp
639For a generic way to interconnect processes, take a look at IPC::Run.
640.IP "Expect::test_handles(@objects)" 4
641.IX Item "Expect::test_handles(@objects)"
642Given a set of objects determines which objects' handles have data ready
643to be read. \fBReturns an array\fR who's members are positions in \f(CW@objects\fR that
644have ready handles. Returns undef if there are no such handles ready.
645.IP "Expect::version($version_requested or undef);" 4
646.IX Item "Expect::version($version_requested or undef);"
647Returns current version of Expect. As of .99 earlier versions are not
648supported. Too many things were changed to make versioning possible.
649.ie n .IP "$object\->interact( ""\e*FILEHANDLE, $escape_sequence"")" 4
650.el .IP "$object\->interact( \f(CW\e*FILEHANDLE, $escape_sequence\fR)" 4
651.IX Item "$object->interact( *FILEHANDLE, $escape_sequence)"
652\&\fIinteract()\fR is essentially a macro for calling \fIinterconnect()\fR for
653connecting 2 processes together. \e*FILEHANDLE defaults to \e*STDIN and
654\&\f(CW$escape_sequence\fR defaults to undef. Interaction ceases when \f(CW$escape_sequence\fR
655is read from \fB\s-1FILEHANDLE\s0\fR, not \f(CW$object\fR. \f(CW$object\fR's listen group will
656consist solely of \e*FILEHANDLE for the duration of the interaction.
657\&\e*FILEHANDLE will not be echoed on \s-1STDOUT\s0.
658.IP "$object\->log_group(0 | 1 | undef)" 4
659.IX Item "$object->log_group(0 | 1 | undef)"
660Set/unset logging of \f(CW$object\fR to its 'listen group'. If set all objects
661in the listen group will have output from \f(CW$object\fR printed to them during
662\&\f(CW$object\fR\->\fIexpect()\fR, \f(CW$object\fR\->\fIsend_slow()\fR, and \f(CW\*(C`Expect::interconnect($object
663, ...)\*(C'\fR. Default value is on. During creation of \f(CW$object\fR the setting will
664match the value of \f(CW$Expect::Log_Group\fR, normally 1.
665.IP "$object\->log_user(0 | 1 | undef) \fIor\fR" 4
666.IX Item "$object->log_user(0 | 1 | undef) or"
667.PD 0
668.IP "$object\->log_stdout(0 | 1 | undef)" 4
669.IX Item "$object->log_stdout(0 | 1 | undef)"
670.PD
671Set/unset logging of object's handle to \s-1STDOUT\s0. This corresponds to Tcl's
672log_user variable. Returns current setting if called without parameters.
673Default setting is off for initialized handles. When a process object is
674created (not a filehandle initialized with exp_init) the log_stdout setting
675will match the value of \f(CW$Expect::Log_Stdout\fR variable, normally 1.
676If/when you initialize \s-1STDIN\s0 it is usually associated with a tty which
677will by default echo to \s-1STDOUT\s0 anyway, so be careful or you will have
678multiple echoes.
679.ie n .IP "$object\->log_file(""filename"" | $filehandle | \e&coderef | undef)" 4
680.el .IP "$object\->log_file(``filename'' | \f(CW$filehandle\fR | \e&coderef | undef)" 4
681.IX Item "$object->log_file(filename | $filehandle | &coderef | undef)"
682Log session to a file. All characters send to or received from the
683spawned process are written to the file. Normally appends to the
684logfile, but you can pass an additional mode of \*(L"w\*(R" to truncate the
685file upon \fIopen()\fR:
686.Sp
687.Vb 1
688\& $object->log_file("filename", "w");
689.Ve
690.Sp
691Returns the logfilehandle.
692.Sp
693If called with an undef value, stops logging and closes logfile:
694.Sp
695.Vb 1
696\& $object->log_file(undef);
697.Ve
698.Sp
699If called without argument, returns the logfilehandle:
700.Sp
701.Vb 1
702\& $fh = $object->log_file();
703.Ve
704.Sp
705Can be set to a code ref, which will be called instead of printing
706to the logfile:
707.Sp
708.Vb 1
709\& $object->log_file(\e&myloggerfunc);
710.Ve
711.IP "$object\->print_log_file(@strings)" 4
712.IX Item "$object->print_log_file(@strings)"
713Prints to logfile (if opened) or calls the logfile hook function.
714This allows the user to add arbitraty text to the logfile. Note that
715this could also be done as \f(CW$object\fR\->log_file\->\fIprint()\fR but would only
716work for log files, not code hooks.
717.IP "$object\->set_seq($sequence, \e&function, \e@function_parameters)" 4
718.IX Item "$object->set_seq($sequence, &function, @function_parameters)"
719During Expect\->\fIinterconnect()\fR if \f(CW$sequence\fR is read from \f(CW$object\fR &function
720will be executed with parameters \f(CW@function_parameters\fR. It is \fB_highly
721recommended_\fR that the escape sequence be a single character since the
722likelihood is great that the sequence will be broken into to separate reads
723from the \f(CW$object\fR's handle, making it impossible to strip \f(CW$sequence\fR from
724getting printed to \f(CW$object\fR's listen group. \e&function should be something
725like 'main::control_w_function' and \f(CW@function_parameters\fR should be an
726array defined by the caller, passed by reference to \fIset_seq()\fR.
727Your function should return a non-zero value if execution of \fIinterconnect()\fR
728is to resume after the function returns, zero or undefined if \fIinterconnect()\fR
729should return after your function returns.
730The special sequence '\s-1EOF\s0' matches the end of file being reached by \f(CW$object\fR.
731See \fIinterconnect()\fR for details.
732.IP "$object\->set_group(@listener_objects)" 4
733.IX Item "$object->set_group(@listener_objects)"
734@listener_objects is the list of objects that should have their handles
735printed to by \f(CW$object\fR when Expect::interconnect, \f(CW$object\fR\->\fIexpect()\fR or
736\&\f(CW$object\fR\->\fIsend_slow()\fR are called. Calling w/out parameters will return
737the current list of the listener objects.
738.IP "$object\->manual_stty(0 | 1 | undef)" 4
739.IX Item "$object->manual_stty(0 | 1 | undef)"
740Sets/unsets whether or not Expect should make reasonable guesses as to
741when and how to set tty parameters for \f(CW$object\fR. Will match
742\&\f(CW$Expect::Manual_Stty\fR value (normally 0) when \f(CW$object\fR is created. If called
743without parameters \fImanual_stty()\fR will return the current manual_stty setting.
744.IP "$object\->match_max($maximum_buffer_length | undef) \fIor\fR" 4
745.IX Item "$object->match_max($maximum_buffer_length | undef) or"
746.PD 0
747.IP "$object\->max_accum($maximum_buffer_length | undef)" 4
748.IX Item "$object->max_accum($maximum_buffer_length | undef)"
749.PD
750Set the maximum accumulator size for object. This is useful if you think
751that the accumulator will grow out of hand during \fIexpect()\fR calls. Since
752the buffer will be matched by every match_pattern it may get slow if the
753buffer gets too large. Returns current value if called without parameters.
754Not defined by default.
755.IP "$object\->notransfer(0 | 1)" 4
756.IX Item "$object->notransfer(0 | 1)"
757If set, matched strings will not be deleted from the accumulator.
758Returns current value if called without parameters. False by default.
759.IP "$object\->\fIexp_pid()\fR \fIor\fR" 4
760.IX Item "$object->exp_pid() or"
761.PD 0
762.IP "$object\->\fIpid()\fR" 4
763.IX Item "$object->pid()"
764.PD
765Return pid of \f(CW$object\fR, if one exists. Initialized filehandles will not have
766pids (of course).
767.ie n .IP "$object\->send_slow($delay, @strings);" 4
768.el .IP "$object\->send_slow($delay, \f(CW@strings\fR);" 4
769.IX Item "$object->send_slow($delay, @strings);"
770print each character from each string of \f(CW@strings\fR one at a time with \f(CW$delay\fR
771seconds before each character. This is handy for devices such as modems
772that can be annoying if you send them data too fast. After each character
773\&\f(CW$object\fR will be checked to determine whether or not it has any new data ready
774and if so update the accumulator for future \fIexpect()\fR calls and print the
775output to \s-1STDOUT\s0 and \f(CW@listen_group\fR if log_stdout and log_group are
776appropriately set.
777.Sh "Configurable Package Variables:"
778.IX Subsection "Configurable Package Variables:"
779.RE
780.IP "$Expect::Debug"
781.IX Item "$Expect::Debug"
782Defaults to 0. Newly created objects have a \f(CW$object\fR\->\fIdebug()\fR value
783of \f(CW$Expect::Debug\fR. See \f(CW$object\fR\->\fIdebug()\fR;
784.RE
785.IP "$Expect::Do_Soft_Close"
786.IX Item "$Expect::Do_Soft_Close"
787Defaults to 0. When destroying objects, soft_close may take up to half
788a minute to shut everything down. From now on, only hard_close will
789be called, which is less polite but still gives the process a chance
790to terminate properly. Set this to '1' for old behaviour.
791.RE
792.IP "$Expect::Exp_Internal"
793.IX Item "$Expect::Exp_Internal"
794Defaults to 0. Newly created objects have a \f(CW$object\fR\->\fIexp_internal()\fR
795value of \f(CW$Expect::Exp_Internal\fR. See \f(CW$object\fR\->\fIexp_internal()\fR.
796.RE
797.IP "$Expect::Log_Group"
798.IX Item "$Expect::Log_Group"
799Defaults to 1. Newly created objects have a \f(CW$object\fR\->\fIlog_group()\fR
800value of \f(CW$Expect::Log_Group\fR. See \f(CW$object\fR\->\fIlog_group()\fR.
801.RE
802.IP "$Expect::Log_Stdout"
803.IX Item "$Expect::Log_Stdout"
804Defaults to 1 for spawned commands, 0 for file handles
805attached with \fIexp_init()\fR. Newly created objects have a
806\&\f(CW$object\fR\->\fIlog_stdout()\fR value of \f(CW$Expect::Log_Stdout\fR. See
807\&\f(CW$object\fR\->\fIlog_stdout()\fR.
808.RE
809.IP "$Expect::Manual_Stty"
810.IX Item "$Expect::Manual_Stty"
811Defaults to 0. Newly created objects have a \f(CW$object\fR\->\fImanual_stty()\fR
812value of \f(CW$Expect::Manual_Stty\fR. See \f(CW$object\fR\->\fImanual_stty()\fR.
813.RE
814.IP "$Expect::Multiline_Matching"
815.IX Item "$Expect::Multiline_Matching"
816.Vb 6
817\& Defaults to 1. Affects whether or not expect() uses the /m flag for
818\&doing regular expression matching. If set to 1 /m is used.
819\& This makes a difference when you are trying to match ^ and $. If
820\&you have this on you can match lines in the middle of a page of output
821\&using ^ and $ instead of it matching the beginning and end of the entire
822\&expression. I think this is handy.
823.Ve
824.SH "CONTRIBUTIONS"
825.IX Header "CONTRIBUTIONS"
826Lee Eakin <leakin@japh.itg.ti.com> has ported the kibitz script
827from Tcl/Expect to Perl/Expect.
828.PP
829Jeff Carr <jcarr@linuxmachines.com> provided a simple example of how
830handle terminal window resize events (transmitted via the \s-1WINCH\s0
831signal) in a ssh session.
832.PP
833You can find both scripts in the examples/ subdir. Thanks to both!
834.PP
835Historical notes:
836.PP
837There are still a few lines of code dating back to the inspirational
838Comm.pl and Chat.pl modules without which this would not have been possible.
839Kudos to Eric Arnold <Eric.Arnold@Sun.com> and Randal 'Nuke your \s-1NT\s0 box with
840one line of perl code' Schwartz<merlyn@stonehenge.com> for making these
841available to the perl public.
842.PP
843As of .98 I think all the old code is toast. No way could this have been done
844without it though. Special thanks to Graham Barr for helping make sense of
845the IO::Handle stuff as well as providing the highly recommended IO::Tty
846module.
847.SH "REFERENCES"
848.IX Header "REFERENCES"
849Mark Rogaski <rogaski@att.com> wrote:
850.PP
851\&\*(L"I figured that you'd like to know that Expect.pm has been very
852useful to \s-1AT&T\s0 Labs over the past couple of years (since I first talked to
853Austin about design decisions). We use Expect.pm for managing
854the switches in our network via the telnet interface, and such automation
855has significantly increased our reliability. So, you can honestly say that
856one of the largest digital networks in existence (\s-1AT&T\s0 Frame Relay) uses
857Expect.pm quite extensively.\*(R"
858.SH "FAQ \- Frequently Asked Questions"
859.IX Header "FAQ - Frequently Asked Questions"
860This is a growing collection of things that might help.
861Please send you questions that are not answered here to
862RGiersig@cpan.org
863.Sh "What systems does Expect run on?"
864.IX Subsection "What systems does Expect run on?"
865Expect itself doesn't have real system dependencies, but the underlying
866IO::Tty needs pseudoterminals. IO::Stty uses \s-1POSIX\s0.pm and Fcntl.pm.
867.PP
868I have used it on Solaris, Linux and \s-1AIX\s0, others report *BSD and \s-1OSF\s0
869as working. Generally, any modern \s-1POSIX\s0 Unix should do, but there
870are exceptions to every rule. Feedback is appreciated.
871.PP
872See IO::Tty for a list of verified systems.
873.Sh "Can I use this module with ActivePerl on Windows?"
874.IX Subsection "Can I use this module with ActivePerl on Windows?"
875Up to now, the answer was 'No', but this has changed.
876.PP
877You still cannot use ActivePerl, but if you use the Cygwin environment
878(http://sources.redhat.com), which brings its own perl, and have
879the latest IO::Tty (v0.05 or later) installed, it should work (feedback
880appreciated).
881.Sh "The examples in the tutorial don't work!"
882.IX Subsection "The examples in the tutorial don't work!"
883The tutorial is hopelessly out of date and needs a serious overhaul.
884I appologize for this, I have concentrated my efforts mainly on the
885functionality. Volunteers welcomed.
886.Sh "How can I find out what Expect is doing?"
887.IX Subsection "How can I find out what Expect is doing?"
888If you set
889.PP
890.Vb 1
891\& $Expect::Exp_Internal = 1;
892.Ve
893.PP
894Expect will tell you very verbosely what it is receiving and sending,
895what matching it is trying and what it found. You can do this on a
896per-command base with
897.PP
898.Vb 1
899\& $exp->exp_internal(1);
900.Ve
901.PP
902You can also set
903.PP
904.Vb 1
905\& $Expect::Debug = 1; # or 2, 3 for more verbose output
906.Ve
907.PP
908or
909.PP
910.Vb 1
911\& $exp->debug(1);
912.Ve
913.PP
914which gives you even more output.
915.Sh "I am seeing the output of the command I spawned. Can I turn that off?"
916.IX Subsection "I am seeing the output of the command I spawned. Can I turn that off?"
917Yes, just set
918.PP
919.Vb 1
920\& $Expect::Log_Stdout = 0;
921.Ve
922.PP
923to globally disable it or
924.PP
925.Vb 1
926\& $exp->log_stdout(0);
927.Ve
928.PP
929for just that command. 'log_user' is provided as an alias so
930Tcl/Expect user get a \s-1DWIM\s0 experience... :\-)
931.Sh "No, I mean that when I send some text to the spawned process, it gets echoed back and I have to deal with it in the next expect."
932.IX Subsection "No, I mean that when I send some text to the spawned process, it gets echoed back and I have to deal with it in the next expect."
933This is caused by the pty, which has probably 'echo' enabled. A
934solution would be to set the pty to raw mode, which in general is
935cleaner for communication between two programs (no more unexpected
936character translations). Unfortunately this would break a lot of old
937code that sends \*(L"\er\*(R" to the program instead of \*(L"\en\*(R" (translating this
938is also handled by the pty), so I won't add this to Expect just like that.
939But feel free to experiment with \f(CW\*(C`$exp\->raw_pty(1)\*(C'\fR.
940.Sh "How do I send control characters to a process?"
941.IX Subsection "How do I send control characters to a process?"
942A: You can send any characters to a process with the print command. To
943represent a control character in Perl, use \ec followed by the letter. For
944example, control-G can be represented with \*(L"\ecG\*(R" . Note that this will not
945work if you single-quote your string. So, to send control-C to a process in
946\&\f(CW$exp\fR, do:
947.PP
948.Vb 1
949\& print $exp "\ecC";
950.Ve
951.PP
952Or, if you prefer:
953.PP
954.Vb 1
955\& $exp->send("\ecC");
956.Ve
957.PP
958The ability to include control characters in a string like this is provided
959by Perl, not by Expect.pm . Trying to learn Expect.pm without a thorough
960grounding in Perl can be very daunting. We suggest you look into some of
961the excellent Perl learning material, such as the books _Programming Perl_
962and _Learning Perl_ by O'Reilly, as well as the extensive online Perl
963documentation available through the perldoc command.
964.Sh "My script fails from time to time without any obvious reason. It seems that I am sometimes loosing output from the spawned program."
965.IX Subsection "My script fails from time to time without any obvious reason. It seems that I am sometimes loosing output from the spawned program."
966You could be exiting too fast without giving the spawned program
967enough time to finish. Try adding \f(CW$exp\fR\->\fIsoft_close()\fR to terminate the
968program gracefully or do an \fIexpect()\fR for 'eof'.
969.PP
970Alternatively, try adding a 'sleep 1' after you \fIspawn()\fR the program.
971It could be that pty creation on your system is just slow (but this is
972rather improbable if you are using the latest IO\-Tty).
973.Sh "I want to automate password entry for su/ssh/scp/rsh/..."
974.IX Subsection "I want to automate password entry for su/ssh/scp/rsh/..."
975You shouldn't use Expect for this. Putting passwords, especially
976root passwords, into scripts in clear text can mean severe security
977problems. I strongly recommend using other means. For 'su', consider
978switching to 'sudo', which gives you root access on a per-command and
979per-user basis without the need to enter passwords. 'ssh'/'scp' can be
980set up with \s-1RSA\s0 authentication without passwords. 'rsh' can use
981the .rhost mechanism, but I'd strongly suggest to switch to 'ssh'; to
982mention 'rsh' and 'security' in the same sentence makes an oxymoron.
983.PP
984It will work for 'telnet', though, and there are valid uses for it,
985but you still might want to consider using 'ssh', as keeping cleartext
986passwords around is very insecure.
987.Sh "I want to use Expect to automate [anything with a buzzword]..."
988.IX Subsection "I want to use Expect to automate [anything with a buzzword]..."
989Are you sure there is no other, easier way? As a rule of thumb,
990Expect is useful for automating things that expect to talk to a human,
991where no formal standard applies. For other tasks that do follow a
992well-defined protocol, there are often better-suited modules that
993already can handle those protocols. Don't try to do \s-1HTTP\s0 requests by
994spawning telnet to port 80, use \s-1LWP\s0 instead. To automate \s-1FTP\s0, take a
995look at Net::FTP or \f(CW\*(C`ncftp\*(C'\fR (http://www.ncftp.org). You don't use
996a screwdriver to hammer in your nails either, or do you?
997.Sh "I want to log the whole session to a file."
998.IX Subsection "I want to log the whole session to a file."
999Use
1000.PP
1001.Vb 1
1002\& $exp->log_file("filename");
1003.Ve
1004.PP
1005or
1006.PP
1007.Vb 1
1008\& $exp->log_file($filehandle);
1009.Ve
1010.PP
1011or even
1012.PP
1013.Vb 1
1014\& $exp->log_file(\e&log_procedure);
1015.Ve
1016.PP
1017for maximum flexibility.
1018.PP
1019Note that the logfile is appended to by default, but you can
1020specify an optional mode \*(L"w\*(R" to truncate the logfile:
1021.PP
1022.Vb 1
1023\& $exp->log_file("filename", "w");
1024.Ve
1025.PP
1026To stop logging, just call it with a false argument:
1027.PP
1028.Vb 1
1029\& $exp->log_file(undef);
1030.Ve
1031.Sh "How can I turn off multi-line matching for my regexps?"
1032.IX Subsection "How can I turn off multi-line matching for my regexps?"
1033To globally unset multi-line matching for all regexps:
1034.PP
1035.Vb 1
1036\& $Expect::Multiline_Matching = 0;
1037.Ve
1038.PP
1039You can do that on a per-regexp basis by stating \f(CW\*(C`(?\-m)\*(C'\fR inside the regexp
1040(you need perl5.00503 or later for that).
1041.Sh "How can I expect on multiple spawned commands?"
1042.IX Subsection "How can I expect on multiple spawned commands?"
1043You can use the \fB\-i\fR parameter to specify a single object or a list
1044of Expect objects. All following patterns will be evaluated against
1045that list.
1046.PP
1047You can specify \fB\-i\fR multiple times to create groups of objects
1048and patterns to match against within the same expect statement.
1049.PP
1050This works just like in Tcl/Expect.
1051.PP
1052See the source example below.
1053.Sh "I seem to have problems with ptys!"
1054.IX Subsection "I seem to have problems with ptys!"
1055Well, pty handling is really a black magic, as it is extremely system
1056dependend. I have extensively revised IO\-Tty, so these problems
1057should be gone.
1058.PP
1059If your system is listed in the \*(L"verified\*(R" list of IO::Tty, you
1060probably have some non-standard setup, e.g. you compiled your
1061Linux-kernel yourself and disabled ptys. Please ask your friendly
1062sysadmin for help.
1063.PP
1064If your system is not listed, unpack the latest version of IO::Tty,
1065do a 'perl Makefile.PL; make; make test; uname \f(CW\*(C`\-a\*(C'\fR' and send me the
1066results and I'll see what I can deduce from that.
1067.Sh "I just want to read the output of a process without \fIexpect()\fPing anything. How can I do this?"
1068.IX Subsection "I just want to read the output of a process without expect()ing anything. How can I do this?"
1069[ Are you sure you need Expect for this? How about \fIqx()\fR or open(\*(L"prog|\*(R")? ]
1070.PP
1071By using expect without any patterns to match.
1072.PP
1073.Vb 3
1074\& $process->expect(undef); # Forever until EOF
1075\& $process->expect($timeout); # For a few seconds
1076\& $process->expect(0); # Is there anything ready on the handle now?
1077.Ve
1078.Sh "Ok, so now how do I get what was read on the handle?"
1079.IX Subsection "Ok, so now how do I get what was read on the handle?"
1080.Vb 1
1081\& $read = $process->before();
1082.Ve
1083.Sh "Where's IO::Pty?"
1084.IX Subsection "Where's IO::Pty?"
1085Find it on \s-1CPAN\s0 as IO\-Tty, which provides both.
1086.Sh "How come when I automate the passwd program to change passwords for me passwd dies before changing the password sometimes/every time?"
1087.IX Subsection "How come when I automate the passwd program to change passwords for me passwd dies before changing the password sometimes/every time?"
1088What's happening is you are closing the handle before passwd exits.
1089When you close the handle to a process, it is sent a signal (\s-1SIGPIPE\s0?)
1090telling it that \s-1STDOUT\s0 has gone away. The default behavior for
1091processes is to die in this circumstance. Two ways you can make this
1092not happen are:
1093.PP
1094.Vb 1
1095\& $process->soft_close();
1096.Ve
1097.PP
1098This will wait 15 seconds for a process to come up with an \s-1EOF\s0 by
1099itself before killing it.
1100.PP
1101.Vb 1
1102\& $process->expect(undef);
1103.Ve
1104.PP
1105This will wait forever for the process to match an empty set of
1106patterns. It will return when the process hits an \s-1EOF\s0.
1107.PP
1108As a rule, you should always \fIexpect()\fR the result of your transaction
1109before you continue with processing.
1110.Sh "How come when I try to make a logfile with \fIlog_file()\fP or \fIset_group()\fP it doesn't print anything after the last time I run \fIexpect()\fP?"
1111.IX Subsection "How come when I try to make a logfile with log_file() or set_group() it doesn't print anything after the last time I run expect()?"
1112Output is only printed to the logfile/group when Expect reads from the
1113process, during \fIexpect()\fR, \fIsend_slow()\fR and \fIinterconnect()\fR.
1114One way you can force this is to make use of
1115.PP
1116.Vb 1
1117\& $process->expect(undef);
1118.Ve
1119.PP
1120and
1121.PP
1122.Vb 1
1123\& $process->expect(0);
1124.Ve
1125.PP
1126which will make \fIexpect()\fR run with an empty pattern set forever or just
1127for an instant to capture the output of \f(CW$process\fR. The output is
1128available in the accumulator, so you can grab it using
1129\&\f(CW$process\fR\->\fIbefore()\fR.
1130.Sh "I seem to have problems with terminal settings, double echoing, etc."
1131.IX Subsection "I seem to have problems with terminal settings, double echoing, etc."
1132Tty settings are a major pain to keep track of. If you find unexpected
1133behavior such as double-echoing or a frozen session, doublecheck the
1134documentation for default settings. When in doubt, handle them
1135yourself using \f(CW$exp\fR\->\fIstty()\fR and \fImanual_stty()\fR functions. As of .98
1136you shouldn't have to worry about stty settings getting fouled unless
1137you use interconnect or intentionally change them (like doing \-echo to
1138get a password).
1139.PP
1140If you foul up your terminal's tty settings, kill any hung processes
1141and enter 'stty sane' at a shell prompt. This should make your
1142terminal manageable again.
1143.PP
1144Note that IO::Tty returns ptys with your systems default setting
1145regarding echoing, \s-1CRLF\s0 translation etc. and Expect does not change
1146them. I have considered setting the ptys to 'raw' without any
1147translation whatsoever, but this would break a lot of existing things,
1148as '\er' translation would not work anymore. On the other hand, a raw
1149pty works much like a pipe and is more \s-1WYGIWYE\s0 (what you get is what
1150you expect), so I suggest you set it to 'raw' by yourself:
1151.PP
1152.Vb 3
1153\& $exp = new Expect;
1154\& $exp->raw_pty(1);
1155\& $exp->spawn(...);
1156.Ve
1157.PP
1158To disable echo:
1159.PP
1160.Vb 1
1161\& $exp->slave->stty(qw(-echo));
1162.Ve
1163.Sh "I'm spawning a telnet/ssh session and then let the user interact with it. But screen-oriented applications on the other side don't work properly."
1164.IX Subsection "I'm spawning a telnet/ssh session and then let the user interact with it. But screen-oriented applications on the other side don't work properly."
1165You have to set the terminal screen size for that. Luckily, IO::Pty
1166already has a method for that, so modify your code to look like this:
1167.PP
1168.Vb 3
1169\& my $exp = new Expect;
1170\& $exp->slave->clone_winsize_from(\e*STDIN);
1171\& $exp->spawn("telnet somehost);
1172.Ve
1173.PP
1174Also, some applications need the \s-1TERM\s0 shell variable set so they know
1175how to move the cursor across the screen. When logging in, the remote
1176shell sends a query (Ctrl\-Z I think) and expects the terminal to
1177answer with a string, e.g. 'xterm'. If you really want to go that way
1178(be aware, madness lies at its end), you can handle that and send back
1179the value in \f(CW$ENV\fR{\s-1TERM\s0}. This is only a hand-waving explanation,
1180please figure out the details by yourself.
1181.Sh "I set the terminal size as explained above, but if I resize the window, the application does not notice this."
1182.IX Subsection "I set the terminal size as explained above, but if I resize the window, the application does not notice this."
1183You have to catch the signal \s-1WINCH\s0 (\*(L"window size changed\*(R"), change the
1184terminal size and propagate the signal to the spawned application:
1185.PP
1186.Vb 4
1187\& my $exp = new Expect;
1188\& $exp->slave->clone_winsize_from(\e*STDIN);
1189\& $exp->spawn("ssh somehost);
1190\& $SIG{WINCH} = \e&winch;
1191.Ve
1192.PP
1193.Vb 5
1194\& sub winch {
1195\& $exp->slave->clone_winsize_from(\e*STDIN);
1196\& kill WINCH => $exp->pid if $exp->pid;
1197\& $SIG{WINCH} = \e&winch;
1198\& }
1199.Ve
1200.PP
1201.Vb 1
1202\& $exp->interact();
1203.Ve
1204.PP
1205There is an example file ssh.pl in the examples/ subdir that shows how
1206this works with ssh. Please note that I do strongly object against
1207using Expect to automate ssh login, as there are better way to do that
1208(see ssh-keygen).
1209.Sh "I noticed that the test uses a string that resembles, but not exactly matches, a well-known sentence that contains every character. What does that mean?"
1210.IX Subsection "I noticed that the test uses a string that resembles, but not exactly matches, a well-known sentence that contains every character. What does that mean?"
1211That means you are anal\-retentive. :\-) [Gotcha there!]
1212.ie n .Sh "I get a ""Could not assign a pty"" error when running as a non-root user on an \s-1IRIX\s0 box?"
1213.el .Sh "I get a ``Could not assign a pty'' error when running as a non-root user on an \s-1IRIX\s0 box?"
1214.IX Subsection "I get a Could not assign a pty error when running as a non-root user on an IRIX box?"
1215The \s-1OS\s0 may not be configured to grant additional pty's (pseudo terminals)
1216to non-root users. /usr/sbin/mkpts should be 4755, not 700 for this
1217to work. I don't know about security implications if you do this.
1218.Sh "How come I don't notice when the spawned process closes its stdin/out/err??"
1219.IX Subsection "How come I don't notice when the spawned process closes its stdin/out/err??"
1220You are probably on one of the systems where the master doesn't get an
1221\&\s-1EOF\s0 when the slave closes stdin/out/err.
1222.PP
1223One possible solution is when you spawn a process, follow it with a
1224unique string that would indicate the process is finished.
1225.PP
1226.Vb 1
1227\& $process = Expect->spawn('telnet somehost; echo ____END____');
1228.Ve
1229.PP
1230And then \f(CW$process\fR\->expect($timeout,'_\|__\|_END_\|__\|_','other','patterns');
1231.SH "Source Examples"
1232.IX Header "Source Examples"
1233.Sh "How to automate login"
1234.IX Subsection "How to automate login"
1235.Vb 3
1236\& my $telnet = new Net::Telnet ("remotehost") # see Net::Telnet
1237\& or die "Cannot telnet to remotehost: $!\en";;
1238\& my $exp = Expect->exp_init($telnet);
1239.Ve
1240.PP
1241.Vb 3
1242\& # deprecated use of spawned telnet command
1243\& # my $exp = Expect->spawn("telnet localhost")
1244\& # or die "Cannot spawn telnet: $!\en";;
1245.Ve
1246.PP
1247.Vb 37
1248\& my $spawn_ok;
1249\& $exp->expect($timeout,
1250\& [
1251\& qr'login: $',
1252\& sub {
1253\& $spawn_ok = 1;
1254\& my $fh = shift;
1255\& $fh->send("$username\en");
1256\& exp_continue;
1257\& }
1258\& ],
1259\& [
1260\& 'Password: $',
1261\& sub {
1262\& my $fh = shift;
1263\& print $fh "$password\en";
1264\& exp_continue;
1265\& }
1266\& ],
1267\& [
1268\& eof =>
1269\& sub {
1270\& if ($spawn_ok) {
1271\& die "ERROR: premature EOF in login.\en";
1272\& } else {
1273\& die "ERROR: could not spawn telnet.\en";
1274\& }
1275\& }
1276\& ],
1277\& [
1278\& timeout =>
1279\& sub {
1280\& die "No login.\en";
1281\& }
1282\& ],
1283\& '-re', qr'[#>:] $', #' wait for shell prompt, then exit expect
1284\& );
1285.Ve
1286.Sh "How to expect on multiple spawned commands"
1287.IX Subsection "How to expect on multiple spawned commands"
1288.Vb 3
1289\& foreach my $cmd (@list_of_commands) {
1290\& push @commands, Expect->spawn($cmd);
1291\& }
1292.Ve
1293.PP
1294.Vb 16
1295\& expect($timeout,
1296\& '-i', \e@commands,
1297\& [
1298\& qr"pattern", # find this pattern in output of all commands
1299\& sub {
1300\& my $obj = shift; # object that matched
1301\& print $obj "something\en";
1302\& exp_continue; # we don't want to terminate the expect call
1303\& }
1304\& ],
1305\& '-i', $some_other_command,
1306\& [
1307\& "some other pattern",
1308\& sub {
1309\& my ($obj, $parmref) = @_;
1310\& # ...
1311.Ve
1312.PP
1313.Vb 5
1314\& # now we exit the expect command
1315\& },
1316\& \e$parm
1317\& ],
1318\& );
1319.Ve
1320.Sh "How to propagate terminal sizes"
1321.IX Subsection "How to propagate terminal sizes"
1322.Vb 4
1323\& my $exp = new Expect;
1324\& $exp->slave->clone_winsize_from(\e*STDIN);
1325\& $exp->spawn("ssh somehost);
1326\& $SIG{WINCH} = \e&winch;
1327.Ve
1328.PP
1329.Vb 5
1330\& sub winch {
1331\& $exp->slave->clone_winsize_from(\e*STDIN);
1332\& kill WINCH => $exp->pid if $exp->pid;
1333\& $SIG{WINCH} = \e&winch;
1334\& }
1335.Ve
1336.PP
1337.Vb 1
1338\& $exp->interact();
1339.Ve
1340.SH "HOMEPAGE"
1341.IX Header "HOMEPAGE"
1342http://sourceforge.net/projects/expectperl/
1343.SH "MAILING LISTS"
1344.IX Header "MAILING LISTS"
1345There are two mailing lists available, expectperl-announce and
1346expectperl\-discuss, at
1347.PP
1348.Vb 1
1349\& http://lists.sourceforge.net/lists/listinfo/expectperl-announce
1350.Ve
1351.PP
1352and
1353.PP
1354.Vb 1
1355\& http://lists.sourceforge.net/lists/listinfo/expectperl-discuss
1356.Ve
1357.SH "BUG TRACKING"
1358.IX Header "BUG TRACKING"
1359You can use the \s-1CPAN\s0 Request Tracker http://rt.cpan.org/ and submit
1360new bugs under
1361.PP
1362.Vb 1
1363\& http://rt.cpan.org/Ticket/Create.html?Queue=Expect
1364.Ve
1365.SH "AUTHORS"
1366.IX Header "AUTHORS"
1367(c) 1997 Austin Schutz <\fIASchutz@users.sourceforge.net\fR> (retired)
1368.PP
1369\&\fIexpect()\fR interface & functionality enhancements (c) 1999\-2006 Roland Giersig.
1370.PP
1371This module is now maintained by Roland Giersig <\fIRGiersig@cpan.org\fR>
1372.SH "LICENSE"
1373.IX Header "LICENSE"
1374This module can be used under the same terms as Perl.
1375.SH "DISCLAIMER"
1376.IX Header "DISCLAIMER"
1377\&\s-1THIS\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1PROVIDED\s0 ``\s-1AS\s0 \s-1IS\s0'' \s-1AND\s0 \s-1ANY\s0 \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0
1378\&\s-1WARRANTIES\s0, \s-1INCLUDING\s0, \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0, \s-1THE\s0 \s-1IMPLIED\s0 \s-1WARRANTIES\s0 \s-1OF\s0
1379\&\s-1MERCHANTABILITY\s0 \s-1AND\s0 \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0 \s-1ARE\s0 \s-1DISCLAIMED\s0.
1380\&\s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1SHALL\s0 \s-1THE\s0 \s-1AUTHORS\s0 \s-1BE\s0 \s-1LIABLE\s0 \s-1FOR\s0 \s-1ANY\s0 \s-1DIRECT\s0, \s-1INDIRECT\s0,
1381\&\s-1INCIDENTAL\s0, \s-1SPECIAL\s0, \s-1EXEMPLARY\s0, \s-1OR\s0 \s-1CONSEQUENTIAL\s0 \s-1DAMAGES\s0 (\s-1INCLUDING\s0,
1382\&\s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0, \s-1PROCUREMENT\s0 \s-1OF\s0 \s-1SUBSTITUTE\s0 \s-1GOODS\s0 \s-1OR\s0 \s-1SERVICES\s0; \s-1LOSS\s0
1383\&\s-1OF\s0 \s-1USE\s0, \s-1DATA\s0, \s-1OR\s0 \s-1PROFITS\s0; \s-1OR\s0 \s-1BUSINESS\s0 \s-1INTERRUPTION\s0) \s-1HOWEVER\s0 \s-1CAUSED\s0 \s-1AND\s0
1384\&\s-1ON\s0 \s-1ANY\s0 \s-1THEORY\s0 \s-1OF\s0 \s-1LIABILITY\s0, \s-1WHETHER\s0 \s-1IN\s0 \s-1CONTRACT\s0, \s-1STRICT\s0 \s-1LIABILITY\s0, \s-1OR\s0
1385\&\s-1TORT\s0 (\s-1INCLUDING\s0 \s-1NEGLIGENCE\s0 \s-1OR\s0 \s-1OTHERWISE\s0) \s-1ARISING\s0 \s-1IN\s0 \s-1ANY\s0 \s-1WAY\s0 \s-1OUT\s0 \s-1OF\s0 \s-1THE\s0
1386\&\s-1USE\s0 \s-1OF\s0 \s-1THIS\s0 \s-1SOFTWARE\s0, \s-1EVEN\s0 \s-1IF\s0 \s-1ADVISED\s0 \s-1OF\s0 \s-1THE\s0 \s-1POSSIBILITY\s0 \s-1OF\s0 \s-1SUCH\s0
1387\&\s-1DAMAGE\s0.
1388.PP
1389In other words: Use at your own risk. Provided as is. Your mileage
1390may vary. Read the source, Luke!
1391.PP
1392And finally, just to be sure:
1393.PP
1394Any Use of This Product, in Any Manner Whatsoever, Will Increase the
1395Amount of Disorder in the Universe. Although No Liability Is Implied
1396Herein, the Consumer Is Warned That This Process Will Ultimately Lead
1397to the Heat Death of the Universe.