Network Administration with AWK

If you are looking for an easy way to access your network services, AWK scripting provides the means.
Reading the Ticker

Sometimes we are not really interested in viewing a web page. Imagine a web robot (or agent) that looks at the quotes of the Motorola stock shares every 15 minutes and sends you an e-mail if the price hits a certain limit. A command-line call that is executed every 15 minutes is easily written and stored in a shell script. Also, depending on the content of a data file, sending an e-mail is as straightforward to write as a shell script. Here is a script that reads the ticker for you:

 NetService = "/inet/tcp/0/
 print "GET" |&
 while ((NetService |& getline) > 0)
   print $0

Again, you must insert your proxy's name into the second line. During execution of the script, a request is sent to Yahoo's quote server and the resulting web page should be redirected to a file by you. With a grep command, the price can be extracted from the HTML text and compared to the limit.

Advanced Applications

In these examples, we have seen how useful applications can be written built on the same simple framework. This framework represents only a small fraction of what can be done with GNU AWK's networking device. Both TCP and UDP connections are available and both clients and servers can be written. More of the advanced applications can be seen in the small manual that supplements the official documentation distributed with the GNU AWK sources. (See Resources.)

Treating network connections like files is not a feature unique to GNU AWK. When TCP/IP was integrated into BSD UNIX in the early 80s, the creators of the socket API originally intended networking connections to appear as special files even to the user. But networking turned out to have many special cases which could not be handled in a uniform way with file handling. Later, the Portal File System approach was integrated into BSD UNIX. Portals are similar to GNU AWK's special file but are integrated as a file system into the operating system. This works well because the user can even establish connections at the shell prompt. The most recent implementation of the Korn shell (ksh93) provides virtually the same concept (/dev/tcp) at the shell level. None of these approaches has gained wide acceptance among users. Even Richard Stevens' article on Portals (see Resources) has not changed this.

One other approach to networking at the shell level that has gained some acceptance during the past year is the tool netcat. Originally a kind of UNIX hacker tool, it simply binds the standard input and output of a process to a network connection. It knows TCP as well as UDP, can behave as a server and allows “port scanning”, i.e., checking if there are servers listening at certain ports. This tool is simple to use and powerful, but some of the comments in the source code are quite unprofessional. Seldom have I seen such a large number of indecent curses, foolish hype and pure ignorance in a source file. Recently, netcat has been ported to Windows NT. To a humble user of NT, such a tool is like a long-awaited revelation.

Microsoft Windows

Back to pure AWKism and the different forms this belief takes on. On which platforms other than Linux is the networking feature of GNU AWK 3.1 available? It should work on all UNIX systems that comply with the XPG4 rules; this includes every UNIX that has a significant market share. Although the exact release date for GNU AWK 3.1 has not been set, this new feature should also work on Microsoft Windows 95 and NT as a part of the Cygwin tool set as soon as both are out. Cygwin is a UNIX-compatible programming environment that runs on top of Microsoft's Win32 API. It is currently available only as a beta release, but is already able to compile its own set of sources.

When this article was written, compilation of GNU AWK 3.0.3 worked fine, but 3.1 caused problems. If you intend to compile the sources in this environment, be prepared to experience some trouble. Most importantly, avoid compiling on the same machine you are using for networking with GNU AWK. In case you have only one machine available, reboot between compiling and testing. As of release B20 of the Cygwin tool set, clients and servers written in GNU AWK worked on Windows 95 but no server worked on NT 4.0 SP3. As of release B20.2, the compiler supports linking the file gawk.exe statically with all needed dynamic libraries. This would allow for distributing the GNU AWK interpreter as one single executable, but this executable does not work. Those problems should be solved by the time you read this; therefore, networking on Windows 95 should work.