1

Currently I'm looking into scripting and was writing something that will ping devices on a network, state if its reachable, and then take the ttl data from the ping and stating it's operating system.

I'm not completely familiar with how the grep command works but this is what I have so far:

for host in $(seq 1 255);
do

ping -c 1 $sn.$host | grep "Unreachable" &>/dev/null

if [ $? -eq 0 ]; then
    printf "%s\n" "$sn.$host is Offline"
fi

ping -c 1 $sn.$host | grep "ttl" &>/dev/null

if [ $? -eq 0 ]; then
    printf "%s\n" "$sn.$host is Online"
fi

done

How could I modify this script, knowing that Linux has a ttl of 64, Windows a ttl of 128, and ioS a ttl of 255 to make it output something like "$host is Online. Operating System: Linux"?

I know I could for example replicate the "ping -c 1 $sn.$host | grep "ttl"" line three times, adding the ttl value and creating the if statement that would print what I want, but I would like to get it done in one execution of ping if possible.

0

You can get the ttl by modifying your ping as follows:

ttl=$( ping -c 1 turing | grep ttl | sed 's/.*ttl=\([[:digit:]]*\).*/\1/' )

After this, it is straightforward to check the value in ttl and print the required information.

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.