1

I have been working to prepare a script which can help in monitoring a WAN Link with the traceroute command.

  • A script to grep an Primary WAN IP from traceroute and if found echo Primary is UP.
  • If Primary IP not found, grep secondary IP in traceroute and echo Primary down, secondary UP.
  • If both the IP's are not found, echo PRimary and Secondary are down

I prepared something of this sort, but couldnt get it work..

 echo "`traceroute  4.4.4.4>/tmp/trace.txt`"
 grep  “1.1.1.1” /tmp/trace.txt &> /dev/null
 if [ $? -eq 0 ] ; then
     echo  “Primary Is UP“
 else
 grep  “2.2.2.2” /tmp/trace.txt &> /dev/null
 if [ $? -eq 0 ] ; then
     echo “ Primary failed, Secondary Running”
else
     echo "Primary & Secondary both failed"
 fi
 fi
2
  • 2
    echo "`traceroute 4.4.4.4>/tmp/trace.txt`". Is your script supposed to run the traceroute command? In this case, it should be just 'traceroute 4.4.4.4>/tmp/trace.txt' Commented Sep 15, 2016 at 9:45
  • Thanks Loris, I have removed the double quotes and also found few spaces and quotes issues which i fixed.. it seems to be working. Commented Sep 15, 2016 at 11:17

2 Answers 2

2

For a quick test, don't use traceroute if you don't need it. Instead try ping with the Record Route (-R) option. Use the -c option to only make one request, and the -n option to not resolve the route hostnames, and the -W option to not wait too long for failure. Perhaps something like:

if ping -R -n -c1 -W2 4.4.4.4 >/tmp/trace.txt
then
    #echo something is up
    if grep -q '\<1\.1\.1\.1\>' /tmp/trace.txt
    then
        echo  Primary Is UP
    elif grep -q '\<2\.2\.2\.2\>' /tmp/trace.txt
    then
        echo  Primary failed, Secondary Running
    else
        echo "Primary & Secondary both failed, but something else Running"
    fi
else
    echo "Primary & Secondary both failed"
fi

It is possible you might need to convince one or more routers to honor record route.

0

Remove echo and the quotes in the first line of your script, just leave the command traceroute. Your script should be written as:

traceroute .... 
grep .....
...
1
  • One of my friend also suggested to use egrep and both the matching IP's in it using pipe. Some what like this egrep "169.254.255.17|169.254.255.146" /tmp/trace.txt &> /dev/null but i couldnt figure out how to get that work with matching parameter If [ ] Commented Sep 15, 2016 at 11:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.