Le 4 mai 2021, la plateforme Yahoo Questions/Réponses fermera. Elle est désormais accessible en mode lecture seule. Aucune modification ne sera apportée aux autres sites ou services Yahoo, ni à votre compte Yahoo. Vous trouverez plus d’informations sur l'arrêt de Yahoo Questions/Réponses et sur le téléchargement de vos données sur cette page d'aide.

Eugene D a posé la question dans Computers & InternetProgramming & Design · il y a 1 décennie

Is it possible to ping multiple IP addresses simultaneously in a bash script?

I'm looking to ping multiple IP addresses in a bash script. Is there a command to do that?

If not, I'm wondering if there is a way to do it this way:

1. run in a loop

2. ping an address

3. move onto the next ip address without waiting for the response from the previous ping

4. repeat step 1 until all pings are done

5. send the ping output to a file

Mise à jour:

The pinging is just to verify an IP in our network is in use. I currently have a script that will sweep through all the addresses but it takes over 14 hours to ping every address in the network.

pseudo-code:

loop start

ping address

increment to next address

end loop

Mise à jour 2:

adding & like this:

output=`ping -c 1 -t 2 -q $network$index1.$index2 & | grep min/avg/max`

if [ "$output" ]

then

echo "good $network$index1.$index2"

else

echo "BAD $network$index1.$index2"

fi

does not work. I get this:

pingSweep.sh: command substitution: line 1: syntax error near unexpected token `|'

pingSweep.sh: command substitution: line 1: `ping -c 1 -t 2 -q $network$index1.$index2 & | grep min/avg/max'

running this without the & works exactly the same way if I was to loop through and ping each address individually. But I want to hit them all at once to shorten the time it takes to get all the results.

the problem with adding & at the end of the ping is that I can't redirect the output for some reason. It sees it as an invalid command.

Mise à jour 3:

about the above. when I mean it doesn't work, I added the & at the end of the ping and it didn't work.

But as above, it will ping each individually one at a time.

1 réponse

Pertinence
  • il y a 1 décennie
    Réponse favorite

    I assume you know how to make a loop.

    Maybe if you add & to the end of the ping command so that bash doesn't wait for it to return -- just a thought, I'm no good at bash programming.

    You can definitely redirect ping's output to a file with >.

Vous avez d’autres questions ? Pour obtenir des réponses, posez vos questions dès maintenant.