I have a problem and it is that I am trying to make my script when an IP of which I put a range for example: 10.0.2.0 to 10.0.2.10 and of those that are down during the script, resend them to a txt file
This is what I have
while read line
do
echo $line
ping -c 5 $line
if [ $line -eq 0 ]; then
echo "Hay conexión"
else
echo "No hay conexión" >> enviarip.txt
fi
done < ip.txt
ssmtp correo < enviarip.txt
What you're doing is going pretty well. Perhaps your issue is how the command works
ping
and how to make it easy to exit.One option is to use it
-c 1
to send just one packet, in addition-W 1
to waiting for the response for only one second:In a script, it can work like this:
Resulting in:
In your case, it is not necessary to make an array; I just did it for example purposes. Instead of an array, just feed
xargs
the contents of your address file.Ping returns an error code if it failed and 0 when all went well.
In this case
1>/dev/null 2>&1
it is so that it does not output stdout or stderr on the screen