LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2020, 06:20 PM   #1
cadillacrick
LQ Newbie
 
Registered: Jul 2020
Posts: 2

Rep: Reputation: Disabled
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
 
Old 07-10-2020, 06:55 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 4,956

Rep: Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830Reputation: 1830
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 ]
 
Old 07-10-2020, 06:55 PM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,756

Rep: Reputation: Disabled
Quote:
Originally Posted by cadillacrick View Post
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.)
 
1 members found this post helpful.
Old 07-10-2020, 07:14 PM   #4
cadillacrick
LQ Newbie
 
Registered: Jul 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
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

Last edited by cadillacrick; 07-11-2020 at 07:12 AM.
 
Old 07-10-2020, 07:19 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,224

Rep: Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Packet going out of wrong interface due to OS adding cache route - OS trying to route through default route msr1981 Linux - Networking 2 10-11-2017 02:58 PM
Packet going out of wrong interface due to OS adding cache route - OS trying to route through default route msr1981 Red Hat 1 10-09-2017 05:45 AM
Script check ICMP and auto change route static buff2l0 Linux - Networking 3 12-09-2009 02:05 AM
I am not able to add a new route to my route table using route command prashanth s j Linux - Networking 2 09-03-2005 04:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration