Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm commanding a customarily built linux machine so not all linux commands are available. I execute network related commands so I need to set a default gateway right before I run my command, then remove that gateway. To do that I run all my commands in one line:

/sbin/route add default gw 10.10.10.10;my command;/sbin/route del default gw 10.10.10.10;

The problem is, for some reason I once found 2 default gateways on the same machine which caused all my commands to fail because even if I set my default gateway before running my test, its still messed up and can't run my test.

So is there a way to remove ALL default gateways in one command ? I have a large amount of machines that are increasing and it won't be practical to plant a script on every machine. I need a command as simple as the following:

/sbin/route del all default;set my default gw;mycommand;/sbin/route del all default;

I'll I have found so far is a command to remove a default gateway but not all of them /sbin/route del default which won't work for me.

/sbin/route help displays the following

/sbin/route --help

Usage: route [{add|del|delete}]

Edit the kernel's routing tables

Options:
        -n      Dont resolve names
        -e      Display other/more information
        -A inet Select address family

Thank you.

share|improve this question
add comment

4 Answers

If you use ip instead of route, you could simply run:

ip route del 0/0

I believe that catches all default routes.

share|improve this answer
    
Thank you but it still removes only one default gateway, not all. –  Muhammad Gelbana Dec 20 '12 at 19:48
3  
Then perhaps you should iterate through all default routes, and remove them, instead of assuming that your first remove removes all of them. It sounds like your code is breaking somehow and adding more than one default route. This is not a normal circumstance. –  jsbillings Dec 20 '12 at 19:50
    
I agree that my code breaks at some point but may be I can solve this quickly by fixing the GW thing instead of wasting a lot of time which I don't have. Thanks a lot for your help. –  Muhammad Gelbana Dec 20 '12 at 19:54
    
This is modern linux only. –  jordanm Dec 20 '12 at 20:19
add comment

To build up on @jsbillings's solution, repeat the command until it fails:

while ip route del default; do :; done
share|improve this answer
add comment

If your code breaks with multiple default gateways you should probably also look in multiple ip addresses per device.

As you received already an answer here is another approach as you should NEVER change the configuration of your system just because you want to run/test a script. How about using network namespaces instead. For a short introduction how to add virtual devices to your NS (namespace) have a look at http://code.google.com/p/coreemu/wiki/Namespaces.

To create a namespace named test and run a command under this namespace just run

ip netns add test
ip netns exec test COMMAND

where COMMAND can be for example your shell or just the server script.

share|improve this answer
add comment
up vote 0 down vote accepted

All the answers are great but I resolved this problem using a different approach, I used the command to add only one default gateway, but fail if there is already one. And thus eventually remove the wrong gateway at the end of the command. This should work on the second time isa.

ip route add default via my-gateway ip route del default

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.