BASH How to determine whether IP is pinging or not from a list of IP addresses | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

22nd Oct 2018, 12:22 PM
JaMi
JaMi - avatar
3 Answers
+ 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
22nd Oct 2018, 1:30 PM
JaMi
JaMi - avatar
+ 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
22nd Oct 2018, 12:33 PM
Schindlabua
Schindlabua - avatar
+ 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.
22nd Oct 2018, 1:55 PM
Schindlabua
Schindlabua - avatar