custom shell script to adding static route does not work
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.