Simulating Telnet from a Program
You want to simulate a telnet connection from your program by logging into a remote machine, issuing commands, and reacting to what is sent. This has many applications, from automating tasks on machines you can telnet to but which don’t support scripting or rsh, to simply testing whether a machine’s telnet daemon is still running.
Use the CPAN module Net::Telnet:
use Net::Telnet; $t = Net::Telnet->new( Timeout => 10, Prompt => '/%/', Host => $hostname ); $t->login($username, $password); @files = $t->cmd("ls"); $t->print("top"); (undef, $process_string) = $t->waitfor('/\d+ processes/'); $t->close;
Net::Telnet provides an object-oriented interface to the telnet
protocol. Create a connection with
Net::Telnet->new, and then interact with the
remote machine using method calls on the resulting object.
new method named parameters, passed in
hash-like form. We’ll only cover only a few of many possible
parameters. The most important is
machine you’re telnetting to. The default host is
localhost. If you want to telnet to a port other
than one telnet normally uses, specify this in the
Port option. Error handling is done through the
function whose reference is specified in the
Another important option is
Prompt. When you log
in or run a command, Net::Telnet uses the
pattern to determine when the login or command has completed. The
which matches the common shell ...