Same entry in multiple server at once in aix

Hi,

I need to update /etc/hosts file with same entry in 20 servers. Is there any script or command that can help me to put this entry in all servers at once instead of going to each server one by one. thanks

Use the search function of the forum. Enter the following strings to get many results that might give you a start or even a near complete solution:
"multiple servers remote ssh"

It's very simple. Create a file called servers with the server names in each line and run this:

cat servers | while read srv; do ssh user_name@$srv "mv /etc/hosts /etc/hosts.bak"; scp /etc/hosts user_name@$srv:/etc; done;

You may want to think about configuring DNS or NIS servers to avoid updating the hosts file for any addition or removal of machines in your network.

thank you admin.
What I understand from your post that it will copy host file to each server but I dont want that. I want to make an entry in each host file, I want to edit each host file and add server in the host file. This server entry has to be added in multiple servers. Your post definitely gave me an idea how to do it. I am new to aix.

You can use a similar command but instead of the mv / scp you can just echo the desired entry and double redirect >> it to add it to the end of each hosts host file.

Glad to be of assistance. Well this has nothing to do with AIX specifically. This applies to any UN*X system as far as you have bash or ksh shell. For other shells, the syntax may change.

This should do what you want:

cat servers | while read srv; do ssh user_name@$srv 'echo "10.0.1.1  server_name" >>/etc/hosts'; done

If you have multiple entries to add to, you can make use of another additional loop inside the while loop. I guess you get the idea! :slight_smile:

I appreciated guys thanks

don't know why, but a while loop with ssh doesn't work for me, the loop quits after a certain time, so I always use a for loop instead of while. maybe depending on the ssh return code

for server in $(cat /yourfile) 
do
ssh ${server} "yourcommand"
done

@funksen
If the loop stops after the 1st cycle, you can try with the ssh option -n .

1 Like