- Table of Contents
- Copyright
- About the Lead Authors
- About the Contributing Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- I. Red Hat Linux Installation and User Services
- Chapter 1. Introduction to Red Hat Linux
- Chapter 2. Installation of Your Red Hat System
- Chapter 3. LILO and Other Boot Managers
- Chapter 4. Configuring the X Window System, Version 11
- Chapter 5. Window Managers
- Chapter 6. Connecting to the Internet
- Chapter 7. IRC, ICQ, and Chat Clients
- Chapter 8. Using Multimedia and Graphics Clients
- II. Configuring Services
- Chapter 9. System Startup and Shutdown
- Chapter 10. SMTP and Protocols
- Chapter 11. FTP
- Chapter 12. Apache Server
- Chapter 13. Internet News
- Chapter 14. Domain Name Service and Dynamic Host Configuration Protocol
- Chapter 15. NIS: Network Information Service
- Chapter 16. NFS: Network Filesystem
- Chapter 17. Samba
- III. System Administration and Management
- Chapter 18. Linux Filesystems, Disks, and Other Devices
- Chapter 19. Printing with Linux
- Chapter 20. TCP/IP Network Management
- Chapter 21. Linux System Administration
- Chapter 22. Backup and Restore
- Chapter 23. System Security
- IV. Red Hat Development and Productivity
- Chapter 24. Linux C/C++ Programming Tools
- Chapter 25. Shell Scripting
- Chapter 26. Automating Tasks
- Chapter 27. Configuring and Building Kernels
- Chapter 28. Emulators, Tools, and Window Clients
- V. Appendixes
- A. The Linux Documentation Project
- B. Top Linux Commands and Utilities
- C. The GNU General Public License
- D. Red Hat Linux RPM Package Listings
Other Mechanisms: Expect, Perl, and More
Are you ready to move beyond the constraints of the UNIX shell? Several alternative technologies are free, easy to install, easy to learn, and more powerful—that is, with richer capabilities and more structured syntax—than the shell. A few examples will suggest what they have to offer.
Expect
Expect, by Don Libes, is scripting language that works with many different programs, and can be used as a powerful software tool for automation. Why? Expect automates interactions, particularly those involving terminal control and time delays, that no other tool has attempted. Many command-line applications have the reputation for being unscriptable because they involve password entry and refuse to accept redirection of standard input for this purpose. That's no problem for the expect command, however. Under Red Hat Linux, expect is installed under the /usr/bin directory, and you'll find documentation in its manual page.
Create a script hold with the contents of Listing 26.4.
Example 26.4. hold—A "Keep-Alive" Written in Expect
#!/usr /bin/expect
# Usage: "hold HOST USER PASS".
# Action: login to node HOST as USER. Offer a shell prompt for
# normal usage, and also print to the screen the word HELD
# every five seconds, to exercise the connection periodically.
# This is useful for testing and using WANs with short time-outs.
# You can walk away from the keyboard, and never lose your
# connection through a time-out.
# WARNING: the security hazard of passing a password through the
# command line makes this example only illustrative. Modify to
# a particular security situation as appropriate.
set hostname [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
# There's trouble if $username's prompt is not set to "...} ".
# A more sophisticated manager knows how to look for different
# prompts on different hosts.
set prompt_sequence "} "
spawn telnet $hostname
expect "login: "
send "$username\r"
expect "Password:"
send "$password\r"
# Some hosts don't inquire about TERM. That's another
# complexification to consider before widespread use
# of this application is practical.
# Note use of global [gl] pattern matching to parse "*"
# as a wildcard.
expect -gl "TERM = (*)"
send "\r"
expect $prompt_sequence
send "sh -c 'while true; do echo HELD; sleep 5; done'\r"
interact
This script starts a telnet session and then keeps the connection open by sending the string "HELD" every five seconds. I work with several telephone lines that are used with short timeouts, as a check on out-of-pocket expenses. I use a variant of the script in Listing 26.4 daily, for I often need that to hold one of the connections open.
Expect is an extension to tcl, so it is fully programmable with tcl capabilities. For information about tcl and tk from its author, Dr. John Ousterhout, visit http://www.sun.com/960710/cover/ousterhout.html. For more information about Expect, visit http://expect.nist.gov/.
Perl
Some consider Perl the most popular scripting language for Red Hat Linux, apart from the shell. Its power and brevity take on particular value in automation contexts.
For example, assume that /usr/local/bin/modified_directories.pl contains this code:
#!/usr/bin/perl
# Usage: "modified_directories.pl DIR1 DIR2 ... DIRN"
# Output: a list of all directories in the file systems under
# DIR1 ... DIRN, collectively. They appear, sorted by the
# interval since their last activity, that is, since a file
# within them was last created, deleted, or renamed.
# Randal Schwartz wrote a related program from which this is
# descended.
use File::Find;
@directory_list = @ARGV;
# "-M" abbreviates "time since last modification", while
# "-d" "... is a directory."
find ( sub {
$modification_lapse{ $File::Find::name} = -M if -d } ,
@directory_list );
for ( sort {
$modification_lapse{ $a} <=> $modification_lapse{ $b} } keys
%modification_lapse ) {
# Tabulate the results in nice columns.
printf "%5d: %s\n", $modification_lapse{ $_} , $_;
}
Also assume that you adjoin an entry such as this to your crontab:
001 20 2 * * * /usr/local/bin/modified_directories.pl / 002
In this case, each morning you'll receive an email report on the date each directory on your host was last modified. This can be useful both for spotting security issues when read-only directories have been changed (they'll appear unexpectedly at the top of the list) and for identifying dormant domains in the filesystem (at the bottom of the list) that might be liberated for better uses.
Other Tools
Many other general-purpose scripting languages effectively automate operations. Apart from Perl and tcl, Python deserves the most attention for several reasons, such as its portability and extensibility.
The next sections describe Python and two other special-purpose tools important in automation: Emacs and procmail.
Python
Python can be of special interest to Red Hat Linux users. Python is object-oriented, modern, clean, portable, and particularly easy to maintain. If you are a full-time system administrator looking for a scripting language that will grow with you, consider Python. The official home page for Python is http://www.python.org.
Emacs
Emacs is one of the most polarizing lightning rods for religious controversy among computer users. Emacs has many intelligent and zealous users who believe it to be the ideal platform for all automation efforts. Its devotees have developed what was originally a screen editor into a tool with capabilities to manage newsgroup discussion, Web browsing, application development, general-purpose scripting, and much more. For the purposes of this chapter, what you need to know about Emacs follows:
- It's an editor that you ought to try at some point in your career.
- If you favor integrated development environments, Emacs can do almost anything you imagine. As an editor, it emulates any other editor, and its developers ensure that it always offers state-of-the-art capabilities in language-directed formatting, application integration, and development automation.
Even if the "weight" of Emacs sways you against its daily use (it may seem slow on startup and can require quite a bit of education and configuration), keep it in mind as a paragon of how sophisticated programming makes common operations more efficient.
procmail
The most indispensable, most often used Internet function is email. Can email be automated?
Yes—and it's perhaps the single best return on your invested time to do so. Along with aliases, distribution lists, startup configurations, and the plethora of mail agents or clients with their feature sets, you'll want to learn about procmail. Suppose you receive a hundred messages a day, that a fifth of them can be handled completely automatically, and that it takes at least three seconds of your time to process a single piece of email; those are conservative estimates. A bit of procmail automation will save you at least a minute a day, or six hours a year. Even conservative estimates make it clear that an hour of setting up procmail pays for itself many times over.
Along with the man procmail* pages ( man -k procmail for a list), serious study of procmail starts with the page http://www.faqs.org/faqs/mail/filtering-faq, Nancy McGough's Filtering Mail FAQ. This gives detailed installation and debugging directions. You'll also find information about procmail in the Mail-HOWTO (if you installed the HOWTOs). Because your Red Hat Linux machine will almost certainly have a correctly configured procmail, you can immediately begin to program your personal use of it. As a first experiment, create exactly these files.
~/.procmailrc, with these contents:
VERBOSE=on MAILDIR=$HOME/mail PMDIR=$HOME/.procmail LOGFILE=$PMDIR/log INCLUDERC=$PMDIR/rc.testing
~/.procmail/rc.testing, holding this code:
:0: * ^Subject:.*HOT SPAM.HOT # chmod 600 ~/. ~/.procmailrc ~/procmail/rc.testing # chmod a+x ~/.
Now exercise your filter with the following:
# echo "This message 1." | mail -s "Example of HOT SPAM." YOUR_EMAIL_NAME # echo "This message 2." | mail -s "Desired message." YOUR_EMAIL_NAME
What you now see in your mailbox is only one new item: the one with the subject Desired message. You also have a new file in your home directory, SPAM.HOT, holding the first message.
procmail is a robust, flexible utility you can program to achieve even more useful automations than this. When you gain familiarity with it, it will become natural to construct rules that, for example, automatically discard obvious spam, sort incoming mailing-list traffic, and perhaps even implement pager forwarding, remote system monitoring, or FAQ responding. This can save you considerable time each day.
Internal Scripts
One more element of the automation attitude is to be on the lookout for opportunities within every application you use. Scripting has become a pervasive theme, and almost all common applications have at least a rudimentary macro or scripting capability. IRC users know about bots, Web browsers typically expose at least a couple of scripting interfaces, all modern PPP clients are scriptable, and even such venerable tools as vi and ftp have configuration, shortcut, and macro capabilities that enormously magnify productivity. Even the apm and cardmgr (for PCMCIA cards) will run scripts on certain actions). If you use a tool regularly, take a few minutes to reread its presentation in this volume; chances are you'll come up with a way to make your work easier and more effective.
Concluding Challenge for an Automater Explaining Value | Next Section

Account Sign In
View your cart