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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

host file

Status
Not open for further replies.

vti

Technical User
Feb 26, 2001
189
TR

Hi ,
I need a script to ping all /etc/hosts file entries and find out if are there any entries in this file which i don't need.I think to find out ping would be fine but if you got any diferent idea it would be cool too.

Thanks
 
VTI,

Something like this should work fine:

for server in `cat /etc/hosts | awk '{print $2}'`

do ping $server >> /tmp/ping.out
done

grep -v alive /tmp/ping.out > /tmp/notalive.out

cat /tmp/notalive.out | mailx -s "Hosts That Are Not Responding" abc@abc.com

Thanks,

John
 
You may need to change the following line:

for server in `cat /etc/hosts | awk '{print $2}'`

to

for server in `cat /etc/hosts | grep -v ^# | awk '{print $2}'`

Thanks,

John
 
or you can even change it to:

for server in $(nawk '/^[^#].*/ {print $2}' /etc/hosts) vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
hi ,
Thanks for answers .I tried all but it only pings the first line (local host) but i need to ping all entries and if it's pinging doesn't matter but is there any entries i can not ping i am gonna remove them from hosts file.So i need to find only entries i don't need.
 
The script that I gave you should work fine.

for server in `cat /etc/hosts | grep -v ^# | awk '{print $2}'`

do ping $server >> /tmp/ping.out
done

grep -v alive /tmp/ping.out > /tmp/notalive.out

cat /tmp/notalive.out | mailx -s &quot;Hosts That Are Not Responding&quot; abc@abc.com

Are you getting any error messages? Did it create the output files ping.out and notalive.out?

Thanks,

John
 
Here is a quickie, however it will try to ping blank lines. I did not have time to fix it

sed -e/^#/d /etc/host | cut -f 1 > tmp.hosts

while read line x
do
eval &quot;ping -w 1 -c 1 ${line}&quot;
done < tmp.hosts
 
thank you all,they all works fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top