0447-0449.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 320 行
HTML
320 行
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Automating Tasks</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=22 //-->
<!-- PAGES=0437-0454 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0444-0446.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0450-0452.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-447"><P>Page 447</P></A>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
at 17:00 Friday << COMMAND
mail -s "This week's CYCLES report." mailing_list < analysis.already_written
COMMAND
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<H3><A NAME="ch22_ 20">
Other Mechanisms: Expect, Perl, and More
</A></H3>
<P>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.
</P>
<H4><A NAME="ch22_ 21">
Comparing Technologies
</A></H4>
<P>I'm often asked to compare different technologies for automation; as a service to readers,
I've launched the page <a href="http://starbase.neosoft.com/~claird/comp.lang.misc/portable_">http://starbase.neosoft.com/~claird/comp.lang.misc/portable_</A>
scripting.html, which answers questions about choosing among different scripting
languages. The most important principles are as follows:
</P>
<UL>
<LI> Choose a language that your friends (acquaintances, coworkers, correspondents,
and so on) use.
<LI> Choose a language that feels good to you.
</UL>
<P>With few exceptions, the capabilities of different languages are close enough that the social
and psychological factors dominate.
</P>
<H4><A NAME="ch22_ 22">
Expect
</A></H4>
<P>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 <a href="(http://starbase.neosoft.com/~claird/comp.lang.tcl/expect.html">(http://starbase.neosoft.com/~claird/comp.lang.tcl/expect.html</A>
), create a script hold with the contents of Listing 22.4.
</P>
<A NAME="PAGENUM-448"><P>Page 448</P></A>
<P>Listing 22.4. hold—a "keep-alive" written in Expect.
</P>
<!-- CODE //-->
<PRE>
#!/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
</PRE>
<!-- END CODE //-->
<P>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.
</P>
<P>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
<a href="http:/starbase.neosoft.com/~claird/comp.lang.tcl/tcl.html">http:/starbase.neosoft.com/~claird/comp.lang.tcl/tcl.html</A>
and
<a href="http://starbase.neosoft.com/~claird/comp.lang.tcl/">http://starbase.neosoft.com/~claird/comp.lang.tcl/</A>
expect.html.
</P>
<A NAME="PAGENUM-449"><P>Page 449</P></A>
<H4><A NAME="ch22_ 23">
Perl
</A></H4>
<P>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 <a href="http://starbase.neosoft.com/~claird/comp.lang.perl.misc/">http://starbase.neosoft.com/~claird/comp.lang.perl.misc/</A>
</P>
<P>perl.html emphasizes. For example, if
/usr/local/bin/modified_directories.pl contains
</P>
<!-- CODE //-->
<PRE>
#!/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{$_}, $_;
}
</PRE>
<!-- END CODE //-->
<P>and you adjoin an entry such as
</P>
<!-- CODE SNIP //-->
<PRE>
20 2 * * * /usr/local/bin/modified_directories.pl /
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<H4><A NAME="ch22_ 24">
Other Tools
</A></H4>
<P>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
<a href="http://starbase.neosoft.com/~claird/comp.lang.java/java.html">http://starbase.neosoft.com/~claird/comp.lang.java/java.html</A>
) 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.
</P>
<P>Several special-purpose tools are also important in automation, such as Python, Emacs,
procmail, and calendar.
</P>
<P><CENTER>
<a href="0444-0446.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0450-0452.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?