+ 1
BASH How to determine whether IP is pinging or not from a list of IP addresses
#!/bin/bash # mandatory line for bash ips=$( cat /home/$USER/Documents/list_IPs.txt ) # command cat writes the file from place where I have a list for ip in $ips do ping -c1 "$ip" &>/dev/null && echo "$ip ping" | tee ping.txt || echo "$ip no ping" | tee noping.txt done echo "All done" # &> I do not want any comments, just IP and /dev/null is a black hole in Linux # tee - outputs both on the screen and the file at the same time
3 Respostas
+ 3
for ip in $ips
do
ping -c1 $ip | grep -q "time="
if [ $? ]
then
echo "$ip YES"
else
echo "$ip NO"
fi
done
gives results like this
107.23.70.193 YES
but the first script from question gives
107.23.70.193 no ping (it is correct) but thank you for the interesting use of grep
+ 2
I don't have a linux box to check at the moment but it should be a straightforward grep job. You can check whether something that should be in the ping response is there after you pinged (I picked "time=" which is how long it took for the response to arrive)
ping $ip | grep -q "time="
if $? then
# we got a response
else
# we didn't
fi
+ 2
Maybe grep something different, like "bytes from". Also you can try grep -c to count the number of occurences and see if that's equal to 0 or not.