1Net::Telnet(3) User Contributed Perl Documentation Net::Telnet(3)
2
3
4
6 Net::Telnet - interact with TELNET port or other TCP ports
7
9 "use Net::Telnet ();"
10
11 see METHODS section below
12
14 Net::Telnet allows you to make client connections to a TCP port and do
15 network I/O, especially to a port using the TELNET protocol. Simple
16 I/O methods such as print, get, and getline are provided. More sophis‐
17 ticated interactive features are provided because connecting to a TEL‐
18 NET port ultimately means communicating with a program designed for
19 human interaction. These interactive features include the ability to
20 specify a time-out and to wait for patterns to appear in the input
21 stream, such as the prompt from a shell.
22
23 Other reasons to use this module than strictly with a TELNET port are:
24
25 · You're not familiar with sockets and you want a simple way to make
26 client connections to TCP services.
27
28 · You want to be able to specify your own time-out while connecting,
29 reading, or writing.
30
31 · You're communicating with an interactive program at the other end of
32 some socket or pipe and you want to wait for certain patterns to
33 appear.
34
35 Here's an example that prints who's logged-on to the remote host
36 sparky. In addition to a username and password, you must also know the
37 user's shell prompt, which for this example is "bash$"
38
39 use Net::Telnet ();
40 $t = new Net::Telnet (Timeout => 10,
41 Prompt => '/bash\$ $/');
42 $t->open("sparky");
43 $t->login($username, $passwd);
44 @lines = $t->cmd("who");
45 print @lines;
46
47 More examples are in the EXAMPLES section below.
48
49 Usage questions should be directed to the Usenet newsgroup
50 comp.lang.perl.modules.
51
52 Contact me, Jay Rogers <jay@rgrs.com>, if you find any bugs or have
53 suggestions for improvement.
54
55 What To Know Before Using
56
57 · All output is flushed while all input is buffered. Each object con‐
58 tains its own input buffer.
59
60 · The output record separator for "print()" and "cmd()" is set to "\n"
61 by default, so that you don't have to append all your commands with a
62 newline. To avoid printing a trailing "\n" use "put()" or set the
63 output_record_separator to "".
64
65 · The methods "login()" and "cmd()" use the prompt setting in the
66 object to determine when a login or remote command is complete.
67 Those methods will fail with a time-out if you don't set the prompt
68 correctly.
69
70 · Use a combination of "print()" and "waitfor()" as an alternative to
71 "login()" or "cmd()" when they don't do what you want.
72
73 · Errors such as timing-out are handled according to the error mode
74 action. The default action is to print an error message to standard
75 error and have the program die. See the "errmode()" method for more
76 information.
77
78 · When constructing the match operator argument for "prompt()" or
79 "waitfor()", always use single quotes instead of double quotes to
80 avoid unexpected backslash interpretation (e.g. '/bash\$ $/'). If
81 you're constructing a DOS like file path, you'll need to use four
82 backslashes to represent one (e.g. '/c:\\\\users\\\\bill>$/i').
83
84 Of course don't forget about regexp metacharacters like ".", "[", or
85 "$". You'll only need a single backslash to quote them. The anchor
86 metacharacters "^" and "$" refer to positions in the input buffer.
87 To avoid matching characters read that look like a prompt, it's a
88 good idea to end your prompt pattern with the "$" anchor. That way
89 the prompt will only match if it's the last thing read.
90
91 · In the input stream, each sequence of carriage return and line feed
92 (i.e. "\015\012" or CR LF) is converted to "\n". In the output
93 stream, each occurrence of "\n" is converted to a sequence of CR LF.
94 See "binmode()" to change the behavior. TCP protocols typically use
95 the ASCII sequence, carriage return and line feed to designate a new‐
96 line.
97
98 · Timing-out while making a connection is disabled for machines that
99 don't support the "alarm()" function. Most notably these include MS-
100 Windows machines.
101
102 · You'll need to be running at least Perl version 5.002 to use this
103 module. This module does not require any libraries that don't
104 already come with a standard Perl distribution.
105
106 If you have the IO:: libraries installed (they come standard with
107 perl5.004 and later) then IO::Socket::INET is used as a base class,
108 otherwise FileHandle is used.
109
110 · Contact me, Jay Rogers <jay@rgrs.com>, if you find any bugs or have
111 suggestions for improvement.
112
113 Debugging
114
115 The typical usage bug causes a time-out error because you've made
116 incorrect assumptions about what the remote side actually sends. The
117 easiest way to reconcile what the remote side sends with your expecta‐
118 tions is to use "input_log()" or "dump_log()".
119
120 "dump_log()" allows you to see the data being sent from the remote side
121 before any translation is done, while "input_log()" shows you the
122 results after translation. The translation includes converting end of
123 line characters, removing and responding to TELNET protocol commands in
124 the data stream.
125
126 Style of Named Parameters
127
128 Two different styles of named parameters are supported. This document
129 only shows the IO:: style:
130
131 Net::Telnet->new(Timeout => 20);
132
133 however the dash-option style is also allowed:
134
135 Net::Telnet->new(-timeout => 20);
136
137 Connecting to a Remote MS-Windows Machine
138
139 By default MS-Windows doesn't come with a TELNET server. However third
140 party TELNET servers are available. Unfortunately many of these
141 servers falsely claim to be a TELNET server. This is especially true
142 of the so-called "Microsoft Telnet Server" that comes installed with
143 some newer versions MS-Windows.
144
145 When a TELNET server first accepts a connection, it must use the ASCII
146 control characters carriage-return and line-feed to start a new line
147 (see RFC854). A server like the "Microsoft Telnet Server" that doesn't
148 do this, isn't a TELNET server. These servers send ANSI terminal
149 escape sequences to position to a column on a subsequent line and to
150 even position while writing characters that are adjacent to each other.
151 Worse, when sending output these servers resend previously sent command
152 output in a misguided attempt to display an entire terminal screen.
153
154 Connecting Net::Telnet to one of these false TELNET servers makes your
155 job of parsing command output very difficult. It's better to replace a
156 false TELNET server with a real TELNET server. The better TELNET
157 servers for MS-Windows allow you to avoid the ANSI escapes by turning
158 off something some of them call console mode.
159
161 In the calling sequences below, square brackets [] represent optional
162 parameters.
163
164 new - create a new Net::Telnet object
165 $obj = new Net::Telnet ([$host]);
166
167 $obj = new Net::Telnet ([Binmode => $mode,]
168 [Cmd_remove_mode => $mode,]
169 [Dump_Log => $filename,]
170 [Errmode => $errmode,]
171 [Fhopen => $filehandle,]
172 [Host => $host,]
173 [Input_log => $file,]
174 [Input_record_separator => $chars,]
175 [Option_log => $file,]
176 [Ors => $chars,]
177 [Output_log => $file,]
178 [Output_record_separator => $chars,]
179 [Port => $port,]
180 [Prompt => $matchop,]
181 [Rs => $chars,]
182 [Telnetmode => $mode,]
183 [Timeout => $secs,]);
184
185 This is the constructor for Net::Telnet objects. A new object is
186 returned on success, the error mode action is performed on failure
187 - see "errmode()". The optional arguments are short-cuts to meth‐
188 ods of the same name.
189
190 If the $host argument is given then the object is opened by con‐
191 necting to TCP $port on $host. Also see "open()". The new object
192 returned is given the following defaults in the absence of corre‐
193 sponding named parameters:
194
195 * The default Host is "localhost"
196
197 * The default Port is 23
198
199 * The default Prompt is '/[\$%#>] $/'
200
201 * The default Timeout is 10
202
203 * The default Errmode is "die"
204
205 * The default Output_record_separator is "\n". Note that Ors is
206 synonymous with Output_record_separator.
207
208 * The default Input_record_separator is "\n". Note that Rs is
209 synonymous with Input_record_separator.
210
211 * The default Binmode is 0, which means do newline translation.
212
213 * The default Telnetmode is 1, which means respond to TELNET com‐
214 mands in the data stream.
215
216 * The default Cmd_remove_mode is "auto"
217
218 * The defaults for Dump_log, Input_log, Option_log, and Out‐
219 put_log are "", which means that logging is turned-off.
220
221 binmode - toggle newline translation
222 $mode = $obj->binmode;
223
224 $prev = $obj->binmode($mode);
225
226 This method controls whether or not sequences of carriage returns
227 and line feeds (CR LF or more specifically "\015\012") are trans‐
228 lated. By default they are translated (i.e. binmode is 0).
229
230 If no argument is given, the current mode is returned.
231
232 If $mode is 1 then binmode is on and newline translation is not
233 done.
234
235 If $mode is 0 then binmode is off and newline translation is done.
236 In the input stream, each sequence of CR LF is converted to "\n"
237 and in the output stream, each occurrence of "\n" is converted to a
238 sequence of CR LF.
239
240 Note that input is always buffered. Changing binmode doesn't
241 effect what's already been read into the buffer. Output is not
242 buffered and changing binmode will have an immediate effect.
243
244 break - send TELNET break character
245 $ok = $obj->break;
246
247 This method sends the TELNET break character. This character is
248 provided because it's a signal outside the ASCII character set
249 which is currently given local meaning within many systems. It's
250 intended to indicate that the Break Key or the Attention Key was
251 hit.
252
253 This method returns 1 on success, or performs the error mode action
254 on failure.
255
256 buffer - scalar reference to object's input buffer
257 $ref = $obj->buffer;
258
259 This method returns a scalar reference to the input buffer for
260 $obj. Data in the input buffer is data that has been read from the
261 remote side but has yet to be read by the user. Modifications to
262 the input buffer are returned by a subsequent read.
263
264 buffer_empty - discard all data in object's input buffer
265 $obj->buffer_empty;
266
267 This method removes all data in the input buffer for $obj.
268
269 close - close object
270 $ok = $obj->close;
271
272 This method closes the socket, file, or pipe associated with the
273 object. It always returns a value of 1.
274
275 cmd - issue command and retrieve output
276 $ok = $obj->cmd($string);
277 $ok = $obj->cmd(String => $string,
278 [Output => $ref,]
279 [Cmd_remove_mode => $mode,]
280 [Errmode => $mode,]
281 [Input_record_separator => $chars,]
282 [Ors => $chars,]
283 [Output_record_separator => $chars,]
284 [Prompt => $match,]
285 [Rs => $chars,]
286 [Timeout => $secs,]);
287
288 @output = $obj->cmd($string);
289 @output = $obj->cmd(String => $string,
290 [Output => $ref,]
291 [Cmd_remove_mode => $mode,]
292 [Errmode => $mode,]
293 [Input_record_separator => $chars,]
294 [Ors => $chars,]
295 [Output_record_separator => $chars,]
296 [Prompt => $match,]
297 [Rs => $chars,]
298 [Timeout => $secs,]);
299
300 This method sends the command $string, and reads the characters
301 sent back by the command up until and including the matching
302 prompt. It's assumed that the program to which you're sending is
303 some kind of command prompting interpreter such as a shell.
304
305 The command $string is automatically appended with the out‐
306 put_record_separator, By default that's "\n". This is similar to
307 someone typing a command and hitting the return key. Set the out‐
308 put_record_separator to change this behavior.
309
310 In a scalar context, the characters read from the remote side are
311 discarded and 1 is returned on success. On time-out, eof, or other
312 failures, the error mode action is performed. See "errmode()".
313
314 In a list context, just the output generated by the command is
315 returned, one line per element. In other words, all the characters
316 in between the echoed back command string and the prompt are
317 returned. If the command happens to return no output, a list con‐
318 taining one element, the empty string is returned. This is so the
319 list will indicate true in a boolean context. On time-out, eof, or
320 other failures, the error mode action is performed. See
321 "errmode()".
322
323 The characters that matched the prompt may be retrieved using
324 "last_prompt()".
325
326 Many command interpreters echo back the command sent. In most sit‐
327 uations, this method removes the first line returned from the
328 remote side (i.e. the echoed back command). See
329 "cmd_remove_mode()" for more control over this feature.
330
331 Use "dump_log()" to debug when this method keeps timing-out and you
332 don't think it should.
333
334 Consider using a combination of "print()" and "waitfor()" as an
335 alternative to this method when it doesn't do what you want, e.g.
336 the command you send prompts for input.
337
338 The Output named parameter provides an alternative method of
339 receiving command output. If you pass a scalar reference, all the
340 output (even if it contains multiple lines) is returned in the ref‐
341 erenced scalar. If you pass an array or hash reference, the lines
342 of output are returned in the referenced array or hash. You can
343 use "input_record_separator()" to change the notion of what sepa‐
344 rates a line.
345
346 Optional named parameters are provided to override the current set‐
347 tings of cmd_remove_mode, errmode, input_record_separator, ors,
348 output_record_separator, prompt, rs, and timeout. Rs is synonymous
349 with input_record_separator and ors is synonymous with out‐
350 put_record_separator.
351
352 cmd_remove_mode - toggle removal of echoed commands
353 $mode = $obj->cmd_remove_mode;
354
355 $prev = $obj->cmd_remove_mode($mode);
356
357 This method controls how to deal with echoed back commands in the
358 output returned by cmd(). Typically, when you send a command to
359 the remote side, the first line of output returned is the command
360 echoed back. Use this mode to remove the first line of output nor‐
361 mally returned by cmd().
362
363 If no argument is given, the current mode is returned.
364
365 If $mode is 0 then the command output returned from cmd() has no
366 lines removed. If $mode is a positive integer, then the first
367 $mode lines of command output are stripped.
368
369 By default, $mode is set to "auto". Auto means that whether or not
370 the first line of command output is stripped, depends on whether or
371 not the remote side offered to echo. By default, Net::Telnet
372 always accepts an offer to echo by the remote side. You can change
373 the default to reject such an offer using "option_accept()".
374
375 A warning is printed to STDERR when attempting to set this
376 attribute to something that's not "auto" or a non-negative integer.
377
378 dump_log - log all I/O in dump format
379 $fh = $obj->dump_log;
380
381 $fh = $obj->dump_log($fh);
382
383 $fh = $obj->dump_log($filename);
384
385 This method starts or stops dump format logging of all the object's
386 input and output. The dump format shows the blocks read and writ‐
387 ten in a hexadecimal and printable character format. This method
388 is useful when debugging, however you might want to first try
389 "input_log()" as it's more readable.
390
391 If no argument is given, the current log filehandle is returned.
392 An empty string indicates logging is off.
393
394 To stop logging, use an empty string as an argument.
395
396 If an open filehandle is given, it is used for logging and
397 returned. Otherwise, the argument is assumed to be the name of a
398 file, the file is opened and a filehandle to it is returned. If
399 the file can't be opened for writing, the error mode action is per‐
400 formed.
401
402 eof - end of file indicator
403 $eof = $obj->eof;
404
405 This method returns 1 if end of file has been read, otherwise it
406 returns an empty string. Because the input is buffered this isn't
407 the same thing as $obj has closed. In other words $obj can be
408 closed but there still can be stuff in the buffer to be read.
409 Under this condition you can still read but you won't be able to
410 write.
411
412 errmode - define action to be performed on error
413 $mode = $obj->errmode;
414
415 $prev = $obj->errmode($mode);
416
417 This method gets or sets the action used when errors are encoun‐
418 tered using the object. The first calling sequence returns the
419 current error mode. The second calling sequence sets it to $mode
420 and returns the previous mode. Valid values for $mode are "die"
421 (the default), "return", a coderef, or an arrayref.
422
423 When mode is "die" and an error is encountered using the object,
424 then an error message is printed to standard error and the program
425 dies.
426
427 When mode is "return" then the method generating the error places
428 an error message in the object and returns an undefined value in a
429 scalar context and an empty list in list context. The error mes‐
430 sage may be obtained using "errmsg()".
431
432 When mode is a coderef, then when an error is encountered coderef
433 is called with the error message as its first argument. Using this
434 mode you may have your own subroutine handle errors. If coderef
435 itself returns then the method generating the error returns unde‐
436 fined or an empty list depending on context.
437
438 When mode is an arrayref, the first element of the array must be a
439 coderef. Any elements that follow are the arguments to coderef.
440 When an error is encountered, the coderef is called with its argu‐
441 ments. Using this mode you may have your own subroutine handle
442 errors. If the coderef itself returns then the method generating
443 the error returns undefined or an empty list depending on context.
444
445 A warning is printed to STDERR when attempting to set this
446 attribute to something that's not "die", "return", a coderef, or an
447 arrayref whose first element isn't a coderef.
448
449 errmsg - most recent error message
450 $msg = $obj->errmsg;
451
452 $prev = $obj->errmsg(@msgs);
453
454 The first calling sequence returns the error message associated
455 with the object. The empty string is returned if no error has been
456 encountered yet. The second calling sequence sets the error mes‐
457 sage for the object to the concatenation of @msgs and returns the
458 previous error message. Normally, error messages are set inter‐
459 nally by a method when an error is encountered.
460
461 error - perform the error mode action
462 $obj->error(@msgs);
463
464 This method concatenates @msgs into a string and places it in the
465 object as the error message. Also see "errmsg()". It then per‐
466 forms the error mode action. Also see "errmode()".
467
468 If the error mode doesn't cause the program to die, then an unde‐
469 fined value or an empty list is returned depending on the context.
470
471 This method is primarily used by this class or a sub-class to per‐
472 form the user requested action when an error is encountered.
473
474 fhopen - use already open filehandle for I/O
475 $ok = $obj->fhopen($fh);
476
477 This method associates the open filehandle $fh with $obj for fur‐
478 ther I/O. Filehandle $fh must already be opened.
479
480 Suppose you want to use the features of this module to do I/O to
481 something other than a TCP port, for example STDIN or a filehandle
482 opened to read from a process. Instead of opening the object for
483 I/O to a TCP port by using "open()" or "new()", call this method
484 instead.
485
486 The value 1 is returned success, the error mode action is performed
487 on failure.
488
489 get - read block of data
490 $data = $obj->get([Binmode => $mode,]
491 [Errmode => $errmode,]
492 [Telnetmode => $mode,]
493 [Timeout => $secs,]);
494
495 This method reads a block of data from the object and returns it
496 along with any buffered data. If no buffered data is available to
497 return, it will wait for data to read using the timeout specified
498 in the object. You can override that timeout using $secs. Also
499 see "timeout()". If buffered data is available to return, it also
500 checks for a block of data that can be immediately read.
501
502 On eof an undefined value is returned. On time-out or other fail‐
503 ures, the error mode action is performed. To distinguish between
504 eof or an error occurring when the error mode is not set to "die",
505 use "eof()".
506
507 Optional named parameters are provided to override the current set‐
508 tings of binmode, errmode, telnetmode, and timeout.
509
510 getline - read next line
511 $line = $obj->getline([Binmode => $mode,]
512 [Errmode => $errmode,]
513 [Input_record_separator => $chars,]
514 [Rs => $chars,]
515 [Telnetmode => $mode,]
516 [Timeout => $secs,]);
517
518 This method reads and returns the next line of data from the
519 object. You can use "input_record_separator()" to change the
520 notion of what separates a line. The default is "\n". If a line
521 isn't immediately available, this method blocks waiting for a line
522 or a time-out.
523
524 On eof an undefined value is returned. On time-out or other fail‐
525 ures, the error mode action is performed. To distinguish between
526 eof or an error occurring when the error mode is not set to "die",
527 use "eof()".
528
529 Optional named parameters are provided to override the current set‐
530 tings of binmode, errmode, input_record_separator, rs, telnetmode,
531 and timeout. Rs is synonymous with input_record_separator.
532
533 getlines - read next lines
534 @lines = $obj->getlines([Binmode => $mode,]
535 [Errmode => $errmode,]
536 [Input_record_separator => $chars,]
537 [Rs => $chars,]
538 [Telnetmode => $mode,]
539 [Timeout => $secs,]
540 [All => $boolean,]);
541
542 This method reads and returns all the lines of data from the object
543 until end of file is read. You can use "input_record_separator()"
544 to change the notion of what separates a line. The default is
545 "\n". A time-out error occurs if all the lines can't be read
546 within the time-out interval. See "timeout()".
547
548 The behavior of this method was changed in version 3.03. Prior to
549 version 3.03 this method returned just the lines available from the
550 next read. To get that old behavior, use the optional named param‐
551 eter All and set $boolean to "" or 0.
552
553 If only eof is read then an empty list is returned. On time-out or
554 other failures, the error mode action is performed. Use "eof()" to
555 distinguish between reading only eof or an error occurring when the
556 error mode is not set to "die".
557
558 Optional named parameters are provided to override the current set‐
559 tings of binmode, errmode, input_record_separator, rs, telnetmode,
560 and timeout. Rs is synonymous with input_record_separator.
561
562 host - name of remote host
563 $host = $obj->host;
564
565 $prev = $obj->host($host);
566
567 This method designates the remote host for "open()". With no argu‐
568 ment it returns the current host name set in the object. With an
569 argument it sets the current host name to $host and returns the
570 previous host name. You may indicate the remote host using either
571 a hostname or an IP address.
572
573 The default value is "localhost". It may also be set by "open()"
574 or "new()".
575
576 input_log - log all input
577 $fh = $obj->input_log;
578
579 $fh = $obj->input_log($fh);
580
581 $fh = $obj->input_log($filename);
582
583 This method starts or stops logging of input. This is useful when
584 debugging. Also see "dump_log()". Because most command inter‐
585 preters echo back commands received, it's likely all your output
586 will also be in this log. Note that input logging occurs after
587 newline translation. See "binmode()" for details on newline trans‐
588 lation.
589
590 If no argument is given, the log filehandle is returned. An empty
591 string indicates logging is off.
592
593 To stop logging, use an empty string as an argument.
594
595 If an open filehandle is given, it is used for logging and
596 returned. Otherwise, the argument is assumed to be the name of a
597 file, the file is opened for logging and a filehandle to it is
598 returned. If the file can't be opened for writing, the error mode
599 action is performed.
600
601 input_record_separator - input line delimiter
602 $chars = $obj->input_record_separator;
603
604 $prev = $obj->input_record_separator($chars);
605
606 This method designates the line delimiter for input. It's used
607 with "getline()", "getlines()", and "cmd()" to determine lines in
608 the input.
609
610 With no argument this method returns the current input record sepa‐
611 rator set in the object. With an argument it sets the input record
612 separator to $chars and returns the previous value. Note that
613 $chars must have length.
614
615 A warning is printed to STDERR when attempting to set this
616 attribute to a string with no length.
617
618 last_prompt - last prompt read
619 $string = $obj->last_prompt;
620
621 $prev = $obj->last_prompt($string);
622
623 With no argument this method returns the last prompt read by cmd()
624 or login(). See "prompt()". With an argument it sets the last
625 prompt read to $string and returns the previous value. Normally,
626 only internal methods set the last prompt.
627
628 lastline - last line read
629 $line = $obj->lastline;
630
631 $prev = $obj->lastline($line);
632
633 This method retrieves the last line read from the object. This may
634 be a useful error message when the remote side abnormally closes
635 the connection. Typically the remote side will print an error mes‐
636 sage before closing.
637
638 With no argument this method returns the last line read from the
639 object. With an argument it sets the last line read to $line and
640 returns the previous value. Normally, only internal methods set
641 the last line.
642
643 login - perform standard login
644 $ok = $obj->login($username, $password);
645
646 $ok = $obj->login(Name => $username,
647 Password => $password,
648 [Errmode => $mode,]
649 [Prompt => $match,]
650 [Timeout => $secs,]);
651
652 This method performs a standard login by waiting for a login prompt
653 and responding with $username, then waiting for the password prompt
654 and responding with $password, and then waiting for the command
655 interpreter prompt. If any of those prompts sent by the remote
656 side don't match what's expected, this method will time-out, unless
657 timeout is turned off.
658
659 Login prompt must match either of these case insensitive patterns:
660
661 /login[: ]*$/i
662 /username[: ]*$/i
663
664 Password prompt must match this case insensitive pattern:
665
666 /password[: ]*$/i
667
668 The command interpreter prompt must match the current setting of
669 prompt. See "prompt()".
670
671 Use "dump_log()" to debug when this method keeps timing-out and you
672 don't think it should.
673
674 Consider using a combination of "print()" and "waitfor()" as an
675 alternative to this method when it doesn't do what you want, e.g.
676 the remote host doesn't prompt for a username.
677
678 On success, 1 is returned. On time out, eof, or other failures,
679 the error mode action is performed. See "errmode()".
680
681 Optional named parameters are provided to override the current set‐
682 tings of errmode, prompt, and timeout.
683
684 max_buffer_length - maximum size of input buffer
685 $len = $obj->max_buffer_length;
686
687 $prev = $obj->max_buffer_length($len);
688
689 This method designates the maximum size of the input buffer. An
690 error is generated when a read causes the buffer to exceed this
691 limit. The default value is 1,048,576 bytes (1MB). The input buf‐
692 fer can grow much larger than the block size when you continuously
693 read using "getline()" or "waitfor()" and the data stream contains
694 no newlines or matching waitfor patterns.
695
696 With no argument, this method returns the current maximum buffer
697 length set in the object. With an argument it sets the maximum
698 buffer length to $len and returns the previous value. Values of
699 $len smaller than 512 will be adjusted to 512.
700
701 A warning is printed to STDERR when attempting to set this
702 attribute to something that isn't a positive integer.
703
704 ofs - field separator for print
705 $chars = $obj->ofs
706
707 $prev = $obj->ofs($chars);
708
709 This method is synonymous with "output_field_separator()".
710
711 open - connect to port on remote host
712 $ok = $obj->open($host);
713
714 $ok = $obj->open([Host => $host,]
715 [Port => $port,]
716 [Errmode => $mode,]
717 [Timeout => $secs,]);
718
719 This method opens a TCP connection to $port on $host. If either
720 argument is missing then the current value of "host()" or "port()"
721 is used. Optional named parameters are provided to override the
722 current setting of errmode and timeout.
723
724 On success 1 is returned. On time-out or other connection fail‐
725 ures, the error mode action is performed. See "errmode()".
726
727 Time-outs don't work for this method on machines that don't imple‐
728 ment SIGALRM - most notably MS-Windows machines. For those
729 machines, an error is returned when the system reaches its own
730 time-out while trying to connect.
731
732 A side effect of this method is to reset the alarm interval associ‐
733 ated with SIGALRM.
734
735 option_accept - indicate willingness to accept a TELNET option
736 $fh = $obj->option_accept([Do => $telopt,]
737 [Dont => $telopt,]
738 [Will => $telopt,]
739 [Wont => $telopt,]);
740
741 This method is used to indicate whether to accept or reject an
742 offer to enable a TELNET option made by the remote side. If you're
743 using Do or Will to indicate a willingness to enable, then a noti‐
744 fication callback must have already been defined by a prior call to
745 "option_callback()". See "option_callback()" for details on
746 receiving enable/disable notification of a TELNET option.
747
748 You can give multiple Do, Dont, Will, or Wont arguments for differ‐
749 ent TELNET options in the same call to this method.
750
751 The following example describes the meaning of the named parame‐
752 ters. A TELNET option, such as "TELOPT_ECHO" used below, is an
753 integer constant that you can import from Net::Telnet. See the
754 source in file Telnet.pm for the complete list.
755
756 * Do => "TELOPT_ECHO"
757
758 * we'll accept an offer to enable the echo option on the
759 local side
760
761 * Dont => "TELOPT_ECHO"
762
763 * we'll reject an offer to enable the echo option on the
764 local side
765
766 * Will => "TELOPT_ECHO"
767
768 * we'll accept an offer to enable the echo option on the
769 remote side
770
771 * Wont => "TELOPT_ECHO"
772
773 * we'll reject an offer to enable the echo option on the
774 remote side
775
776 * Use "option_send()" to send a request to the remote side to enable
777 or disable a particular TELNET option.
778
779 option_callback - define the option negotiation callback
780 $coderef = $obj->option_callback;
781
782 $prev = $obj->option_callback($coderef);
783
784 This method defines the callback subroutine that's called when a
785 TELNET option is enabled or disabled. Once defined, the
786 option_callback may not be undefined. However, calling this method
787 with a different $coderef changes it.
788
789 A warning is printed to STDERR when attempting to set this
790 attribute to something that isn't a coderef.
791
792 Here are the circumstances that invoke $coderef:
793
794 * An option becomes enabled because the remote side requested an
795 enable and "option_accept()" had been used to arrange that it
796 be accepted.
797
798 * The remote side arbitrarily decides to disable an option that
799 is currently enabled. Note that Net::Telnet always accepts a
800 request to disable from the remote side.
801
802 * "option_send()" was used to send a request to enable or disable
803 an option and the response from the remote side has just been
804 received. Note, that if a request to enable is rejected then
805 $coderef is still invoked even though the option didn't change.
806
807 * Here are the arguments passed to &$coderef:
808
809 &$coderef($obj, $option, $is_remote,
810 $is_enabled, $was_enabled, $buf_position);
811
812 * 1. $obj is the Net::Telnet object
813
814 * 2. $option is the TELNET option. Net::Telnet exports con‐
815 stants for the various TELNET options which just equate to an
816 integer.
817
818 * 3. $is_remote is a boolean indicating for which side the
819 option applies.
820
821 * 4. $is_enabled is a boolean indicating the option is enabled
822 or disabled
823
824 * 5. $was_enabled is a boolean indicating the option was previ‐
825 ously enabled or disabled
826
827 * 6. $buf_position is an integer indicating the position in the
828 object's input buffer where the option takes effect. See "buf‐
829 fer()" to access the object's input buffer.
830
831 option_log - log all TELNET options sent or received
832 $fh = $obj->option_log;
833
834 $fh = $obj->option_log($fh);
835
836 $fh = $obj->option_log($filename);
837
838 This method starts or stops logging of all TELNET options being
839 sent or received. This is useful for debugging when you send
840 options via "option_send()" or you arrange to accept option
841 requests from the remote side via "option_accept()". Also see
842 "dump_log()".
843
844 If no argument is given, the log filehandle is returned. An empty
845 string indicates logging is off.
846
847 To stop logging, use an empty string as an argument.
848
849 If an open filehandle is given, it is used for logging and
850 returned. Otherwise, the argument is assumed to be the name of a
851 file, the file is opened for logging and a filehandle to it is
852 returned. If the file can't be opened for writing, the error mode
853 action is performed.
854
855 option_send - send TELNET option negotiation request
856 $ok = $obj->option_send([Do => $telopt,]
857 [Dont => $telopt,]
858 [Will => $telopt,]
859 [Wont => $telopt,]
860 [Async => $boolean,]);
861
862 This method is not yet implemented. Look for it in a future ver‐
863 sion.
864
865 option_state - get current state of a TELNET option
866 $hashref = $obj->option_state($telopt);
867
868 This method returns a hashref containing a copy of the current
869 state of TELNET option $telopt.
870
871 Here are the values returned in the hash:
872
873 * $hashref->{remote_enabled}
874
875 * boolean that indicates if the option is enabled on the
876 remote side.
877
878 * $hashref->{remote_enable_ok}
879
880 * boolean that indicates if it's ok to accept an offer to
881 enable this option on the remote side.
882
883 * $hashref->{remote_state}
884
885 * string used to hold the internal state of option negotia‐
886 tion for this option on the remote side.
887
888 * $hashref->{local_enabled}
889
890 * boolean that indicates if the option is enabled on the
891 local side.
892
893 * $hashref->{local_enable_ok}
894
895 * boolean that indicates if it's ok to accept an offer to
896 enable this option on the local side.
897
898 * $hashref->{local_state}
899
900 * string used to hold the internal state of option negotia‐
901 tion for this option on the local side.
902
903 ors - output line delimiter
904 $chars = $obj->ors;
905
906 $prev = $obj->ors($chars);
907
908 This method is synonymous with "output_record_separator()".
909
910 output_field_separator - field separator for print
911 $chars = $obj->output_field_separator;
912
913 $prev = $obj->output_field_separator($chars);
914
915 This method designates the output field separator for "print()".
916 Ordinarily the print method simply prints out the comma separated
917 fields you specify. Set this to specify what's printed between
918 fields.
919
920 With no argument this method returns the current output field sepa‐
921 rator set in the object. With an argument it sets the output field
922 separator to $chars and returns the previous value.
923
924 By default it's set to an empty string.
925
926 output_log - log all output
927 $fh = $obj->output_log;
928
929 $fh = $obj->output_log($fh);
930
931 $fh = $obj->output_log($filename);
932
933 This method starts or stops logging of output. This is useful when
934 debugging. Also see "dump_log()". Because most command inter‐
935 preters echo back commands received, it's likely all your output
936 would also be in an input log. See "input_log()". Note that out‐
937 put logging occurs before newline translation. See "binmode()" for
938 details on newline translation.
939
940 If no argument is given, the log filehandle is returned. An empty
941 string indicates logging is off.
942
943 To stop logging, use an empty string as an argument.
944
945 If an open filehandle is given, it is used for logging and
946 returned. Otherwise, the argument is assumed to be the name of a
947 file, the file is opened for logging and a filehandle to it is
948 returned. If the file can't be opened for writing, the error mode
949 action is performed.
950
951 output_record_separator - output line delimiter
952 $chars = $obj->output_record_separator;
953
954 $prev = $obj->output_record_separator($chars);
955
956 This method designates the output line delimiter for "print()" and
957 "cmd()". Set this to specify what's printed at the end of
958 "print()" and "cmd()".
959
960 The output record separator is set to "\n" by default, so there's
961 no need to append all your commands with a newline. To avoid
962 printing the output_record_separator use "put()" or set the out‐
963 put_record_separator to an empty string.
964
965 With no argument this method returns the current output record sep‐
966 arator set in the object. With an argument it sets the output
967 record separator to $chars and returns the previous value.
968
969 port - remote port
970 $port = $obj->port;
971
972 $prev = $obj->port($port);
973
974 This method designates the remote TCP port. With no argument this
975 method returns the current port number. With an argument it sets
976 the current port number to $port and returns the previous port. If
977 $port is a TCP service name, then it's first converted to a port
978 number using the perl function "getservbyname()".
979
980 The default value is 23. It may also be set by "open()" or
981 "new()".
982
983 A warning is printed to STDERR when attempting to set this
984 attribute to something that's not a positive integer or a valid TCP
985 service name.
986
987 print - write to object
988 $ok = $obj->print(@list);
989
990 This method writes @list followed by the output_record_separator to
991 the open object and returns 1 if all data was successfully written.
992 On time-out or other failures, the error mode action is performed.
993 See "errmode()".
994
995 By default, the "output_record_separator()" is set to "\n" so all
996 your commands automatically end with a newline. In most cases your
997 output is being read by a command interpreter which won't accept a
998 command until newline is read. This is similar to someone typing a
999 command and hitting the return key. To avoid printing a trailing
1000 "\n" use "put()" instead or set the output_record_separator to an
1001 empty string.
1002
1003 On failure, it's possible that some data was written. If you
1004 choose to try and recover from a print timing-out, use
1005 "print_length()" to determine how much was written before the error
1006 occurred.
1007
1008 You may also use the output field separator to print a string
1009 between the list elements. See "output_field_separator()".
1010
1011 print_length - number of bytes written by print
1012 $num = $obj->print_length;
1013
1014 This returns the number of bytes successfully written by the most
1015 recent "print()" or "put()".
1016
1017 prompt - pattern to match a prompt
1018 $matchop = $obj->prompt;
1019
1020 $prev = $obj->prompt($matchop);
1021
1022 This method sets the pattern used to find a prompt in the input
1023 stream. It must be a string representing a valid perl pattern
1024 match operator. The methods "login()" and "cmd()" try to read
1025 until matching the prompt. They will fail with a time-out error if
1026 the pattern you've chosen doesn't match what the remote side sends.
1027
1028 With no argument this method returns the prompt set in the object.
1029 With an argument it sets the prompt to $matchop and returns the
1030 previous value.
1031
1032 The default prompt is '/[\$%#>] $/'
1033
1034 Always use single quotes, instead of double quotes, to construct
1035 $matchop (e.g. '/bash\$ $/'). If you're constructing a DOS like
1036 file path, you'll need to use four backslashes to represent one
1037 (e.g. '/c:\\\\users\\\\bill>$/i').
1038
1039 Of course don't forget about regexp metacharacters like ".", "[",
1040 or "$". You'll only need a single backslash to quote them. The
1041 anchor metacharacters "^" and "$" refer to positions in the input
1042 buffer.
1043
1044 A warning is printed to STDERR when attempting to set this
1045 attribute with a match operator missing its opening delimiter.
1046
1047 put - write to object
1048 $ok = $obj->put($string);
1049
1050 $ok = $obj->put(String => $string,
1051 [Binmode => $mode,]
1052 [Errmode => $errmode,]
1053 [Telnetmode => $mode,]
1054 [Timeout => $secs,]);
1055
1056 This method writes $string to the opened object and returns 1 if
1057 all data was successfully written. This method is like "print()"
1058 except that it doesn't write the trailing output_record_separator
1059 ("\n" by default). On time-out or other failures, the error mode
1060 action is performed. See "errmode()".
1061
1062 On failure, it's possible that some data was written. If you
1063 choose to try and recover from a put timing-out, use
1064 "print_length()" to determine how much was written before the error
1065 occurred.
1066
1067 Optional named parameters are provided to override the current set‐
1068 tings of binmode, errmode, telnetmode, and timeout.
1069
1070 rs - input line delimiter
1071 $chars = $obj->rs;
1072
1073 $prev = $obj->rs($chars);
1074
1075 This method is synonymous with "input_record_separator()".
1076
1077 telnetmode - turn off/on telnet command interpretation
1078 $mode = $obj->telnetmode;
1079
1080 $prev = $obj->telnetmode($mode);
1081
1082 This method controls whether or not TELNET commands in the data
1083 stream are recognized and handled. The TELNET protocol uses cer‐
1084 tain character sequences sent in the data stream to control the
1085 session. If the port you're connecting to isn't using the TELNET
1086 protocol, then you should turn this mode off. The default is on.
1087
1088 If no argument is given, the current mode is returned.
1089
1090 If $mode is 0 then telnet mode is off. If $mode is 1 then telnet
1091 mode is on.
1092
1093 timed_out - time-out indicator
1094 $boolean = $obj->timed_out;
1095
1096 $prev = $obj->timed_out($boolean);
1097
1098 This method indicates if a previous read, write, or open method
1099 timed-out. Remember that timing-out is itself an error. To be
1100 able to invoke "timed_out()" after a time-out error, you'd have to
1101 change the default error mode to something other than "die". See
1102 "errmode()".
1103
1104 With no argument this method returns 1 if the previous method
1105 timed-out. With an argument it sets the indicator. Normally, only
1106 internal methods set this indicator.
1107
1108 timeout - I/O time-out interval
1109 $secs = $obj->timeout;
1110
1111 $prev = $obj->timeout($secs);
1112
1113 This method sets the timeout interval that's used when performing
1114 I/O or connecting to a port. When a method doesn't complete within
1115 the timeout interval then it's an error and the error mode action
1116 is performed.
1117
1118 A timeout may be expressed as a relative or absolute value. If
1119 $secs is greater than or equal to the time the program started, as
1120 determined by $^T, then it's an absolute time value for when time-
1121 out occurs. The perl function "time()" may be used to obtain an
1122 absolute time value. For a relative time-out value less than $^T,
1123 time-out happens $secs from when the method begins.
1124
1125 If $secs is 0 then time-out occurs if the data cannot be immedi‐
1126 ately read or written. Use the undefined value to turn off timing-
1127 out completely.
1128
1129 With no argument this method returns the timeout set in the object.
1130 With an argument it sets the timeout to $secs and returns the pre‐
1131 vious value. The default timeout value is 10 seconds.
1132
1133 A warning is printed to STDERR when attempting to set this
1134 attribute to something that's not an "undef" or a non-negative
1135 integer.
1136
1137 waitfor - wait for pattern in the input
1138 $ok = $obj->waitfor($matchop);
1139 $ok = $obj->waitfor([Match => $matchop,]
1140 [String => $string,]
1141 [Binmode => $mode,]
1142 [Errmode => $errmode,]
1143 [Telnetmode => $mode,]
1144 [Timeout => $secs,]);
1145
1146 ($prematch, $match) = $obj->waitfor($matchop);
1147 ($prematch, $match) = $obj->waitfor([Match => $matchop,]
1148 [String => $string,]
1149 [Binmode => $mode,]
1150 [Errmode => $errmode,]
1151 [Telnetmode => $mode,]
1152 [Timeout => $secs,]);
1153
1154 This method reads until a pattern match or string is found in the
1155 input stream. All the characters before and including the match
1156 are removed from the input stream.
1157
1158 In a list context the characters before the match and the matched
1159 characters are returned in $prematch and $match. In a scalar con‐
1160 text, the matched characters and all characters before it are dis‐
1161 carded and 1 is returned on success. On time-out, eof, or other
1162 failures, for both list and scalar context, the error mode action
1163 is performed. See "errmode()".
1164
1165 You can specify more than one pattern or string by simply providing
1166 multiple Match and/or String named parameters. A $matchop must be
1167 a string representing a valid Perl pattern match operator. The
1168 $string is just a substring to find in the input stream.
1169
1170 Use "dump_log()" to debug when this method keeps timing-out and you
1171 don't think it should.
1172
1173 An optional named parameter is provided to override the current
1174 setting of timeout.
1175
1176 To avoid unexpected backslash interpretation, always use single
1177 quotes instead of double quotes to construct a match operator argu‐
1178 ment for "prompt()" and "waitfor()" (e.g. '/bash\$ $/'). If you're
1179 constructing a DOS like file path, you'll need to use four back‐
1180 slashes to represent one (e.g. '/c:\\\\users\\\\bill>$/i').
1181
1182 Of course don't forget about regexp metacharacters like ".", "[",
1183 or "$". You'll only need a single backslash to quote them. The
1184 anchor metacharacters "^" and "$" refer to positions in the input
1185 buffer.
1186
1187 Optional named parameters are provided to override the current set‐
1188 tings of binmode, errmode, telnetmode, and timeout.
1189
1191 RFC 854
1192 TELNET Protocol Specification
1193
1194 ftp://ftp.isi.edu/in-notes/rfc854.txt
1195
1196 RFC 1143
1197 Q Method of Implementing TELNET Option Negotiation
1198
1199 ftp://ftp.isi.edu/in-notes/rfc1143.txt
1200
1201 TELNET Option Assignments
1202 http://www.iana.org/assignments/telnet-options
1203
1205 This example gets the current weather forecast for Brainerd, Minnesota.
1206
1207 my ($forecast, $t);
1208
1209 use Net::Telnet ();
1210 $t = new Net::Telnet;
1211 $t->open("rainmaker.wunderground.com");
1212
1213 ## Wait for first prompt and "hit return".
1214 $t->waitfor('/continue:.*$/');
1215 $t->print("");
1216
1217 ## Wait for second prompt and respond with city code.
1218 $t->waitfor('/city code.*$/');
1219 $t->print("BRD");
1220
1221 ## Read and print the first page of forecast.
1222 ($forecast) = $t->waitfor('/[ \t]+press return to continue/i');
1223 print $forecast;
1224
1225 exit;
1226
1227 This example checks a POP server to see if you have mail.
1228
1229 my ($hostname, $line, $passwd, $pop, $username);
1230
1231 $hostname = "your_destination_host_here";
1232 $username = "your_username_here";
1233 $passwd = "your_password_here";
1234
1235 use Net::Telnet ();
1236 $pop = new Net::Telnet (Telnetmode => 0);
1237 $pop->open(Host => $hostname,
1238 Port => 110);
1239
1240 ## Read connection message.
1241 $line = $pop->getline;
1242 die $line unless $line =~ /^\+OK/;
1243
1244 ## Send user name.
1245 $pop->print("user $username");
1246 $line = $pop->getline;
1247 die $line unless $line =~ /^\+OK/;
1248
1249 ## Send password.
1250 $pop->print("pass $passwd");
1251 $line = $pop->getline;
1252 die $line unless $line =~ /^\+OK/;
1253
1254 ## Request status of messages.
1255 $pop->print("list");
1256 $line = $pop->getline;
1257 print $line;
1258
1259 exit;
1260
1261 Here's an example that uses the ssh program to connect to a remote
1262 host. Because the ssh program reads and writes to its controlling ter‐
1263 minal, the IO::Pty module is used to create a new pseudo terminal for
1264 use by ssh. A new Net::Telnet object is then created to read and write
1265 to that pseudo terminal. To use the code below, substitute "changeme"
1266 with the actual host, user, password, and command prompt.
1267
1268 ## Main program.
1269 {
1270 my ($pty, $ssh, @lines);
1271 my $host = "changeme";
1272 my $user = "changeme";
1273 my $password = "changeme";
1274 my $prompt = '/changeme:~> $/';
1275
1276 ## Start ssh program.
1277 $pty = &spawn("ssh", "-l", $user, $host); # spawn() defined below
1278
1279 ## Create a Net::Telnet object to perform I/O on ssh's tty.
1280 use Net::Telnet;
1281 $ssh = new Net::Telnet (-fhopen => $pty,
1282 -prompt => $prompt,
1283 -telnetmode => 0,
1284 -cmd_remove_mode => 1,
1285 -output_record_separator => "\r");
1286
1287 ## Login to remote host.
1288 $ssh->waitfor(-match => '/password: ?$/i',
1289 -errmode => "return")
1290 or die "problem connecting to host: ", $ssh->lastline;
1291 $ssh->print($password);
1292 $ssh->waitfor(-match => $ssh->prompt,
1293 -errmode => "return")
1294 or die "login failed: ", $ssh->lastline;
1295
1296 ## Send command, get and print its output.
1297 @lines = $ssh->cmd("who");
1298 print @lines;
1299
1300 exit;
1301 } # end main program
1302
1303 sub spawn {
1304 my(@cmd) = @_;
1305 my($pid, $pty, $tty, $tty_fd);
1306
1307 ## Create a new pseudo terminal.
1308 use IO::Pty ();
1309 $pty = new IO::Pty
1310 or die $!;
1311
1312 ## Execute the program in another process.
1313 unless ($pid = fork) { # child process
1314 die "problem spawning program: $!\n" unless defined $pid;
1315
1316 ## Disassociate process from existing controlling terminal.
1317 use POSIX ();
1318 POSIX::setsid
1319 or die "setsid failed: $!";
1320
1321 ## Associate process with a new controlling terminal.
1322 $tty = $pty->slave;
1323 $tty_fd = $tty->fileno;
1324 close $pty;
1325
1326 ## Make stdio use the new controlling terminal.
1327 open STDIN, "<&$tty_fd" or die $!;
1328 open STDOUT, ">&$tty_fd" or die $!;
1329 open STDERR, ">&STDOUT" or die $!;
1330 close $tty;
1331
1332 ## Execute requested program.
1333 exec @cmd
1334 or die "problem executing $cmd[0]\n";
1335 } # end child process
1336
1337 $pty;
1338 } # end sub spawn
1339
1340 Here's an example that changes a user's login password. Because the
1341 passwd program always prompts for passwords on its controlling termi‐
1342 nal, the IO::Pty module is used to create a new pseudo terminal for use
1343 by passwd. A new Net::Telnet object is then created to read and write
1344 to that pseudo terminal. To use the code below, substitute "changeme"
1345 with the actual old and new passwords.
1346
1347 my ($pty, $passwd);
1348 my $oldpw = "changeme";
1349 my $newpw = "changeme";
1350
1351 ## Start passwd program.
1352 $pty = &spawn("passwd"); # spawn() defined above
1353
1354 ## Create a Net::Telnet object to perform I/O on passwd's tty.
1355 use Net::Telnet;
1356 $passwd = new Net::Telnet (-fhopen => $pty,
1357 -timeout => 2,
1358 -output_record_separator => "\r",
1359 -telnetmode => 0,
1360 -cmd_remove_mode => 1);
1361 $passwd->errmode("return");
1362
1363 ## Send existing password.
1364 $passwd->waitfor('/password: ?$/i')
1365 or die "no old password prompt: ", $passwd->lastline;
1366 $passwd->print($oldpw);
1367
1368 ## Send new password.
1369 $passwd->waitfor('/new password: ?$/i')
1370 or die "bad old password: ", $passwd->lastline;
1371 $passwd->print($newpw);
1372
1373 ## Send new password verification.
1374 $passwd->waitfor('/new password: ?$/i')
1375 or die "bad new password: ", $passwd->lastline;
1376 $passwd->print($newpw);
1377
1378 ## Display success or failure.
1379 $passwd->waitfor('/changed/')
1380 or die "bad new password: ", $passwd->lastline;
1381 print $passwd->lastline;
1382
1383 $passwd->close;
1384 exit;
1385
1386 Here's an example you can use to down load a file of any type. The
1387 file is read from the remote host's standard output using cat. To pre‐
1388 vent any output processing, the remote host's standard output is put in
1389 raw mode using the Bourne shell. The Bourne shell is used because some
1390 shells, notably tcsh, prevent changing tty modes. Upon completion, FTP
1391 style statistics are printed to stderr.
1392
1393 my ($block, $filename, $host, $hostname, $k_per_sec, $line,
1394 $num_read, $passwd, $prevblock, $prompt, $size, $size_bsd,
1395 $size_sysv, $start_time, $total_time, $username);
1396
1397 $hostname = "your_destination_host_here";
1398 $username = "your_username_here";
1399 $passwd = "your_password_here";
1400 $filename = "your_download_file_here";
1401
1402 ## Connect and login.
1403 use Net::Telnet ();
1404 $host = new Net::Telnet (Timeout => 30,
1405 Prompt => '/[%#>] $/');
1406 $host->open($hostname);
1407 $host->login($username, $passwd);
1408
1409 ## Make sure prompt won't match anything in send data.
1410 $prompt = "_funkyPrompt_";
1411 $host->prompt("/$prompt\$/");
1412 $host->cmd("set prompt = '$prompt'");
1413
1414 ## Get size of file.
1415 ($line) = $host->cmd("/bin/ls -l $filename");
1416 ($size_bsd, $size_sysv) = (split ' ', $line)[3,4];
1417 if ($size_sysv =~ /^\d+$/) {
1418 $size = $size_sysv;
1419 }
1420 elsif ($size_bsd =~ /^\d+$/) {
1421 $size = $size_bsd;
1422 }
1423 else {
1424 die "$filename: no such file on $hostname";
1425 }
1426
1427 ## Start sending the file.
1428 binmode STDOUT;
1429 $host->binmode(1);
1430 $host->print("/bin/sh -c 'stty raw; cat $filename'");
1431 $host->getline; # discard echoed back line
1432
1433 ## Read file a block at a time.
1434 $num_read = 0;
1435 $prevblock = "";
1436 $start_time = time;
1437 while (($block = $host->get) and ($block !~ /$prompt$/o)) {
1438 if (length $block >= length $prompt) {
1439 print $prevblock;
1440 $num_read += length $prevblock;
1441 $prevblock = $block;
1442 }
1443 else {
1444 $prevblock .= $block;
1445 }
1446
1447 }
1448 $host->close;
1449
1450 ## Print last block without trailing prompt.
1451 $prevblock .= $block;
1452 $prevblock =~ s/$prompt$//;
1453 print $prevblock;
1454 $num_read += length $prevblock;
1455 die "error: expected size $size, received size $num_read\n"
1456 unless $num_read == $size;
1457
1458 ## Print totals.
1459 $total_time = (time - $start_time) ⎪⎪ 1;
1460 $k_per_sec = ($size / 1024) / $total_time;
1461 $k_per_sec = sprintf "%3.1f", $k_per_sec;
1462 warn("$num_read bytes received in $total_time seconds ",
1463 "($k_per_sec Kbytes/s)\n");
1464
1465 exit;
1466
1468 Jay Rogers <jay@rgrs.com>
1469
1471 Copyright 1997, 2000, 2002 by Jay Rogers. All rights reserved. This
1472 program is free software; you can redistribute it and/or modify it
1473 under the same terms as Perl itself.
1474
1475
1476
1477perl v5.8.8 2002-07-16 Net::Telnet(3)