2012年3月15日星期四

Distribute files to multiple servers via scp

The most common task when operating the servers is to distribute a file to multiple servers.

So I wrote a piece of shell script to solve this problem:

  1. #!/bin/bash  
  2. echo "mscp <source file> <target dir>"  
  3. SourceFile=$1  
  4. TargetDir=$2  
  5. echo "Copy $SourceFile to $TargetDir as $RemoteUser"  
  6. echo "Enter the servers:"  
  7. if [ -f $SourceFile ]  
  8. then  
  9.   printf "File found, preparing to transfer\n"  
  10.   while read server  
  11.   do  
  12.   scp -p $SourceFile ${server}:$TargetDir  
  13.   done  
  14. else  
  15.   printf "File \"$SourceFile\" not found\n"  
  16.   exit 1  
  17. fi  
  18. exit 0  

 

call the script "mscp <source file> <target dir>", then the script will ask you the list of target servers. So you can type them one by one. If the remote user is different than you current user, you can also explicitly identify it by typeing user@server

 

Beside the previous scenario, there is a more common sceanrio, that you have got a server list stored in afile already. Then instead of type the servers line by line, you can pipe the file content to the script.

e.g: cat server_list.txt > mscp src_files dest_path

Best regards,

TimNew
-----------
If not now then when?
If not me then who?

Release your passion
To realize your potential

Sent with Sparrow

Posted via email from 米良的实验室

没有评论:

发表评论