Hello,
Thanks to all who replied. The info was helpful. I decided the task was
too cumbersome to always type from the command line, and also
straightforward enough to write some scripts. So I wrote two scripts: dssh
(to issue commands) and dscp (to copy files) to multiple Linux systems.
First you need a file with a list of target systems in /etc/:
# cat /etc/lnxs
ntc201
ntc202
ntc203
ntc204
ntc205
ntc206
ntc207
ntc208
Here's a test of copying the two files to multiple systems. Note the two
files in the current working directory:
# ls
dscp* dssh*
Here's an example of the dscp command (copy to the systems in /etc/lnxs
all files in current directory to the target directory /usr/local/sbin):
# dscp lnxs * /usr/local/sbin
==========================================
Performing command scp dscp dssh ntc201:/usr/local/sbin
------------------------------------------
dscp 100% 1414 1.4KB/s 00:00
dssh 100% 1027 1.0KB/s 00:00
------------------------------------------
return code = 0
==========================================
Performing command scp dscp dssh ntc202:/usr/local/sbin
------------------------------------------
dscp 100% 1414 1.4KB/s 00:00
dssh 100% 1027 1.0KB/s 00:00
------------------------------------------
return code = 0
... (5 more times)
==========================================
Performing command scp dscp dssh ntc208:/usr/local/sbin
------------------------------------------
dscp 100% 1414 1.4KB/s 00:00
dssh 100% 1027 1.0KB/s 00:00
------------------------------------------
return code = 0
Here's the dssh command (on systems in /etc/lnxs perform the command
ls/usr/local/sbin):
# dssh lnxs ls /usr/local/sbin
==========================================
Performing command ls /usr/local/sbin on ntc201:
------------------------------------------
dscp
dssh
------------------------------------------
return code = 0
... (7 more times)
Here's the source to dssh:
#!/bin/bash
#+--------------------------------------------------------------------------+
function help()
# give help
#+--------------------------------------------------------------------------+
{
echo "Usage: dssh target-hosts-file <command ...> "
echo ""
echo "Perform <command> on hosts specified in /etc/target-hosts-file"
echo ""
exit 1
}
if [[ $# < 2 ]]; then # user did not pass at least two arguments
echo ""
echo "Error: at least two arguments are needed"
help
fi
hostsFile="/etc/$1"
shift # peel of hosts file - remaining args are command
command="$@"
if [ ! -f $hostsFile ]; then
echo ""
echo "Error: list of hosts must be found in $hostsFile"
help
fi
for host in $(cat $hostsFile); do
echo "=========================================="
echo "Performing command $command on $host:"
echo "------------------------------------------"
ssh $host $command
returnCode=$?
echo "------------------------------------------"
echo "return code = $returnCode"
done
Here's the source to dscp:
#!/bin/bash
#+--------------------------------------------------------------------------+
function help()
# give help
#+--------------------------------------------------------------------------+
{
echo "Usage: dscp target-hosts-file source1 <source2 ...> target"
echo ""
echo "Perform scp of source(s) to hosts specified in
/etc/target-hosts-file"
echo ""
exit 1
}
if [[ $# < 3 ]]; then # user did not pass at least two arguments
echo ""
echo "Error: at least three arguments are needed"
help
fi
hostsFile="/etc/$1"
if [ ! -f $hostsFile ]; then
echo ""
echo "Error: list of hosts must be found in $hostsFile"
help
fi
shift # peel off hosts file
sources="$1" # get first source file
shift # peel off first source file
while [[ $# > 1 ]]; do # iterate while more than 1 arg left
sources="$sources $1" # append to list of source files
shift # shift argument list
done
target=$@
for host in $(cat $hostsFile); do # iterate through hosts
echo "=========================================="
echo "Performing command scp $sources $host:$target"
echo "------------------------------------------"
scp $sources $host:$target
returnCode=$?
echo "------------------------------------------"
echo "return code = $returnCode"
done
The code is lightly tested, almost certainly has bugs, no warranties, no
license, bug reports appreciated, and note that I refrained from using
back ticks even though I would not consider nesting them :))
"Mike MacIsaac" <[EMAIL PROTECTED]> (845) 433-7061
----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390