Use scp to copy a file to different servers
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
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.