Answer the question
In order to leave comments, you need to log in
How to ping IPs and get only ip and ttl in the output?
I have a hosts.txt file, it contains a list of network ip addresses. I need to ping each of these IPs and get something like "192.168.X.X => ttl=64" in the output. How can this be implemented?
Answer the question
In order to leave comments, you need to log in
#!/bin/bash
FILE=$1
while read LINE; do
ttlstr=$(ping -c1 -w1 $LINE | grep -o 'ttl=[0-9][0-9]*') || {
printf "%s is Offline\n" "$LINE"
continue;
}
ttl="${ttlstr#*=}"
printf "%s is Online, ttl=%d\n" "$LINE" "$ttl"
done < $FILE
file addr.txt
192.168.2.1
8.8.8.8
10.9. 9.9
4.4.4.4
9.9.9.9
1.1.1.1
Running and outputting
[email protected]:~# ./pingffile.sh addr.txt
192.168.2.1 is Offline
8.8.8.8 is Online, ttl=110
10.9.9.9 is Offline
4.4.4.4 is Offline
9.9.9.9 is Online,
1.1.1.1 is Online, ttl=58
As one of the options
for line in `cat host.txt` ; do ping -c 1 $line |awk '{print $4 $6}'| head -n2|tail -n 1; done
fping -e -f host
8.8.8.8 is alive (45.9 ms)
4.2.2.4 is alive (62.0 ms)
8.8.8.8 is alive (45.9 ms)
4.2.2.4 is alive (62.0 ms)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question