Home > Articles > Web Development > Perl

  • Print
  • + Share This

Networking Made Easy

Before we get into details, let's look at two simple programs.

The lgetl.pl script (for "line get local," Figure 1.1) reads the first line of a local file. Call it with the path to the file you want to read, and it will print out the top line. For example, here's what I see when I run the script on a file that contains a quote from James Hogan's "Giants Star":

% lgetl.pl giants_star.txt
"Reintegration complete," ZORAC advised. "We're back in the universe."

This snippet illustrates the typographic conventions this book uses for terminal (command-line interpreter) sessions. The "%" character is the prompt printed out by my command-line interpreter. Bold-faced text is what I (the user) typed. Everything else is regular monospaced font.

Figure 1.1
lgetl.pl—Read the first line of a local file

The script itself is straightforward:

    Lines 1–2: Load modules  We use() the IO::File module, which wraps an object-oriented interface around Perl file operations.

    Line 3: Process the command line argument  We shift() the filename off the command line and store it in a variable named $file.

    Line 4: Open the file  We call the IO::File->new() method to open the file, returning a filehandle, which we store in $fh. Don't worry if the OO syntax is unfamiliar to you; we discuss it more later in this chapter.

    Lines 5–6: Read a line from the filehandle and print it  We use the <> operator to read a line of text from the filehandle into the variable $line, which we immediately print.

Now we'll look at a very similar script named lgetr.pl (for "line get remote," Figure 1.2). It too fetches and prints a line of text, but instead of reading from a local file, this one reads from a remote server. Its command-line argument is the name of a remote host followed by a colon and the name of the network service you want to access.

Figure 1.2
lgetr.pl—Read the first line from a remote server

To read a line of text from the "daytime" service running on the FTP server wuarchive.wustl.edu, we use an argument of "wuarchive.wustl.edu:daytime." This retrieves the current time of day at the remote site:

% lgetr.pl wuarchive.wustl.edu:daytime
Tue Aug 8 06:49:20 2000

To read the welcome banner from the FTP service at the same site, we ask for "wuarchive.wustl.edu:ftp":

% lgetr.pl wuarchive.wustl.edu:ftp
220 wuarchive.wustl.edu FTP server (Version wu-2.6.1(1) Thu Jul 13 21:24:09 CDT 2000) ready.

Or for a change of hosts, we can read the welcome banner from the SMTP (Internet mail) server running at mail.hotmail.com like this:

% lgetr.pl mail.hotmail.com:smtp
220-HotMail (NO UCE) ESMTP server ready at Tue Aug 08 05:24:40 2000

Let's turn to the code for the lgetr.pl script in Figure 1.2.

    Lines 1–2: Load modules  We use() the IO::Socket module, which provides an object-oriented interface for network socket operations.

    Line 3: Process the command line argument  We shift() the host and service name off the command line and store it in a variable named $server.

    Line 4: Open a socket  We call the IO::Socket::INET->new() method to create a "socket" connected to the designated service running on the remote machine. IO::Socket::INET is a filehandle class that is adapted for Internet-based communications. A socket is just a specialized form of filehandle, and can be used interchangeably with other types of filehandles in I/O operations.

    Lines 5–6: Read a line from the socket and print it  We use the <> operator to read a line of text from the socket into the variable $line, which we immediately print.

Feel free to try the lgetr.pl script on your favorite servers. In addition to the services used in the examples above, other services to try include "nntp," the Netnews transfer protocol, "chargen," a test character generator, and "pop3," a protocol for retrieving mail messages. If the script appears to hang indefinitely, you've probably contacted a service that requires the client to send the first line of text, such as an HTTP (Web) server. Just interrupt the script and try a different service name.

Although lgetr.pl doesn't do all that much, it is useful in its own right. You can use it to check the time on a remote machine, or wrap it in a shell script to check the time synchronization of all the servers on your network. You could use it to generate a summary of the machines on your network that are running an SMTP mail server and the software they're using.

Notice the similarity between the two scripts. Simply by changing IO::File->new() to IO::Socket::INET->new(), we have created a fully functional network client. Such is the power of Perl.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.