subnet ping scan in shell

Today I logged in a machine I don’t want to install anything on it, but I wanted to find a machine in its network.
I came up with the little shell script that scans the subnet:


while [ $CURR -lt 255 ] ; do
  ping -c1 -t1 $SUBNET.$CURR 2>&1 >/dev/null
  if [ "$?" -eq "0" ]; then
    echo "$SUBNET.$CURR"
  let CURR=$CURR+1

This script is suboptimal but it does the stuff: It uses ping with a timeout of 1 sec, so If no machine is up, the script takes around 255 seconds to scan the subnet, it doesn’t list the machines that doesn’t reply to ping and so on … but as I said it , it does the stuff.

I tested this script in Linux and OSX.



  1. FreedomSound said

    About 4min to scan subnet.. Waow.. Great Job 😉

    Is -W option to speed up your script ?

  2. smaftoul said

    I think 4 minutes is slow ! 🙂

    ‘-W’ is the timeout, I set the timeout to 1 sec , this is the minimum you can set !

  3. Billigflug said

    Looks like a nice one, I’ll give it a try tomorrow. I have to see and log and maybe then I can give you some suggestions to boost its speed a little.

  4. smaftoul said

    Hi Billigflug !
    I would be very happy if you make some suggestions !
    I would also be happy if it works for you, and don’t forget to tell me the os on which it works (or doesn’t) ! 🙂

  5. federico cattozzi said

    I have edit the script and now it’s very fast!
    It uses parallel jobs to execute many pings at the same time.


    touch $FILE

    function multiping() {
    ping -c1 -t1 $SUBNET.$CURR 2>&1 >/dev/null
    if [ “$?” -eq “0” ]; then
    echo $(arp $SUBNET.$CURR) >> $FILE

    while [ $CURR -lt 255 ] ; do
    multiping &
    let CURR=$CURR+1
    let MOD=$CURR%100
    #this code is for prevent the saturation of the executable connections
    if [[ $MOD -eq 0 ]]; then
    cat $FILE
    rm $FILE

  6. federico cattozzi said

    Excuse me, i have forgotten the use:

    MacChicken:~ federico$ bash 192.168.1

    The only parameter is the subnet ip: 192.168.1 for example.
    I tested this script in Debian and MacOSX.

  7. Philipe said

    I have tried pasting both of these into Apple’s Script Editor but they don’t appear to work. What am I missing?

    • smaftoul said

      Philipe: it’s not applescript, it’s shell script.
      To use it, create a file with the script, launch a terminal, do chmod +x /path/to/filename_of_script and the execut: /path/to/filaname 192.168.0 for example. Hope this helps !

  8. thewrz said

    Did exactly what I needed. Thanks dude.

  9. eric said

    I think “ping -b ” can solve the problem easily.

    ping -b
    WARNING: pinging broadcast address
    PING ( 56(84) bytes of data.
    64 bytes from icmp_seq=1 ttl=64 time=0.104 ms
    64 bytes from icmp_seq=1 ttl=64 time=0.203 ms (DUP!)

    • smaftoul said

      Not every platform answers to a broadcast ping, but that may be a good start !

  10. john said

    um… has no one heard of nmap? It does exactly this and soooo much more.

    nmap -sP

    try it, if you don’t have it installed, shame on you!


  11. Gary said

    Cool script for an impatient Linux newb. We’re on Centos and added -w1 and it helped performance significantly.


RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: