Sponsored Content
Top Forums UNIX for Advanced & Expert Users Condition to test if a host can be pinged Post 302622189 by MaindotC on Wednesday 11th of April 2012 05:16:46 PM
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?
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ftpaddhost(1M)                                            System Administration Commands                                            ftpaddhost(1M)

NAME
ftpaddhost - set up a virtual FTP host SYNOPSIS
ftpaddhost -c | -l [-b] [-x xferlog] hostname root_dir DESCRIPTION
The ftpaddhost script is executed by the super user to set up virtual FTP hosts. The ftpaddhost command configures the virtual host host- name under directory root_dir. The value of hostname can be an IP address or the name of a host. OPTIONS
The ftpaddhost script supports the following options: -b Create a banner for the virtual host. This option is useful to confirm that the virtual host is working. -c Configure complete virtual hosting. This option allows each virtual host to have its own version of the ftpaccess, ftpconversions, ftpgroups, ftphosts, and ftpusers files. The master version of each of these configuration files is copied from the /etc/ftpd directory and placed in the /etc/ftpd/virtual-ftpd/hostname directory. If the /etc/ftpusers file exists it is appended to the virtual ftpusers file. If a virtual host lacks its own version of a configuration file, the master version is used. -l Configure limited virtual hosting. This option allows a small number of parameters to be configured differently for a virtual host. See the virtual keyword on the ftpaccess(4) manual page. -x xferlog Create a logfile entry such that the transfer logs for the virtual host are written to the specified file. An abso- lute path must be specified for the xferlog file. OPERANDS
The following operands are supported: hostname The host name or IP address of the virtual server. root_dir The absolute pathname of the directory under which the virtual server is set up. EXIT STATUS
The following exit values are returned: 0 Successful completion 1 Improper usage of the command 2 Command failed FILES
/etc/ftpd/virtual-ftpd/hostname The configuration files directory for the virtual host hostname. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWftpu | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
ftpconfig(1M), in.ftpd(1M), ftpaccess(4), ftpconversions(4), ftpgroups(4), ftphosts(4), ftpusers(4), xferlog(4), attributes(5) SunOS 5.10 1 May 2003 ftpaddhost(1M)
All times are GMT -4. The time now is 12:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy