cancel
Showing results for 
Search instead for 
Did you mean: 

Need help in script

 
SOLVED
Go to solution
Waqar Razi
Regular Advisor

Need help in script

We have a HPUX server, where printers have been defined as queues. There are some printers which are not valid, either they have been replaced or have been shutdown (their IP's have been changed). I want to write a script which ping every printer in the list and then make a list which printers are not pingable.

Can some one please help me in this?
4 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need help in script

Hi Wagar:

# cat ./pinghosts
#!/usr/bin/sh
while read HOST X
do
ping ${HOST} -n 1 >/dev/null 2>&1
[ $? = 0 ] || echo "'${HOST}' does not answer"
done < pinghosts.lst
exit 0

# cat ./pinghosts.lst
hostname1
hostname2
hostname3

...run as:

# ./pinghosts

Regards!

...JRF...

Waqar Razi
Regular Advisor

Re: Need help in script

Hey James,

I tried this script as follows:

I made a list of all the printers as pinghosts.lst and then ran your script. It only points out the printers who are not present in DNS or failed in name resolution but not the ones who have been defined in DNS but are not pingable.
ping aguawhinv2200 -n 2
PING aguawhinv2200.col.com: 64 byte packets

----aguawhinv2200.col.com PING Statistics----
2 packets transmitted, 0 packets received, 100% packet loss
/home/root # echo $?
0

As you can see, it returns a code 0 for the host who is defined in DNS but you cant ping it as it is down or unavailable.

Can you please give me some guidance in this regard?


James R. Ferguson
Acclaimed Contributor

Re: Need help in script

Hi (again):

Sorry about that. Try this:

#!/usr/bin/sh
while read HOST X
do
ping ${HOST} -n 1 | grep -q "100% packet loss" && echo "'${HOST}' is down"
done < hostlist
exit 0

Regards!

...JRF...
Waqar Razi
Regular Advisor

Re: Need help in script

Thank you very much