-1

Is there any way or script that could ping and fetch the ttl simultaneously and store in two columns in a text file for a set of IP addresses that are alive on network- I wish to make a shell script that could help me make a load balance graph. Had tried with nmap and dig and fping command but couldn't get the desired output as asked. Working on CentOs 6.5

0

This simple script might do the job for you, if I have understood the question that is.

#!/bin/sh

# List of IP or domain names
LIST="192.168.1.101 192.168.1.110 192.168.1.254 192.168.1.250"

# Where to store the data?
outFile="${HOME}/network-test"

# raw data per IP
raw=""

# Clear the result each time or not? This will clear it each time
echo -n > "${outFile}"

for ip in $LIST
do
    raw=`ping -c 1 -t 255 "${ip}" | grep ttl | awk -F" |ttl=" '{ print $1 }'`
    if [ "$raw" != "" ]
    then
        echo "${ip} ${raw}" >> $outFile
    else
        echo "${ip} no-ping" >> $outFile
    fi
done

cat $outFile

exit

The display would be something like:

192.168.1.101 64
192.168.1.110 64
192.168.1.254 64
192.168.1.250 no-ping
  • You just made my day brother @Tigger Thanks... – Pranjal Gupta Aug 18 '17 at 10:09
  • @PranjalGupta - Happy to have helped. – Tigger Aug 18 '17 at 23:14
  • Hye @Tigger, I am getting 64 as output i.e. from "64 bytes of data........." part of ping query since when I pinged the required IP with 2 packets the TTL was 127 and not 64 but the script you provided yield 64 as an answer which I think is 64 from "64 bytes of data...." part – Pranjal Gupta Aug 26 '17 at 19:09
  • @PranjalGupta You need to do some research in to TTL. From Time to live (wikipedia) A recommended initial value is 64 and also check this SuperUser Q&A what's the meaning of TTL=64?. – Tigger Aug 27 '17 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.