rhl6u253

Lemur zaprasza

Red Hat® Linux 6 Unleashed










Chapter 27: Automating Tasks





Previous
ChapterNext
Chapter










Sections in this Chapter:








First
Example--Automating Data Entry





Other Mechanisms: Expect, Perl,
and More






Tips for
Improving Automation Technique





Concluding
Challenge for an Automater--Explaining Value






Shell
Scripts











Scheduling
Tasks with cron and at Jobs










 

Previous
SectionNext
Section





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
6.0, 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
27.4.



Listing 27.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 » 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
timeouts, as a check on out-of-pocket expenses. I use a variant of the script
in Listing 27.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 33, "tcl and
tk Programming," presents. For information about tcl and tk from its
author, Dr. John Ousterhout, visit .
For more information about Expect, visit .



Tip - You'll also find
the autoexpect command included with Red Hat
Linux. This command watches an interactive session at the console and
then creates an executable program to execute the console session. See
for an example of how to use autoexpect
to automate an FTP session.



Perl
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.



Note - For more information
about Perl, or to get the latest release, browse
or .


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:



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

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. See
for
more information. The official home page for Python is .



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 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:



    l

    It's an editor that you ought to try at some point in
    your career.

    l

    l

    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.

    l


Even if the "weight" of Emacs (it may seem slow on startup
and can require quite a bit of education and configuration) sways you against
its daily use, keep it in mind as a paragon of how sophisticated programming
makes common operations more efficient.


Note -
The Emacs editor is included with Red Hat Linux. You can use Emacs with or
without the X Window system. Type the word
emacs on the command line of your console or
an X11 terminal window and press the Enter key. Run its built-in tutorial by
pressing Ctrl+H and then pressing the T key.


procmail
Computer use has
exploded
in the Internet era. 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,
serious study of procmail starts with the page ,
Nancy McGough's Filtering Mail FAQ. This gives detailed installation and
debugging directions. You'll also find information about procmail in the
Mail-HOWTO, found under the /usr/doc/HOWTO directory. 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

~/.forward, with this:



"|IFS=' ' && exec /usr/bin/procmail -f || exec 75 #YOUR_EMAIL_NAME"

After you create these three, set necessary permissions with the
following:



# chmod 644 ~/.forward
# 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. 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.





Red Hat® Linux 6 Unleashed










Chapter 27: Automating Tasks





Previous
ChapterNext
Chapter










Sections in this Chapter:








First
Example--Automating Data Entry





Other Mechanisms: Expect, Perl,
and More






Tips for
Improving Automation Technique





Concluding
Challenge for an Automater--Explaining Value






Shell
Scripts











Scheduling
Tasks with cron and at Jobs










 

Previous
SectionNext
Section





© Copyright Macmillan USA. All rights reserved.

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • teen-mushing.xlx.pl
  • Wątki
    Powered by wordpress | Theme: simpletex | © Lemur zaprasza