Help with script to draw routes

1

I am creating a script similar to tracerouce, when I type an IP it traces the route of arriving at the final destination, but when the route reaches the destination, the while still continues, as I correct this problem, stop when I reach the destination end?

#!/bin/bash
echo "Digite um IP: "; read ip
count=1;
while [ $count -lt 30 ]; do
    ping $ip -t $count -c 1 | grep ^From | awk '{print $2}'
        if [ $? -eq 0 ]; then
            printf 'O salto é: '    
        else
        echo 'Rota Indisponível'
        fi
let count=$count+1;
done
    
asked by anonymous 08.05.2017 / 03:31

1 answer

0

The problem is in the return variable '$?', used in conditional deviation. You are checking for the awk command return, which always returns '0' in this case, so the loop will always execute until $ count reaches 30. You can validate ping output after awk, as the change in your script below :

#!/bin/bash
echo "Digite um IP: "; read ip
count=1;
while [ $count -lt 30 ]; do
  ping $ip -t $count -c 1 | grep ^From | awk '{print $2}' | grep -P '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
    if [ $? -eq 0 ]; then
      printf 'O salto é: '
    else
      echo 'Rota Indisponível'
      exit
    fi
let count=$count+1;
done
    
19.01.2018 / 19:12