James Stephens

January 16, 2006

Securely Copying A File To Multiple Machines With Secure Copy (SCP)

Filed under: Unix Scripts — James Stephens @ 2:20 pm

A bash script is provided for copying a file to multiple machines via Secure Copy (SCP).

I often find when doing system administration that I need to copy the same file to a whole bunch of machines so I wrote a bash script to achieve this. You may need to setup SSH keys first to avoid having to type in the password on every machine.

Here is the script. I hope you find it useful.

#!/bin/bash
#Default list of hosts to use
INFILE=/home/sysadmin/linux/bin/linux_hosts

#Directory where we keep the file to be copied
FILEDIR=/home/sysadmin/linux/tmp

#The default output file
OUTFILE=${0}_output

ECHO=/bin/echo
SCP=/usr/bin/scp
SSH=/usr/bin/ssh
CAT=/bin/cat
RM=/bin/rm
TOUCH=/bin/touch
TEE=/usr/bin/tee

if [ -z "$*" ]; then
${ECHO}
${ECHO} “$0 — a simple bash script for copying a file to”
${ECHO} “multiple machines via scp.”
${ECHO}
${ECHO} “J.C.Stephens jns@ias.edu”
${ECHO} “http://www.sns.ias.edu/~jns”
${ECHO}
${ECHO} “Usage: $0 remotepathtofile/file”
${ECHO}
${ECHO} “Note: Always looks in $FILEDIR for the file to be copied”
${ECHO}
${ECHO} ” [-f filename] Use list of hosts contained in filename”
${ECHO} ” instead of ${INFILE}”
${ECHO} ” [-r filename] Direct the output to filename+screen”
exit $?
fi

while getopts “:f:r:” opt; do
case $opt in
f ) INFILE=${OPTARG};;
r ) OUTFILE=${OPTARG};;
\? ) ${ECHO} “Illegal option -${OPTARG}”
exit 1
esac
done
shift $((${OPTIND} - 1))

${ECHO} “Using hostnames from ${INFILE}”
${ECHO}
HOSTS=`${CAT} ${INFILE}`

if [ -f ${OUTFILE} ]; then
${RM} ${OUTFILE}
${TOUCH} ${OUTFILE}
fi

for host in ${HOSTS}; do

echo $HOST

# Find the permissions of the remote file first …. we want to copy these if it exists
RCOMMD=”stat $1 | sed -e “1,3d” | sed -e “2,5d” | sed -e “s/\//(/g” | cut -d”(” -f2″
${SSH} -t -q -l root ${host} stat $1 >>/dev/null 2>&1 && PERMS=`${SSH} -l root ${host} ${RCOMMD}`

if [ -z $PERMS ]; then
${ECHO}
${ECHO} “Please enter the permissions for the new file, e.g. 4777″
#read PERMS
PERMS=700
fi

${ECHO} | ${TEE} -a ${OUTFILE}
${ECHO} “${host}: $1″ 2>&1 | ${TEE} -a ${OUTFILE}
${ECHO} “Before:” | ${TEE} -a ${OUTFILE}

${SSH} -t -q -l root ${host} ls -l $1 2>&1 | ${TEE} -a ${OUTFILE}
${SCP} $FILEDIR/${1##/*/} root@${host}:$1 2>&1 | ${TEE} -a ${OUTFILE}
${SSH} -t -q -l root ${host} chmod ${PERMS} $1 2>&1 | ${TEE} -a ${OUTFILE}
${ECHO} “After:” | ${TEE} -a ${OUTFILE}
${SSH} -t -q -l root ${host} ls -l $1 2>&1 | ${TEE} -a ${OUTFILE}
${ECHO} 2>&1 | ${TEE} -a ${OUTFILE}
done

$ECHO
$ECHO

chmod 664 ${OUTFILE}

exit $?

2 Comments »

  1. Hello James,
    I am trying to run the shell provided by you and I get into a loop provided below :

    bash-2.05$ ./sh1.sh test/test_14.txt
    Using hostnames from hosts

    btnbtw2
    siebel@157.227.35.13’s password:

    Please enter the permissions for the new file, e.g. 4777

    157.227.35.13: test/test_14.txt
    Before:
    siebel@157.227.35.13’s password:
    -rw-r–r– 1 siebel other 22 Dec 20 19:32 test/test_14.txt
    siebel@157.227.35.13’s password:
    /soft/siebel/test/test/test_14.txt: No such file or directory
    siebel@157.227.35.13’s password:

    I have changed the shell to below :

    INFILE=hosts
    FILEDIR=/soft/siebel/test

    Comment by Ravi — December 20, 2006 @ 9:01 am

  2. This is exactly what I expected to find out after reading the title Securely Copying A File To Multiple Machines With Secure Copy (SCP). Thanks for informative article

    Comment by Eric — June 18, 2007 @ 7:30 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress