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.

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 need to write a bash shell script that will check a range of IP addresses to see if a host is “alive” at that address.I also need to list the IP numbers of those that are “alive”, and provide a summary showing the number of “alive” and “not alive” IP addresses. The range of addresses will all be in a “Class C” network. I'm having problems figuring out how to pass through each arg and get it pinging each individual address within the range.

Below is a sample run of the script I intend to create.

$ programName 192.168.42 18 22 

Checking: 19 20 21 22 

Live hosts: 

There were: 
 2 alive hosts 
 3 not alive hosts 
 found through the use of 'ping'.
share|improve this question
Note that ping gives only the most rudimentary indication of 'aliveness'. Which is why things like Nagios exist, to allow you to check specific services or the running state on the machine. A ping check is a bit like checking a person is breathing; they could nevertheless be in a coma and not able to perform any function. It is entirely possible for a server to respond to pings and be otherwise totally unresponsive. – ire_and_curses Nov 20 '13 at 5:35
Don't write a bash script for this. There are prebuilt tools available already. Leverage them instead. Try nmap for starters, it has just this function built in. – Caleb Nov 20 '13 at 8:50
Upvoting @Caleb for nmap. If systems are filtering ICMP and not responding, you'd never know if there was a system at the address you're trying to ping. However, if you're on the same subnet, when you ping the address your system will also leverage ARP. This will cause it to cache the MAC address in the ARP table which is visible with ip neigh show. So even if it doesn't respond to a ping, you'll still know if something is there because you'll get it's MAC address in your ARP cache. – Sean C. Nov 20 '13 at 13:03
Just to add a note: Your example seems to assume a /24 subnet (which used to be called class C, but in the last decades we changed to CIDR). Thus your example will not allow you to test a /25, a /23 or any other subnet which netmask is not a multiple of 8. That might suffice for you, but it really is something you need to be aware off. – Hennes Nov 20 '13 at 14:08

This should get you started.

for i in `seq ${2} ${3}`
    ping -c 1 ${1}.${i} > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "${1}.${i} responded."
        echo "${1}.${i} did not respond."

Sample output:

xxxx@xxxxxx:~$ bash test.sh 10.140.0 100 103 responded. did not respond. did not respond. did not respond.

The bash manual can probably handle anything else you need.

share|improve this answer
Also, according to the OP's example, you need to use ${1%.*} instead of ${1}. – Joseph R. Nov 20 '13 at 5:25
@ChrisDown I'd love to concoct some great logic behind why I chose to evaluate the full path to the binary in a sub shell and start an enthusiastic discussion. I'd just be trolling at that point though. For now I'll just say that I threw that in there because I wanted the person asking the question, who obviously didn't even bother to look at any documentation, to question its function and maybe read a manual. I'll go ahead and remove the unnecessary sub shell for you. – Sean C. Nov 20 '13 at 12:55

Use nmap. It's the standard tool for the job.

Since you just want to know which hosts are online (and responding), run a “ping scan”: nmap -sP.

If you don't care about the formatting of the output, your script can be written this way:

nmap -sP "$1.$2-$3"
share|improve this answer

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.