Results 1 to 6 of 6

Thread: Ping several computers at a time

  1. #1
    Join Date
    Oct 2010
    Location
    California, US
    Beans
    81
    Distro
    Ubuntu 12.04 Precise Pangolin

    Ping several computers at a time

    Hi all. I want to ping several computers connected to my network simultaneously without going through them one at a time to find out if the system is alive or not in the network. I want to write a bash command to do it (preferably). To do this, below is the code I came up with:

    Code:
    for host in (CAT ipaddresslist.txt)
    ping -c 1 $host >/dev/null 2>&1 && echo "$host up and running" || echo "$host down"
    where the ip address list is stored in the text file (lets say)

    Is there a faster way to do it? Because this will take O(n) time since the code pings one by one plus the communication overhead of the network. I want to do it for several thousands of computers on my network and do it fast. Any suggestions? Anyone?

    Thanks in advance

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,697
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Ping several computers at a time

    Hi najdorfchess.

    There are a couple of typical bash structures to do that.

    Using a for:
    Code:
    for host in $(cat ipaddresslist.txt); do
        ...
    done
    Or better, using a while:
    Code:
    while read -r host; do
       ...
    done < ipaddresslist.txt
    As for the body of the loop, this would work:
    Code:
    if ping -c 1 $host &> /dev/null
    then
        echo "$host up and running"
    else 
        echo "$host down"
    fi
    Now this:
    Quote Originally Posted by najdorfchess View Post
    I want to do it for several thousands of computers on my network and do it fast. Any suggestions? Anyone?
    At first glance, it does not look like a very good idea, and would really recommend rethinking what you are doing.

    The best way I can think of is doing it slowly, in intervals, so you don't overload the network.

    For example:
    Code:
    while read -r host; do
        if ping -c 1 $host &> /dev/null
        then
            echo "$host up and running"
        else 
            echo "$host down"
        fi
    
        sleep 10s
    
    done < ipaddresslist.txt
    That would make a 10 seconds pause after every trial.

    I hope this helps, and tell us how it goes.
    Regards.

  3. #3
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Ping several computers at a time

    Use nmap with the -sP option
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  4. #4
    Join Date
    Oct 2010
    Location
    California, US
    Beans
    81
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Ping several computers at a time

    Thanks for your reply guys. I think nmap is a good way to do it. I found couple of other solutions too, by using

    1) fping
    2) ARP protocol

    Both these basically send broadcast messages to all devices in network and invokes a reply from them confirming their existence on the network.

  5. #5

    Re: Ping several computers at a time



  6. #6
    Join Date
    Oct 2010
    Location
    California, US
    Beans
    81
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Ping several computers at a time

    Thank you all

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •