简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM