LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Issue with shell script (https://www.linuxquestions.org/questions/linux-networking-3/issue-with-shell-script-854086/)

zaeem 01-04-2011 04:19 AM

Issue with shell script
 
Dear All,

I have a shell script which adds route which is not working if called through /etc/rc.local file. The contents of scripts are;

#!/bin/bash
route add default gw 192.168.0.1 netmask 0.0.0.0 dev eth3

If I just execute above command on console then it puts route entry in routing table. I want to make this entry permanent. Please let me know what I am making wrong and how can I get this done.

Thanking you in advance.

andrewthomas 01-04-2011 05:10 AM

Have you made the file executable?

What distribution are you using?

Is the rc.local script if the correct location?

zaeem 01-04-2011 06:43 AM

Yes I have given permission as chmod a+x script.sh
I am using CentOS 5.5 64 bit
yes I have mentioned correct location of script in rc.local as "sh /root/script.sh"

Thanks for your help.

ComputerErik 01-04-2011 07:13 AM

If all you are trying to do is set the default gateway why not do so in the standard interface configuration scripts found under /etc/sysconfig/network-scripts/ifcfg-eth0?

Oliv' 01-04-2011 08:01 AM

Hello,

I agree with ComputerErik, the correct way would be to use standard scripts.
Anyway, if you do a script which is supposed to be run during boot sequence, always use full path, I mean you should specify "/sbin/route", because nothing guarantee you that PATH is correctly set during boot.

Regards,

Oliv'

zaeem 01-04-2011 12:11 PM

Dear Oliv,

Let me explain my requirement in detail :)

Actually my servers has two NICs and both NICs have been assigned IP addresses and default gateways. Linux by default picks only one default gateway and puts it in routing table which can be viewed in 'route -n' command. Now purpose of dual NICs and IPs is that if one NIC or even router gets down then traffic will be received using send NIC or router and after processing send traffic back to router.

I have defined default gateways of both NICs using scripts placed at /etc/sysconfig/network-scripts/ but only one default gateway is used by linux and if that gateways gets down then traffic can't be sent from the server. Now if I execute command route add default gw 192.168.0.1 netmask 0.0.0.0 dev eth3 then it puts second default gateway in routing table and this way I can achieve redundancy. I hope now my requirement is clear so please suggest me how can I automate above command on startup/boot time.

Oliv' 01-04-2011 02:38 PM

Ok, I think that this link may help you: http://www.cyberciti.biz/faq/setting...terfaces-file/
I think that if I were you, I would try to "play" with "post-down command" stuff to change the default gateway when an interface comes down. As I have never tried I cannot assert it will work, but it is from my point of view the cleanest way to proceed.

Cheers,

Oliv'

kamalkgarg 01-05-2011 04:32 AM

You can also configure static route and make it persistent.

Suppose you have two interfaces eth0 and eth1 and default gateway is configured for eth0. Then you can create a file route-eth1 in /etc/sysconfig/network-scripts and enter the following text...

"
GATEWAY0=<Gateway IP>
NETMASK0=<Netmask>
ADDRESS0=<Network Address>
"

Restart the network using "service network restart"
Execute "route -n" command to view the routes appearing.

- Kamal

zaeem 01-06-2011 04:39 AM

Dear Kamal,

This is not working for me. Any other suggestions?

zaeem 01-06-2011 04:40 AM

Dear Oliv,

The suggested link is for Debian and Ubuntu. It is not working for CentOS. Can you please give any other suggestion?

Oliv' 01-06-2011 09:56 AM

Sorry but unfortunately, I have never used Centos and barely never used Fedora (I think that they are quite similar)... So I cannot help you more.
Maybe have a look at /etc/sysconfig/network-scripts/ directory and ifup-routes file... the best would be to create a similar file named ifdown-routes to change your gateway when an interface bvrings down.

Oliv'


All times are GMT -5. The time now is 05:32 AM.