Network Administration with AWK
What does the scripting language AWK have to do with networking? In the May 1996 LJ, Ian Gordon introduced us to AWK and demonstrated how to solve common problems with this scripting language that is part of Linux and every UNIX-compatible operating system. He summarized:
If your main concern is getting a working program written as quickly as possible, you probably do not want to wrestle with C or C++ for a week to perfect the most efficient algorithm. By trading off the speed advantages and control features of C (or another compiled language) for ease of use, gawk lets you get the job done quickly and relatively painlessly.
Let's look at an example. It asks the finger service of your local machine if a particular user is logged in.
BEGIN {
NetService = "/inet/tcp/0/localhost/finger"
print "
while ((NetService |& getline) > 0)
print $0
close(NetService)
}
Store this script in a file named finger.awk and let GNU AWK 3.1 execute it by typing gawk -f finger.awk. The strange pipe symbol, |&, is the second and last addition to the AWK language needed for networking. When communicating over a network, we have to use |& instead of simply |.
After telling the service on the machine which user it is looking for, the program repeatedly reads lines that form the reply. When no more lines are received (because the service has closed the connection), the program closes the socket before finishing. Try replacing name by your login name or the name of someone else logged in. If you want a list of all users currently logged in, replace name by an empty string (""). Also, change localhost to another machine name in your local network; doing so allows you to watch who is logged in on machines at remote locations.
Okay, this is not really an exciting application. The result you get is identical to the one you get by typing finger name@localhost at the shell prompt. So, let's try a really useful application. Today, many Coke machines are connected to the Internet. A short list of such machines can be found at http://www5.biostr.washington.edu/~jsp/coke.html. There, you see that the way to access them is identical to what we did in our first (and not so exciting) example—a finger request. Let us take the first Coke machine from the list and ask the machine which kinds of soft drinks are available there.
BEGIN {
NetService = "/inet/tcp/0/cs.wisc.edu/finger"
print "coke" |& NetService
while ((NetService |& getline) > 0)
print $0
close(NetService)
}
Usually you get a reply with information on the different flavours of Coke and root beer currently available. If you have an account there, you can also order a drink. Many other machines of this kind are connected to the Internet. (See Resources.)
Both examples shown would work even if we deleted the final close command, because the operating system closes any open connection by default when a script reaches the end of execution. In order to avoid portability problems, we always close connections explicitly.
Unlike the Coke machine service, most web services we access usually transmit HTML pages across the Internet with a protocol named HTTP. To most people, this is the real Internet. Can we access the real Internet with GNU AWK? Certainly. We just have to make sure we connect to port 80 of the web server instead of the finger port. This way, we can connect to the Yahoo machine and let it tell us the weather conditions at the place we live.
BEGIN {
NetService = "/inet/tcp/0/
print "GET http://weather.yahoo.com/forecast/Bremen_DL_c.html" |&
NetService
while ((NetService |& getline) > 0)
print $0
close(NetService)
}
Before starting this script, make sure you know which proxy server your provider uses and insert its name into the second line. If you do not use a proxy, insert the name of the web server (weather.yahoo.com). The result is the HTML content of the web page. It is up to your scripts to bring it into a more readable form or to extract the details of interest for further processing.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android






2 hours 23 min ago
6 hours 54 min ago
12 hours 1 min ago
13 hours 1 min ago
22 hours 29 min ago
22 hours 39 min ago
1 day 4 hours ago
1 day 8 hours ago
1 day 9 hours ago
1 day 9 hours ago