Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 5 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 '14 at 14:04
(Thanks for the edit though, still, the answer was not wrong ;) – Bonsi Scott Apr 1 '14 at 14:05
I want something with a shorter timeout than 1 second... decimal values for '-W' don't work – isaaclw Apr 13 '15 at 16:20

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"]=" 22"
SERVER["SERVER2 - Client bar"]=" 80"
# ...
# add as many server/port combinations as you'd like


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
    printf "%25s %25s %10s %10s\n" "$server" "$1" "$2" "$STATUS" 

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

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

 ~$ bash /tmp/foo.sh
 ========================= ========================= ========== ==========
          Host Description                        IP       Port     Status
 ========================= ========================= ========== ==========
      SERVER2 - Client bar          80     *DOWN*
      SERVER1 - Client foo          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

Your Answer


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.