LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2015, 07:11 PM   #1
slionl43
LQ Newbie
 
Registered: Jun 2015
Posts: 3

Rep: Reputation: Disabled
Angry custom shell script to adding static route does not work


Hi guys,

I've googled this many times and nobody seems to have a clear answer.
Here is what im trying to do:
I want to add a static route to the route table on my CentOS box. I know I can do this with the following command:
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0

As you know, when you reboot, static routes disappear. So what I did was write a shellscript so that every hour the shell script is run using crond.

The script i put in the following dir:
/usr/bin

The script is run as:
root

The script looks like this:
#!/bin/sh
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0

permissions look like this:
-rwxrwx--- 1 root root 87 Jun 2 17:01 tunroutetest.sh

When I run this script manually from the webmin scheduled cronjobs GUI, it adds the route successfully. So I know its not my script command. However, when it is run automatically, i get the following error via email alert:

/usr/bin/tunroutetest.sh: line 2: route: command not found

How do I fix this? Why is it not finding the route command and adding it automatically via crond? Please help!
 
Old 06-02-2015, 07:17 PM   #2
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 875

Rep: Reputation: 282Reputation: 282Reputation: 282
When writing scripts assume that you will not have a valid path setting.

so instead of just putting "route" and hoping for the best put "/sbin/route"
 
Old 06-02-2015, 07:27 PM   #3
slionl43
LQ Newbie
 
Registered: Jun 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wildwizard View Post
When writing scripts assume that you will not have a valid path setting.

so instead of just putting "route" and hoping for the best put "/sbin/route"

Ok, I modified it to:
#!/bin/sh
/sbin/route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0

and its working. Thanks! Why include /sbin when the file is actually in /usr/bin? Thanks again for your help!

By the way, I get this error in email alert:

/usr/bin/tunroutetest.sh
SIOCADDRT: File exists

Last edited by slionl43; 06-02-2015 at 07:30 PM.
 
Old 06-02-2015, 07:35 PM   #4
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 875

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by slionl43 View Post
Why include /sbin when the file is actually in /usr/bin? Thanks again for your help!
Because it's the path to route not your script, your script is in /usr/bin and route is in /sbin

Quote:
By the way, I get this error in email alert:

/usr/bin/tunroutetest.sh
SIOCADDRT: File exists
You'll get this error when your script runs and the route is still up.

You can either ignore the error or modify your script to check for the route first and then only try and create it if it's not already there.
 
Old 06-03-2015, 11:56 AM   #5
slionl43
LQ Newbie
 
Registered: Jun 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wildwizard View Post
Because it's the path to route not your script, your script is in /usr/bin and route is in /sbin



You'll get this error when your script runs and the route is still up.

You can either ignore the error or modify your script to check for the route first and then only try and create it if it's not already there.

Makes sense. Thank you! I modified my script with following to del route first and then add it because I dont know how to write a script to check route first (route -n) and only create it if its not already there. If you can point me in the right direction, that would be great! Thanks again for your help!

#!/bin/sh
/sbin/route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0
/sbin/route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0
 
Old 06-06-2015, 07:13 PM   #6
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
Checking the existence of a route gets a little messy, you are going to need to print the route to a variable, search the output for the address your looking for, tie that into an if statement to conditionally modify the existing route. However to just get rid of the error is simple, just add " 2>/dev/null". If it is still persistent then try "2>&1>/dev/null"

/sbin/route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0 2>/dev/null
/sbin/route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.120.1 dev eth0 2>/dev/null
 
  


Reply



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
route-eth - Adding a static route gbwien Linux - Networking 5 02-24-2012 05:15 AM
trouble adding static route to fedora 11 JSLayton Fedora 13 11-02-2011 11:25 PM
Adding a route to a static Host helptonewbie Linux - Newbie 6 02-16-2010 04:20 PM
Script check ICMP and auto change route static buff2l0 Linux - Networking 3 12-09-2009 02:05 AM
Adding static route in Mandrake 9.2 bloke Linux - Networking 1 02-23-2004 07:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 01:24 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