Automate scp between servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automate scp between servers
# 1  
Old 05-02-2013
Linux 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
# 2  
Old 05-02-2013
try this one!
Code:
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!
This User Gave Thanks to Revansidhu For This Post:
# 3  
Old 05-02-2013
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.
This User Gave Thanks to palsevlohit_123 For This Post:
# 4  
Old 05-02-2013
Quote:
Originally Posted by Revansidhu
try this one!
Code:
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!
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.

Code:
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.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-07-2013
Worked perfectly

Quote:
Originally Posted by Corona688
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.

Code:
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.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. Shell Programming and Scripting

scp without password between two servers

Hello Folks, I have two linux server accounts server1 and server2 From the terminal if I say this command, scp /source/folder/from/server1/unix.txt user@server2.com:/destination/folder/ Then it prompts for the password user@server2.com's password: I enter my password and then it... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

3. Shell Programming and Scripting

automate scp faster

Hi, I am currently using a shell script to transfer file to another machine non-stop using scp . below is my code : #!/bin/bash while : do scp /home/pc3/Desktop/b.html pc1@192.168.1.102:/home/pc1/Documents done After it transfer the first image, it needs 5 seconds to send the second... (9 Replies)
Discussion started by: Ericyue
9 Replies

4. UNIX for Advanced & Expert Users

Script that can Automate Printer adding task in HP-UX servers

I want to make a script to automate printer adding task.My inputs are like Printer name : xyz Port number :9001 I should write a script to make the Printer adding task will be automated. Like in manually adding task we are doing through hppi or jetadmin tools. ---------- Post updated at... (2 Replies)
Discussion started by: AnilKPatnaik
2 Replies

5. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: rdlover
7 Replies

6. Shell Programming and Scripting

Scp between two servers

Is there a way to connect betwwen two servers A and B without using prompting for a password ..... I am writing a script which includes picking up files from and puts them to B. using mget .. To make the script fully automable I am looking for a passwordless authentication... Any... (2 Replies)
Discussion started by: ultimatix
2 Replies

7. UNIX for Dummies Questions & Answers

Unable to scp/sftp between two servers

I have four servers that for all intents and purposes are the same (I have the same profile on all four), North, South, Brooklyn & Queens. I have a script that scp's a file from Queens to brooklyn, and it runs just fine. I tried to replicate the script on South, to transfer a file to North, and... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

8. Shell Programming and Scripting

Automate FTP

Hi, Currently, i am using sftp manully to transfer files between two secure servers. Can anyone provide me a sample shell script which can automate the sftp process? (11 Replies)
Discussion started by: borncrazy
11 Replies

9. Shell Programming and Scripting

scp between 2 servers - invoked at 3rd server

I have a couple of servers that can't see each other and need to copy files from one to the other. I try to invoke scp from a 3rd server that can see both servers - get error msgs that are cryptic. from server C I can do scp user@serverA:~/file . scp file user@serverB:~ but if I try to... (2 Replies)
Discussion started by: bigjohn-nj
2 Replies

10. Shell Programming and Scripting

automate useradd

Hi I wounder if some one knows how to make a script to automate user adding? When i am starting the script it will add user like 04pers00 and fowllowing untill that i'm quit. The password will be a standard password typed in the file. I'm unsing Sun solaris 9 on Intel Sorry for my... (1 Reply)
Discussion started by: steffa
1 Replies
Login or Register to Ask a Question