Automate scp between servers

Hello,

Could someone please help me with the below requirement?

I need to automate scp files between two servers. I have a file having server names and paths to the folder like below

server1 /path/to/folder/ server1 /path/to/folder/
server1 /path/to/folder/ server2 /path/to/folder/
server2 /path/to/folder/ server2 /path/to/folder/
server2/path/to/folder/ server1 /path/to/folder/
and likewise

The script should read the server name in first column and get the location which is second column and scp it to the other server/location which is third column to the path which is fourth column. I have enabled passwordless authentication between server and the server from which I will be executing the script.

Can someone please help and advise? Please let me know if you have any questions.

Cheers,
Kochappa

try this one!

lineNumber=1
lstMaxLine=`wc -l serverdetailsfile.txt | awk '{print $1}'`
while [ $lineNumber -le $lstMaxLine ]
	do
	S_server=`head -$lineNumber serverdetailsfile.txt | tail -1|awk '{print $1}'` 
	S_path=`head -$lineNumber serverdetailsfile.txt | tail -1|awk '{print $2}'`
	d_server=`head -$lineNumber serverdetailsfile.txt | tail -1|awk '{print $3}'` 
	d_path=`head -$lineNumber serverdetailsfile.txt | tail -1|awk '{print $4}'`
	
	lineNumber=`expr $lineNumber + 1`
	scp Username@$S_server:$S_path* Username@$d_server:$d_path.
	done

i think this will work!

1 Like

Only the scp script is not going to help.

If you do the automate (ssh-keygen) login, then scp will be automated/more useful.

How it going to help u?
how frequently this will be used?
what OS installed in source and dest servers?...

above things will be more useful to provide details.

1 Like

Running head 9 times, awk 9 times, and tail 9 times is silly when the shell can do it all at once with one shell builtin -- read.

while read -r s_server s_path d_server d_path
do
        echo scp "${s_server}:${s_path}" ${d_server}:${d_path}"
done < inputfile

Remove the echo once you're sure it does what you want.

1 Like

Thanks Corona, your script worked like a charm.. thanks again for making this simple for me.

Thanks to everyone who replied this and was trying to help.

Cheers,
Kochappa.