Script to SCP a file to multiple servers

Hi All,

I am a total noob to the Unix world, and i hope to learn a lot from this wonderful community. Here's my first post and question , i am trying to SCP a file to multiple servers (multiple destinations) through this little script :

#!/bin/ksh
# copy files

# File to be copied
FILE="/tmp/$1"
LOCATION="/tmp/"

check()

{
if [ -f $FILE ]

then

echo "File found,preparing to transfer"

else

echo "File not found "

exit 0

}

while read server; do

scp -p $FILE $server:$LOCATION

done < server.txt

The server.txt contains the list of destination servers, and $1 takes the 'filename' as a parameter, for whatever the reason, everytime i run the script i get bumped with an error :

./sctest[8]: 0403-057 Syntax error at line 24 : `}' is not expected.

I tried removing the braces, that didn't help either. Please advise.

You have to close the if/then with fi. Also use code-tags please for better readability and preserving of formats etc.
Your function "check" isn't called anywhere if I saw it right. Not sure if this is intended. When an error occurse like that the file isn't found, I would exit at least with 1, not with 0. 0 means everything is ok. So maybe change exit 0 vs exit 1 inside check() and put a exit 0 at the end of the script.

@ rdlover,

the if construct needs to be closed with a matching fi, which is missing in your code.

eg:

if [ expr ]
then
command 1
command 2
else
command 3
command 4
fi

HTH, :cool:

Regards,

Praveen

#/usr/bin/sh
# This is a script to copy files from one host to a group of hosts

# There are three variables accepted via commandline
# $1 = first parameter (/source_path/source_filename)
# $2 = second parameter (/target_directory/)
# $3 = third paramter (file that contains list of hosts)

SOURCEFILE=$1
TARGETDIR=$2
HOSTFILE=$3

if [ -f $SOURCEFILE ]
then
   printf "File found, preparing to transfer\n"
   while read server
   do
      scp -p $SOURCEFILE ${server}:$TARGETDIR
   done < $HOSTFILE
else
   printf "File \"$SOURCEFILE\" not found\n"
   exit 0
fi
exit 0

Thanks guys! That script works! But it kind of doesn't achieve what i had hoped it would. I am looking for a way to do multiple transfers in parallel to save time. That is, from a source to multiple destinations at the same time. Is this possible ?

How about make the scp process running in the background? use the '&'

Like this,
scp -p $SOURCEFILE ${server}:$TARGETDIR &

rdist has a parallel option. rysnc may work well to but not sure off hand if it can run in parallel.

Grazie Frank! I guess rdist is the only way to go if i have to transfer a file to multiple servers at the sametime. I'll think about it, although i am not so sure if my boss would wanna let me implement this on mission critical production servers. I guess manually scp'ing files to 15 different servers is the only way to go for the time being :frowning: