Condition to test if a host can be pinged


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Condition to test if a host can be pinged
# 1  
Old 04-11-2012
Condition to test if a host can be pinged

I have a much larger script that takes an input file of hosts and determines if we support them and checks to ensure the FQDN coincides with our DNS. For instance, the hostname may return a different FQDN when passed to the "host" command, so I keep the value of the output of the "host" command and also apply some conditions to determine if NXDOMAIN is returned.

In a stunning twist of fate I see that sometimes the file I'm reading returns the ip address of a machine with more than one NIC (and ip address), and usually that NIC is not in operation so it's screwing up my reporting. I need to put a condition in my script that not only checks to see if the ip returns a valid hostname using the "host" command, but now I also need to the script to ping the machine and ensure it is working.

The input file contains a hostname, and three fields following the hostname. Each field is reserved for an IP address - sometimes there's 1 ip, sometimes 2, or 3. I already have a test to determine if the ip address works:
Code:
# if tmp1 is not a zero value
        if [[ -n $tmp1 ]]
        then

# if hosting the value of tmp1 does not return NXDOMAIN (hence if it's a valid host)
# and if that host can be pinged
# then host that value and exit the loop
                if [[ -z `host $tmp1 | grep -o NXDOMAIN` ]]
                then
                        host $tmp1 | cut -d" " -f5 | sed 's/.$//g'
                else
                        if [[ -n $tmp2 ]]
                        then
                                if [[ -z `host $tmp2 | grep -o NXDOMAIN` ]]                                then
                                        host $tmp2 | cut -d" " -f5 | sed 's/.$//g'
                                else
                                        if [[ -z `host $tmp3 | grep -o NXDOMAIN` ]]
                                        then
                                                host $tmp3 | cut -d" " -f5 | sed 's/.$//g'
                                        fi
                                fi
                        fi
                fi
        else echo $i >> exceptions_$dt
        fi
done

This works fine as is. However now I need to add an additional condition that says "can the host be pinged also? If not exit the loop and move onto the next one". I tried to add this but it's somehow sending an invalid value to the host command:
Code:
if [[ -z `host $tmp1 | grep -o NXDOMAIN` && -n `ping -c 1 -w 1 $tmp1` ]]

I tried putting the ping condition in its own brackets separating the conditions by the ampersands but I'm still having a problem. Can anyone suggest how I can implement this additional check?
# 2  
Old 04-11-2012
Code:
if ! host "$tmp1" | grep -qF NXDOMAIN && ping -c 1 -w 1 "$tmp1" > /dev/null 2>&1; then
  ...

P.S. I've noticed that you're using a GNU syntax (most likely you're executing the script on Linux),
so the -q option of grep should be available Smilie

Last edited by radoulov; 04-12-2012 at 04:20 AM..
# 3  
Old 04-11-2012
You don't have to run cut hundreds of times in backticks. Use the read command once.

Code:
while read FIELD1 FIELD2 FIELD3 FIELD4
do
        echo "Field 1 is "$FIELD1"
done < inputfile

'host' might be outputting to stderr, try 2>&1
# 4  
Old 04-12-2012
Quote:
Originally Posted by radoulov
Code:
if ! host "$tmp1" | grep -qF NXDOMAIN && ping -c 1 -w 1 "$tmp1" > /dev/null 2>&1; then
  ...

P.S. I've noticed that you're using a GNU syntax (most likely you're executing the script on Linux),
so the -q option of grep should be available Smilie
Oh thank you so much - it works now!!!

Quote:
Originally Posted by Corona688
You don't have to run cut hundreds of times in backticks. Use the read command once.

Code:
while read FIELD1 FIELD2 FIELD3 FIELD4
do
        echo "Field 1 is "$FIELD1"
done < inputfile

'host' might be outputting to stderr, try 2>&1
I'm sure my script is very novice and sophmoric and I do plan to learn more but I'm just in a situation where, figuratively (not really), I've got a boss standing over me saying "YOU BETTER GET THOSE REPORTS IN ORDER AND YOU'RE NOT LEAVING THE OFFICE TILL YOU DO" so I'm just trying to do my best to maintain. I will check the suggestion you mentioned and read more on the bash scripting guide. Thank you both!
# 5  
Old 04-12-2012
No insult intended of course, but the difference is important. It's very easy to make excruciatingly slow scripts by running external utilities thousands of times more than you need to instead of learning the proper way first. One of my first shell scripting experiments was a line-wrapper which wrapped lines slower than you could read them due to pipe and external utility abuse...

Also, I apologize for misunderstanding your original question. I thought you meant grep wasn't working, not that it was printing garbage.

Last edited by Corona688; 04-12-2012 at 01:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Test a condition for n times and take action

i have to test a condition for n times and if its value is greater than one each time for continuous 5 iterations it will take action My Approach run the same command 5 times in for loop and divert the output to a file and then read the values from file and use if else if and take action ... (1 Reply)
Discussion started by: abhaydas
1 Replies

2. UNIX for Dummies Questions & Answers

Simple negated test condition

Without grep, I'd like to make a test condition so that any a word that does not have the successive letters car in it will be echoed. for example, bluecar will contain "car" so it will show up as a no var=bluecar $ echo $var|if ]; then echo "yes";fi yes this variable contains "car" so I... (5 Replies)
Discussion started by: newbie2010
5 Replies

3. Shell Programming and Scripting

How to know if I am Pinged?

Dear All, I need to build a mechanism by which I can log if any node in my DC is pinging my server or not ? The purpose is al follows: Incase any node in my Data Center turns off , I need to have a mechanism by which I am notified that a certain node is no longer responding. On my... (1 Reply)
Discussion started by: Junaid Subhani
1 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

test condition

Hi there, I tried to search for this almost everywhere, but didnt get any proper information on it. What is the difference between ] Some of the code works when I have only single condition i.e. ] && $dothis1 || $dothis2 But if i try to include another testcondition to the... (1 Reply)
Discussion started by: tostay2003
1 Replies

6. Shell Programming and Scripting

Test condition

Hello, what is the better and correct way to perform a comparison: I have been using the following with no problems: if ] then .... fi I have seen this also used : if then .... fi When I try : if then .... fi I get an error like .... the test condition expects a... (4 Replies)
Discussion started by: gio001
4 Replies

7. UNIX for Dummies Questions & Answers

Condition test

Hi there, When I try to do a condition on test: $ str1=abcd $ test $str1 $ echo $? 0 Is there anyway to display the answer to be 'TRUE' or 'YES'? rather than 0? If so, how can I do it without using awk or sed. (2 Replies)
Discussion started by: felixwhoals
2 Replies

8. Shell Programming and Scripting

Condition test ( [[ ]] ) doubt

Hi , I have a doubt on condition test ( ] ). Pls refer blow program. #!/bin/ksh TEMP= if ;then echo $TEMP else print 'invalid option' fi Above script's TEMP variable has no value so it gives "invalid option" as output. But I got an error before priting the string . Result : ... (2 Replies)
Discussion started by: thambi
2 Replies

9. Shell Programming and Scripting

test and if condition

Guys look at this: i have to write a script that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this. READ WRITE EXECUTE OWNER LEE.BALLANCORE YES YES NO... (9 Replies)
Discussion started by: ciroredz
9 Replies

10. Shell Programming and Scripting

help: infinant loop script - host ping test

need to check on some hosts and send an email if there status changes I wanna put together a script in bash that will allow me to check the up/down state of a single host via ping i want it to run in a continuous loop so I can just fire the script and forget about it(dont want cron to drive... (2 Replies)
Discussion started by: zeekblack
2 Replies
Login or Register to Ask a Question