Script to SCP a file to multiple servers | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Script to SCP a file to multiple servers

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-06-2009
rdlover rdlover is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 29 April 2009, 12:11 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 02-06-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 28 August 2014, 10:17 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,227
Thanks: 121
Thanked 451 Times in 411 Posts
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.
Sponsored Links
    #3  
Old 02-06-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 26 August 2014, 3:35 AM EDT
Location: Pruthvi
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
@ rdlover,

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

eg:


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

HTH,

Regards,

Praveen
    #4  
Old 02-06-2009
avronius avronius is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 22 May 2013, 8:38 AM EDT
Location: Calgary
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
#/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

Sponsored Links
    #5  
Old 02-14-2009
rdlover rdlover is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 29 April 2009, 12:11 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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 ?
Sponsored Links
    #6  
Old 02-14-2009
sunny651 sunny651 is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 14 February 2009, 11:02 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
How about make the scp process running in the background? use the '&'

Like this,
scp -p $SOURCEFILE ${server}:$TARGETDIR &
Sponsored Links
    #7  
Old 02-14-2009
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
 
Join Date: Dec 2007
Last Activity: 21 August 2014, 4:30 PM EDT
Posts: 1,136
Thanks: 2
Thanked 88 Times in 85 Posts
Quote:
Originally Posted by rdlover View Post
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 ?
rdist has a parallel option. rysnc may work well to but not sure off hand if it can run in parallel.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to change passwords for the same user on multiple servers stolz Shell Programming and Scripting 7 02-17-2009 02:49 PM
rsh to change multiple ip in multiple servers? kenshinhimura Shell Programming and Scripting 2 02-18-2008 02:04 AM
Need a sheel script to compare no of file on diffrent servers ashahzad Shell Programming and Scripting 1 01-31-2008 08:12 AM
login into multiple servers thru script... avcert1998 Shell Programming and Scripting 1 09-09-2005 03:38 AM
Script to ping servers in a file gbarkhor AIX 1 07-05-2005 04:23 PM



All times are GMT -4. The time now is 02:41 PM.