-->

Previous | Table of Contents | Next

Page 447

almost through packing for the vacation you're starting tonight. How do you do right by your subscribers? It only takes three lines at scripting:


at 17:00 Friday << COMMAND

     mail -s "This week's CYCLES report." mailing_list < analysis.already_written

COMMAND

This schedules the mail command for later processing. You can log off from your session, and your Linux host will still send out the mail at 17:00 Friday, just as you instructed. In fact, you can even shut down your machine after commanding it at ..., and, as long as it's rebooted in time, your scheduled task will still be launched on the schedule you dictated.

Other Mechanisms: Expect, Perl, and More

Are you ready to move beyond the constraints of the UNIX shell? There are several alternative technologies that 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.

Comparing Technologies

I'm often asked to compare different technologies for automation; as a service to readers, I've launched the page http://starbase.neosoft.com/~claird/comp.lang.misc/portable_ scripting.html, which answers questions about choosing among different scripting languages. The most important principles are as follows:

With few exceptions, the capabilities of different languages are close enough that the social and psychological factors dominate.

Expect

Expect "is a must-know tool for system administrators and many others," according to a user testimonial that appears on the back cover of Exploring Expect, its standard reference. 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 Expect, though. After you install Expect (http://starbase.neosoft.com/~claird/comp.lang.tcl/expect.html ), create a script hold with the contents of Listing 22.4.

Page 448

Listing 22.4. hold—a "keep-alive" written in Expect.


#!/usr/local/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

I work with several telephone lines that are used with short time-outs, as a check on out-of-pocket expenses. I use a variant of the script in Listing 22.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 all the tcl capabilities that Chapter 25, "tcl and tk Programming," presents. For a perspective on tcl that emphasizes the automation themes of this chapter, see the pages http:/starbase.neosoft.com/~claird/comp.lang.tcl/tcl.html and http://starbase.neosoft.com/~claird/comp.lang.tcl/ expect.html.

Page 449

Perl

Chapter 24, "Perl Programming," presents Perl as the most popular scripting language for Red Hat Linux, apart from the shell. Its power and brevity take on particular value in automation contexts, as the page http://starbase.neosoft.com/~claird/comp.lang.perl.misc/

perl.html emphasizes. For example, if /usr/local/bin/modified_directories.pl contains


#!/usr/local/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);

foreach (sort{$modification_lapse{$a} <=> $modification_lapse{$b}} keys %size) {

          # Tabulate the results in nice columns.

     printf "%5d:  %s\n", $modification_lapse{$_}, $_;

}

and you adjoin an entry such as


20 2 * * * /usr/local/bin/modified_directories.pl /

to your crontab, then each morning you'll receive an e-mail 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. As of fall 1997, Java (see the page http://starbase.neosoft.com/~claird/comp.lang.java/java.html ) is not such a language; its support of Linux is too immature, and it's too "heavy" for automation projects. This is likely to change in the future. Until then, put your energies into other projects, or realize that you're working at "the bleeding edge," that is, with a technology that is more educational than it is directly useful.

Several special-purpose tools are also important in automation, such as Python, Emacs, procmail, and calendar.

Previous | Table of Contents | Next