Operating System - Linux
1820095 Members
3488 Online
109608 Solutions
New Discussion юеВ

Re: bash script to setup a route

 
SOLVED
Go to solution
'chris'
Super Advisor

bash script to setup a route

hi

I need a bash script running with root rights to setup a route, for example:

0.0.0.0 0.0.0.0 192.168.0.1

It should run on linux as a startup application.

THX
2 REPLIES 2
Matti_Kurkela
Honored Contributor
Solution

Re: bash script to setup a route

Most Linux distributions include configuration files you can use to specify custom routes. You probably don't need a script. (If such a configuration file is available, other sysadmins will expect to find custom routes there, not in arbitrary custom scripts. Being purposefully obscure only imperils your hard-earned vacation time.)

For example, on RedHat Enterprise Linux, if you need custom routes for e.g. network interface eth0, you can create a file named /etc/sysconfig/network-scripts/route-eth0 and write your route definition to it.

Please see:
http://www.cyberciti.biz/tips/configuring-static-routes-in-debian-or-red-hat-linux-systems.html

Furthermore, your example route has network 0/netmask 0, which means it's a default gateway route. All Linux distributions have a way to specify a default gateway without any need of custom scripting.

Even if you for some reason need a script, the script would just be an one-line "route" or "ip" command:

route add default gw 192.168.0.1
or
ip route add default via 192.168.0.1

The standard place for adding custom one-line commands to system startup would be the "rc.local" file, usually located in either /etc/rc.local or /etc/init.d/rc.local. It is automatically run as root at system startup.

MK
MK
'chris'
Super Advisor

Re: bash script to setup a route

Pls let explain our problem.
In the branch office we have 2 breakouts: 1 standard over MPLS and other one, just temporaily for some tests over DSL.
The default (standard) gateway for our branch clients is MPLS router and the standard internet breakout is normally over the same MPLS router.
We have ordered now DSL, just for some tests.
BTW It's quite difficult to change routings at MPLS or DSL router by the providers.
We try to change routings on the linux clients.

I've add these lines in: /etc/sudoers:

sta ALL=(root)NOPASSWD:/home/user/route.sh
sta ALL=(root)NOPASSWD:/sbin/route

and I've created this script, not tested yet:
---------------------------------------------------------
#!/bin/bash

# del existing default gw (MPLS router)
sudo route del default

# set DSL router as default gw
sudo route add default gw 192.168.0.1

# set route for the internal MPLS network
sudo route add 10.10.0.0 netmask 255.255.0.0 gw 10.10.50.1 dev eth0
------------------------------------------------------------------------------------------

Perhaps I should create another script to change everything back.
Both scripts should be run by a normal non-root user.