LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to check ip route (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-check-ip-route-4175678504/)

cadillacrick 07-10-2020 06:20 PM

Script to check ip route
 
Hey guys, first time post...

I'm trying to get a script to check if a route exists after I connect a VPN... First time trying to get this done and need some help...

This is what I have so far --

Code:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
EXIST='ip route show 10.10.200.0/24 | wc -l'

if  [$EXIST -eq 0]
then
        echo "--- Route is down ---"
        ip route add 10.10.200.0/24 via ppp1
fi

if [$EXIST -eq 1]
then
      echo "---Route Enabled---"

fi
exit


Getting these errors when I try and run it...

./routecheck.sh: line 5: [ip: command not found
./routecheck.sh: line 11: [ip: command not found


If I run my command 'ip route show 10.10.200.0/24 | wc -l' it works fine.. i get a value of 1 when the route is active... but for some reason in the script it doesn't work... i'm sure i'm missing something stupid here.. but i'm new to this... so I need help please.


Thanks!

Richard

scasey 07-10-2020 06:55 PM

The error is not about the ip command. It’s a syntax error about the test command. Put a space between the brackets and the test.

Code:

[ $EXIST -eq 0 ]

Ser Olmy 07-10-2020 06:55 PM

Quote:

Originally Posted by cadillacrick (Post 6143980)
Getting these errors when I try and run it...

./routecheck.sh: line 5: [ip: command not found
./routecheck.sh: line 11: [ip: command not found

When $EXIST is expanded, it becomes ip route show 10.10.200.0/24 | wc -l. That means that [$EXIST turns into [ip route show 10.10.200.0/24 | wc -l, and there's obviously no command called "[ip". A space after the "[" would get rid of that particular error.

But the "if [ <some command> -eq 0 ]" construct won't work regardless, because "-eq" expects the left part to be a literal number.

If you want to compare the output from the command(s), use
Code:

if [ $(<command>) -eq <value> ]; then
or
Code:

if [ "$(<command>)" = "<string>" ]; then
as $(<command>) returns the text produced by the command(s) inside the parentheses.

However, if you want to check the exit code of a command, this will work fine:
Code:

if <command>; then
The part after "then" will be executed if the command returns 0. If multiple commands are run via pipes, the exit code of the last command is the one that's relevant.

Unfortunately, neither ip nor wc returns an exit code other than 0 in this particular case, regardless of the contents of the routing table. However, grep would:
Code:

if ip route show 10.10.200.0/24 | grep via; then
  ...do something...
fi

(via being the text that appears before the gateway for the network or host in question, if a route is found.)

cadillacrick 07-10-2020 07:14 PM

scasey -- I tried the space and it just gave me another error -- too many arguments...

Ser Olmy -- Thanks for the suggestions! I found the one that worked for me... here is the final script that works just fine!


Code:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

if [ $(ip route show 10.10.200.0/24 | wc -l) -eq 0 ]; then

        echo "--- Route is down ---"
        ip route add 10.10.200.0/24 via 1.1.1.1
fi

if [ $(ip route show 10.10.200.0/24 | wc -l) -eq 1 ]; then

        echo "--- Route Enabled ---"
fi



exit


michaelk 07-10-2020 07:19 PM

Welcome to LinuxQuestions.

Use http://www.shellcheck.net to check your programming syntax.

Similar to the above since the ip command returns nothing if no route exists
Code:

#!/bin/bash

EXIST=$(ip route show 10.10.200.0/24)

if  [ -z $EXIST ]
then
        echo "--- Route is down ---"
        ip route add 10.10.200.0/24 via ppp1
else
        echo "--- Route is Up ---"
fi



All times are GMT -5. The time now is 07:15 AM.