Home > bash > Bash: Script to find active computers in a subnet using ping

Bash: Script to find active computers in a subnet using ping

January 27th, 2010 mat Leave a comment Go to comments

The following is a simple bash script that will scan each ip address in a give subnet and report if they are alive (or accepting ping requests). The code creates processes for each ping so that it completes quickly rather than scanning each ip address sequentially.

Create a text file called “pinger.sh” and paste the following into it:

#!/bin/sh

: ${1?"Usage: $0 ip subnet to scan. eg '192.168.1.'"}

subnet=$1
for addr in `seq 0 1 255 `; do
#   ( echo $subnet$addr)
( ping -c 3 -t 5 $subnet$addr > /dev/null && echo $subnet$addr is Alive ) &
done

Save and close, then it can be called from the command line like so:

sh pinger.sh 192.168.1.

This will scan from 192.168.1.0 to 192.168.1.255 and will return something like the following:

192.168.1.1 is Alive
192.168.1.105 is Alive
192.168.1.112 is Alive
192.168.1.149 is Alive


Bookmark and Share
Categories: bash Tags: , ,
  1. January 28th, 2010 at 14:22 | #1

    Your example will only scan 192.168.1.1 and above. You need to change your sequence if you want .0 in there.

    for addr in ‘seq 0 1 255 ‘; do

  2. January 28th, 2010 at 14:59 | #2

    Ah good spot!

  1. No trackbacks yet.
// unused langs // // // //