
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
138
|
Chapter 4: Secure Remote Administration
Using SSH to Execute Remote Commands
Now it’s time to take a step back from all this PK voodoo to discuss a simple feature
of SSH that is especially important for scripting: remote commands. So far we’ve
been using the command ssh strictly for remote shell sessions. However, this is
merely its default behavior; if we invoke ssh with a command line as its last argu-
ment(s), SSH will execute that command line rather than a shell on the remote host.
For example, suppose I want to take a quick peek at my remote system’s log (see
Example 4-7).
In Example 4-7, the host zippy will send back the contents of its /var/log/messages file
to my local console. (Note that output has been piped to a local
more process.)
Two caveats are in order here. First, running remote commands that require subse-
quent user interaction is tricky and should be avoided—with the exception of shells,
ssh works best when triggering processes that don’t require user input. Also, all
authentication rules still apply: if you would normally be prompted for a password
or passphrase, you still will. Therefore, if using SSH from a cron job or in other non-
interactive contexts, make sure you’re either using a passphrase-less key or that the
key you are using is first loaded into