Let's suppose you need to send a file to multiple hosts and you can not do it manually and you need to do it now. Below I talk you through three strategies you can use to send files in emergencies.
SCP + EXPECT
From the server send the file to each host using SCP and expect to avoid entering the password many times
#!/bin/bash
USER="user"
PASS="passwd"
DEST_PATH="/home/user"
list="192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
"
for i in $list;
do
VAR=$(expect -c "
spawn scp -oStrictHostKeyChecking=no -oCheckHostIP=no file.txt $USER@$i:$DEST_PATH
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "$VAR" #to see remote command answer
done
rsycn + EXPECT
From each host download the file from the server using rsync and expect to avoid entering the password many times
#!/bin/bash
USER="user"
PASS="passwd"
DEST_PATH="/home/user"
list="192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
"
for i in $list;
do
VAR=$(expect -c "
spawn rsync -e \"ssh -o StrictHostKeyChecking=no\" file.txt $USER@$i:$DEST_PATH
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "$VAR"
done
Parallel-scp
Use the command parallel-scp to sent the file from the server to each host (hosts list on the ips.txt file one per line)
parallel-scp "-O StrictHostKeyChecking=no" --askpass -lremote_host_user -h ips.txt file.txt /remote/host/dest/path
Bonus Tip
If you lose ssh connection to a server but you still have session active this trick could help you. ;)
Generate the file
#For files
base64 file_to_copy > file_to_recover
#For directories
base64 directory_to_copy.tar.gz > file_to_recover
#For RPMs
tar cfz - $(rpm -ql ssh) | base64 > file_to_recover
Then copy the content of the file_to_recover in the host and recover it with these commands
#For files
base64 -d file_to_recover
#For directories
base64 -d file_to_recover | tar xfz
#For RPM
base64 -d < file_to_recover | tar xfz -