Stijn Marivoet
Stijn Marivoet

Reputation: 79

How to ping several hosts at the same time (bash) linux

ip="192.168.129."
function addToList(){
list="$list $1"

}

addToList $1
for i in $ip{$list}
do
ping -c 1 $ip$1 > /dev/null

echo "Ping Status of $ip$1 : Success" ||
echo "Ping Status of $ip$1 : Failed"
done

How can i ping more than one host at the same time and show it in a list which ip address is up or down?

Upvotes: 0

Views: 2988

Answers (2)

firemankurt
firemankurt

Reputation: 107

Here is a script I wrote after reading a similar post.

https://bitbucket.org/kurtjensen/nettest/src/master/

It can use multiple text files as possible configs and the config files give you a chance to name the ip address more descriptively. The example config files are

  • home.txt - Which is the default
  • momdad.txt - This is for my parents network
  • etc.

So I can run the script at home and just hit enter at the prompt or enter something like "momdad" to switch to a different config fo a different network.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328556

One way is to use a more powerful ping tool like fping.

The other approach is to run the pings in the background:

for ip in $*; do
    if [[ "$ip" =~ "^[0-9]+$" ]]; then
        ip="192.168.129.$ip"
    fi

    (
        ping -c 1 $ip > /dev/null
        if [ $? -eq 0 ]; then
            echo "node $ip is up" 
        else
            echo "node $ip is down"
        fi
    )&
done

(...)& runs a script in the background.

Upvotes: 4

Related Questions