Linux – Use scp to copy a file to different servers

file-transferlinuxscp

I have to copy a file to different servers almost every day. What I usually do is:

scp filename user@destinationhost:/destination/folder

I run this same command changing the destination host over and over again until I finish all the servers. What is the best (and fastest) way to transfer the same file to those different servers?

Another drawback is that I need to enter the password over and over again, but using rsa is not an option since several people can connect to the source server.

Edit – I found loop in commandlinefu that may do the trick:

 for h in host1 host2 host3 host4 ; { scp file user@$h:/destination_path/ ; }

Best Answer

There are various tools which can scp files to multiple hosts (with simultaneous connections), like pssh and kanif. In terms of passwords I would suggest using agent forwarding. This allows you to keep the key on your local machine, but use it when initiating SSH connections from another host. Otherwise, the --askpass option to the parallel-scp command from pssh makes it prompt for a password to use for every host.

If you can't install a tool to do this, setup agent forwarding (by adding the -A option to ssh when connecting to the machine you're doing this on) and then run scp in a loop like so:

for HOST in server1 server2 server3; do
    scp somefile $HOST:~/somedir/
done
Related Topic