Chapter 48. Net::Telnet

Jay Rogers

This article introduces Net::Telnet, a module that allows your Perl program to communicate with networked hosts or devices such as workstations, terminal servers, routers, and the like. The dialog is established via a client connection to a TCP port—typically, to a port using the telnet protocol.

Simple I/O methods such as print, get, and getline are provided, of course, but more sophisticated interactive features are available as well—features tailored for communicating with programs intended solely for human interaction. This includes the ability to specify a timeout and to wait for a pattern to appear in the input stream, such as a shell prompt.

Net::Telnet is written entirely in Perl; it doesn’t require a locally installed telnet program. This makes Net::Telnet especially easy to install and use on those ubiquitous Windows machines.

The Problem

Sounded easy when you first thought about it: your Perl program needs to monitor disk space on a remote machine named sparky, but it can access sparky only by connecting to it via telnet. You try something like this:

	open  TELNET, "|telnet sparky";

	print TELNET  "joebob\n";

	print TELNET  "passwd-for-joebob\n";

	print TELNET  "df -k\n";

You find that piping commands to telnet’s standard input doesn’t work. The telnet program connects just fine, but sparky doesn’t process any of the commands you send it. You discover that your telnet only reads from the terminal (also known as a tty ) and not from its standard input. ...

Get Computer Science & Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.