(Disclaimer- I know this can be done more easily with nmap, but its supposed to be practice for bash shell scripting).

$ scriptname 192.168.42 18 23 

Checking: 19 20 21 22 23 

Live hosts: 

There were: 
 5 alive hosts 
 1 not alive hosts 
 found through the use of 'ping'. 

Above is what I want the output to look like. 'Checking: ...' will list whatever IP is being checked. All of the Live ones will be listed under "Live Hosts" and then running totals for alive and not-alive.

Here is what I have so far

for i in `seq ${2} ${3}`    
    ping -c 1 -t 1 ${1}.${i} > /dev/null 2>&1;
    if [ $? -eq 0 ]; then
        echo "Live hosts: \n ${1}.${i}"; LIVECOUNT=$( ???? )

The LIVECOUNT=$( ???? ) because I don't know what to put in it. I'm not sure how to keep a running total for living/non living ones. The 'Live Hosts' and 'There were' sections don't need to appear until the checking is complete.

  • 1
    Just use nmap. Really. It's made to do stuff like this.
    – Chris Down
    Apr 21, 2014 at 7:00
  • 1
    I concur with ChrisDown. Use either nmap or fing.
    – slm
    Apr 21, 2014 at 7:13
  • BTW, what you have with the printing of "Live hosts" is going to be printed every time through the loop.
    – slm
    Apr 21, 2014 at 7:19

1 Answer 1


The short answer to your first question is LIVECOUNT=$((LIVECOUNT+1)). This arithmetic expansion, shown by double parens: $(()), to increment the variable LIVECOUNT by one each time the ping command returns 0 for success.

The short answer to figuring out the total number of hosts is to subtract the lower address (${2}) from the upper address (${3}) and then add one to get the total hosts.


TOTALHOSTS=$(($3 - $2 + 1))
for i in `seq ${2} ${3}`
    ping -c 1 -t 1 ${1}.${i} > /dev/null 2>&1;
    if [ $? -eq 0 ]; then
        LIVEHOSTS="$LIVEHOSTS \n ${1}.${i}"
echo ""
echo "Checking: $CHECKING"
echo ""
echo "Live hosts:"
echo -e "$LIVEHOSTS"
echo ""
echo "There were:"
echo "$LIVECOUNT alive hosts"
echo "$((TOTALHOSTS - $LIVECOUNT)) not alive hosts"
echo "found through the use of ping"

Note the following: $CHECKING is a string that starts with the subnet given as parameter 1 ${1} and then adds the current host number ${i} for each iteration of the do loop.

Because of the way you have this formatted (with the "Checking: " line ahead of the "Live hosts:" line) it seemed easier to keep the live hosts in a string (similar to $CHECKING), so that's what $LIVEHOSTS is.

$LIVEHOSTS and $LIVECOUNT are initialized at blank and zero to make it clear what's going on; this isn't strictly speaking, required.

Note the use of echo -e when $LIVEHOSTS is output. That's to make the \n work as a newline.

EDIT: To show what IP is currently being checked and not a list at the end, replace CHECKING="$CHECKING${i} " with echo "Checking $CHECKING${i}" and delete or comment out the line echo "Checking: $CHECKING". Since the echo is inside the do loop, it will get printed on every pass, using the then-current value of $i. (This is the same thing @slm pointed out about your use of echo "Live hosts" in your original script.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .