1

I need to copy a file for two different severs using shell script. I tried to use Scp usernameip adress and destination path of the folder

sindhu
  • 13
  • How did you type the command? this should work if the servers run a ssh daemon. To let it work automatically you should make use of ssh keys. – switch87 Nov 24 '15 at 15:59

2 Answers2

5

As described here you could write a little shell script:

for dest in $(<destinations.txt); do
  scp /path/to/the/file_to_copy.txt ${dest}:/remote/path/
done

You just need to create destinations.txt file where you will fill in each line the destination user@host , something as follow eg.:

root@192.168.1.65
phphil@unix.stackexchange.com

Note that the destination path (/remote/path/) is hardcoded in the script, it means that this path must exists in both servers. If you prefer to set a dedicated path for each destination, you could edit the script, remove :/remote/path/ and set it for each of your entry in destinations.txt.

Otherwise you could give a look at parallel-scp

lese
  • 2,726
  • Hi lese, can you please elborte the syntax. – sindhu Nov 25 '15 at 04:52
  • Hi Sindhu, what does it mean? – lese Nov 25 '15 at 07:06
  • am new to shell script so you have mention like dest so in dest i need to provide the ip adrress or file name – sindhu Nov 25 '15 at 07:13
  • Oh sorry, I got it. The scp is just in its standard form, but the destination ip/name is a variable, filled by entries (content) of destfile.txt using a for cicle. So you don't have to edit "dest" variable, but you need to set the ip addresses of destination servers (or sure, the domain names). I did update the answer to make it clearer – lese Nov 25 '15 at 08:34
2

By using RSA you can make a script to send the files without it prompting for your password, your password does not have to be typed into the script. run on your computer:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id loginname@server1Ip
ssh-copy-id loginname@server2Ip

inside your script:

scp file loginname@server1IP
scp file loginname@server2IP

if your login and local user name are the same you can leave out the loginname@ part

switch87
  • 926