Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm testing stability of a machine and I need way to simply write a Bash script to determine if a host is online or not. How could I script this?

if [ ! $(hostisonline) ]; then
    # profit
fi
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

A simple solution would be:

ping -W 1 $ip

where -W specifies a timeout in seconds.

Make sure its a capital W. You can also use -i to specify a waiting time in seconds.

share|improve this answer
    
-w works as well. linux.die.net/man/8/ping –  Bonsi Scott Apr 1 at 14:04
    
(Thanks for the edit though, still, the answer was not wrong ;) –  Bonsi Scott Apr 1 at 14:05
add comment

If the host answers ping then test if its on line by (just an example):

ping -c 10 $host

If pings are not returned/forbidden, I'd check for individual services through nmap:

nmap $host -p $known_port --max-retries 10 | grep -q open

For example to test if ssh is expected to be opened, replace known_port by 22

as for the script it could be (the example -- it's Sunday morning and I'm bored so it has grown a bit after some edits -- is aimed for testing a bunch of hosts, not just one):

declare -A SERVER
# SERVER["NAME - Description"]="IP KNOWNPORT"
SERVER["SERVER1 - Client foo"]="111.111.111.111 22"
SERVER["SERVER2 - Client bar"]="222.222.222.222 80"
# ...
# add as many server/port combinations as you'd like

NMAP_MAX_RETRIES=10

printf "========================= ========================= ========== ==========\n"
printf "%25s %25s %10s %10s\n" "Host Description" "IP" "Port" "Status"
printf "========================= ========================= ========== ==========\n"


for server in "${!SERVER[@]}"; do
    set -- ${SERVER["$server"]}
    eval nmap \$1 -p \$2 --max-retries $NMAP_MAX_RETRIES | grep -q open
    if [ "$?" == "0" ]; then
        STATUS="[OK]"
    else
        STATUS="*DOWN*" 
    fi
    printf "%25s %25s %10s %10s\n" "$server" "$1" "$2" "$STATUS" 
done

 printf "========================= ========================= ========== ==========\n"

When executed it returns (fake ips, so servers are down):

 ~$ bash /tmp/foo.sh
 ========================= ========================= ========== ==========
          Host Description                        IP       Port     Status
 ========================= ========================= ========== ==========
      SERVER2 - Client bar           222.222.222.222         80     *DOWN*
      SERVER1 - Client foo           111.111.111.111         22     *DOWN*
 ========================= ========================= ========== ==========
share|improve this answer
    
This requires bash 4.0 or higher. –  jordanm Jan 13 '13 at 17:39
    
true, but just for the array stuff. –  hmontoliu Jan 14 '13 at 11:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.