Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scripting hosts file rollout

Status
Not open for further replies.

themoog

Technical User
Sep 25, 2001
2
NL
I need a little help with scripting...

I want to rollout a master hosts file to a number of machines, the only thing is that /etc/hosts file on each machine is slightly different, ie the 5th line will have:
<host ip> <hostname> loghost

Can someone recommend a script that will keep the first 5 lines and append the master hostlist to the end of the /etc/hosts file ?

I guess awk should be used, however I've just started to learn it so am in need of some advice.

Cheers,
themoog
 
Most people I know of have gone away from the concept of a 'master host file'. Or of using a host file for anything other that local interfaces for that matter. Thats why the internet gods gave us wonderful tools like DNS, LDAP, and NIS (although NIS+ is actually a tool of the devil).

But if you insist on doing it anyway....

Do something like this:

#!/usr/bin/ksh
head -5 /etc/hosts > /tmp/hosts.tmp
cat newhosts >> /tmp/hosts.tmp
mv /tmp/hosts.tmp /etc/hosts
 
Cheers esserc.

yeah we are about to start using LDAP soon, but in the meantime have to stick with silly hosts files.

I should have remembered the head command and >>'s (doh)

whats the fastest way to roll this out now ? ftp to each box and run, or is there a way to script a secure copy and secure rsh ? to do it securely and automatically to all boxes at once ?

Thanks again
themoog
 
The best way might be to make the script to do the work and the master host file then send them both out to /tmp using rdist then use rsh to kick off the scripts.

Or you could do this:
---
#!/usr/bin/ksh
for host in `cat hosts.todo`
do
rsh ${host} &quot;head -5 /etc/inet/hosts&quot; > /tmp/hosts.tmp
cat newhosts >> /tmp/hosts.tmp
rsh ${host} &quot;chmod 644 /etc/inet/hosts&quot;
rcp /tmp/hosts.tmp ${host}:/etc/inet/hosts
rsh ${host} &quot;chmod 444 /etc/inet/hosts&quot;
done
rm /tmp/hosts.tmp

---
hosts.todo is a list of hosts to send the file to.

Also, remember that this needs to run as root with a .rhost file.

And, please test this before using it. I just wrote it off the top of my head and I'm not sure if I got rcp right.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top