Module Version: 3.03
Source
NAME
SYNOPSIS
DESCRIPTION
What To Know Before Using
Debugging
Style of Named Parameters
Connecting to a Remote MS-Windows Machine
METHODS
SEE ALSO
EXAMPLES
AUTHOR
COPYRIGHT
NAME
Net::Telnet - interact with TELNET port or other TCP ports
SYNOPSIS
use Net::Telnet ();
see METHODS section below
DESCRIPTION
Net::Telnet allows you to make client connections to a TCP port and
do network I/O,
especially to a port using the TELNET protocol.
Simple I/O methods such as print,
get,
and getline are provided.
More sophisticated interactive features are provided because connecting
to a TELNET port ultimately means communicating with a program designed
for human interaction.
These interactive features include the ability to specify a time-out
and to wait for patterns to appear in the input stream,
such as the prompt from a shell.
Other reasons to use this module than strictly with a TELNET port are:
You're not familiar with sockets and you want a simple way to make client connections to TCP services.You want to be able to specify your own time-out while connecting,
reading,
or writing.You're communicating with an interactive program at the other end
of some socket or pipe and you want to wait for certain patterns to
appear.
Here's an example that prints who's logged-on to the remote host sparky.
In addition to a username and password,
you must also know the user's shell prompt,
which for this example is bash$
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/');
$t->open("sparky");
$t->login($username, $passwd);
@lines = $t->cmd("who");
print @lines;
More examples are in the EXAMPLES section below.
Usage questions should be directed to the Usenet newsgroup comp.lang.perl.modules.
Contact me, Jay Rogers <
jay@rgrs.com>, if you find any bugs or have suggestions for improvement.
What To Know Before Using
All output is flushed while all input is buffered. Each object contains its own input buffer.The output record separator for print() and cmd() is set to "\n" by default, so that you don't have to append all your commands with a newline. To avoid printing a trailing "\n" use put() or set the output_record_separator to "".The methods login() and cmd() use the prompt
setting in the object to determine when a login or remote command is
complete. Those methods will fail with a time-out if you don't set the
prompt correctly.Use a combination of print() and waitfor() as an alternative to login() or cmd() when they don't do what you want.Errors such as timing-out are handled according to the error mode
action. The default action is to print an error message to standard
error and have the program die. See the errmode() method for more information.When constructing the match operator argument for prompt() or waitfor(), always use single quotes instead of double quotes to avoid unexpected backslash interpretation (e.g. '/bash\$ $/'). If you're constructing a DOS like file path, you'll need to use four backslashes to represent one (e.g. '/c:\\\\users\\\\bill>$/i').
Of course don't forget about regexp metacharacters like ., [, or $. You'll only need a single backslash to quote them. The anchor metacharacters ^ and $
refer to positions in the input buffer. To avoid matching characters
read that look like a prompt, it's a good idea to end your prompt
pattern with the $ anchor. That way the prompt will only match if it's the last thing read.
In the input stream, each sequence of carriage return and line feed (i.e. "\015\012" or CR LF) is converted to "\n". In the output stream, each occurrence of "\n" is converted to a sequence of CR LF. See binmode() to change the behavior. TCP protocols typically use the ASCII sequence, carriage return and line feed to designate a newline.Timing-out while making a connection is disabled for machines that don't support the alarm() function. Most notably these include MS-Windows machines.You'll need to be running at least Perl version 5.002 to use this
module. This module does not require any libraries that don't already
come with a standard Perl distribution.
If you have the IO:: libraries installed (they come standard with
perl5.004 and later) then IO::Socket::INET is used as a base class,
otherwise FileHandle is used.
Contact me, Jay Rogers <
jay@rgrs.com>, if you find any bugs or have suggestions for improvement.
Debugging
The typical usage bug causes a time-out error because you've made
incorrect assumptions about what the remote side actually sends. The
easiest way to reconcile what the remote side sends with your
expectations is to use input_log() or dump_log().
dump_log() allows you to see the data being sent from the remote side before any translation is done, while input_log()
shows you the results after translation. The translation includes
converting end of line characters, removing and responding to TELNET
protocol commands in the data stream.
Style of Named Parameters
Two different styles of named parameters are supported. This document only shows the IO:: style:
Net::Telnet->new(Timeout => 20);
however the dash-option style is also allowed:
Net::Telnet->new(-timeout => 20);
Connecting to a Remote MS-Windows Machine
By default MS-Windows doesn't come with a TELNET server. However
third party TELNET servers are available. Unfortunately many of these
servers falsely claim to be a TELNET server. This is especially true of
the so-called "Microsoft Telnet Server" that comes installed with some
newer versions MS-Windows.
When a TELNET server first accepts a connection, it must use the
ASCII control characters carriage-return and line-feed to start a new
line (see RFC854). A server like the "Microsoft Telnet Server" that
doesn't do this, isn't a TELNET server. These servers send ANSI
terminal escape sequences to position to a column on a subsequent line
and to even position while writing characters that are adjacent to each
other. Worse, when sending output these servers resend previously sent
command output in a misguided attempt to display an entire terminal
screen.
Connecting Net::Telnet to one of these false TELNET servers makes
your job of parsing command output very difficult. It's better to
replace a false TELNET server with a real TELNET server. The better
TELNET servers for MS-Windows allow you to avoid the ANSI escapes by
turning off something some of them call console mode.
METHODS