paint-brush
3 Strategies to Send Files to Multiple Linux Hosts in Emergencies by@levorak
579 reads
579 reads

3 Strategies to Send Files to Multiple Linux Hosts in Emergencies

by Carolina
Carolina HackerNoon profile picture

Carolina

@levorak

Colombian Engineer Tech enthusiast AI|BI|Cybersecurity|Dev|Opensource

October 5th, 2020
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

3 Strategies to Send Files to Multiple Linux Hosts in Emergencies: In emergencies you need to send a file to multiple hosts and you can not do it manually. Below I talk you through three strategies you can use to send files in emergencies. From the server send the file to each host using SCP and expect to avoid entering the password many times. From each host download the file from the server using rsync. Then copy the content of the file_to_recover in the host and recover it.
featured image - 3 Strategies to Send Files to Multiple Linux Hosts in Emergencies
Carolina HackerNoon profile picture
Carolina

Carolina

@levorak

Colombian Engineer Tech enthusiast AI|BI|Cybersecurity|Dev|Opensource

About @levorak
LEARN MORE ABOUT @LEVORAK'S
EXPERTISE AND PLACE ON THE INTERNET.

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 -





L O A D I N G
. . . comments & more!

About Author

Carolina HackerNoon profile picture
Carolina@levorak
Colombian Engineer Tech enthusiast AI|BI|Cybersecurity|Dev|Opensource

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Moomoo
Leftic
Hashnode
Learnrepo
X REMOVE AD