9

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/ ; } 
4
  • Do you have expect on the system ? Commented May 15, 2012 at 21:02
  • @lain Yes, expect is installed. Commented May 15, 2012 at 21:07
  • Anyone knows why everyone else is ignoring scp man page specifies that you can have more than one target? scp file host1:path host2:path host3:path ... Commented Jun 13, 2022 at 12:38
  • Oh because last one is not the target... Commented Jun 13, 2022 at 13:20

4 Answers 4

7

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 
3
  • pssh is not installed in the server and I can't run applications that weren't provided by the sysadmin team. Commented May 15, 2012 at 21:00
  • @RubenPloneda, but it does sound like you have a valid reason for that tool. You should at least consider it, and forward your request to your sysadmin team to get that installed. Commented May 15, 2012 at 21:01
  • Then you'll have to use SSH agent forwarding and a bash loop (see answer). Commented May 15, 2012 at 21:05
4

Try doing this with an expect script e.g.

#!/bin/bash HOSTS="h1.lan h2.lan h3.lan" read -p "Password: " PASSWORD for HOST in $HOSTS do expect -c " spawn /usr/bin/scp file user@$HOST:/destination_path/ expect { "*password:*" { send $PASSWORD\r;interact } } exit " done 

The above should be fairly straight forward to adapt to your requirements.

2
  • It results in ... line 10: expect: command not found Commented Oct 3, 2016 at 14:01
  • My fault. If you don't have expect installed, install it first with sudo apt-get install expect Commented Oct 3, 2016 at 14:08
1

I think sshpass should be mentioned here. This is my go on sending a file to multiple targets, with a required password:

filePath="/home/download/textToSend.txt" ip_range=("10.10.10.1" "10.10.10.2" "10.10.10.3" "10.10.10.4") # Start file transfer echo "Starting file transfer to ${#ip_range[@]} units." for ((i=0; i<${#ip_range[@]}; ++i )) ; do echo "Transfering ${filePath} to ${ip_range[$i]}..." sshpass -p password scp -o 'StrictHostKeyChecking=no' "${filePath}" root@${ip_range[$i]}:/home/downloads& done wait echo "File transfers ended" 
0

I used 'sshpt' for a similar situation. The SSH Power Tool (sshpt) enables you to execute commands and upload files to many servers simultaneously via SSH without using pre-shared keys. Uploaded files and commands can be executed directly or via sudo.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.