This one was easy, first create a file (hosts_ip.txt) with the IP of each server to get its /etc/hosts:
192.168.0.3 192.168.0.4 192.168.0.5 192.168.0.6 192.168.0.7
Generate a private/public pair key and add the public one in authorized_keys of each server :
# generate public/private key ssh-keygen -t rsa -b 2048 #enable public key in server cat mykeys.pub >> ~/.ssh/authorized_keys
Copy the private keys to a common directory, in this case «/home/mortiz/get_hosts/pass_<server_ip>» and with some shell the magic happens:
#!/usr/bin/bash #m.ortiz.montealegre # Lee el archivo de IP's y descarga de cada equipo /etc/hosts cat /home/mortiz/get_hosts/hosts_ip.txt | while read -r g; do scp -q -i /home/mortiz/get_hosts/pass_$g <username>@$g:/etc/hosts /cygdrive/g/resources/etc_hosts/$(date +%Y-%m-%d)_hosts_$g done
That’s it.