Created
August 13, 2012 10:34
-
-
Save chanux/3339247 to your computer and use it in GitHub Desktop.
Easily set routing table to allow the use of 2 different network connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This script helps you easily set routing tables to allow you use | |
# two different network connections. | |
# I use this to use My mobile broadband connection to access internet | |
# while retaining access to office network for internal stuff. | |
# Use 'route -n' to view current routing table. | |
#set your preferred defaults here | |
PRIMARY_GW="192.168.8.1" | |
SECONDARY_GW="192.168.7.1" | |
function usage(){ | |
echo "$0 --flush" | |
echo "$0 --set [primary gateway] [secondary gateway]" | |
exit 1 | |
} | |
function setroutes(){ | |
sudo route add -net 192.168.0.0 netmask 255.255.0.0 gw $2 | |
sudo route add default gw $1 | |
sudo route del default gw $2 | |
exit 0 | |
} | |
case "$1" in | |
-f|--flush) | |
sudo ip route flush table main | |
;; | |
-s|--set) | |
if [ $# == 3 ];then | |
PRIMARY_GW=$2 | |
SECONDARY_GW=$3 | |
fi | |
setroutes $PRIMARY_GW $SECONDARY_GW | |
;; | |
-h|--help|*) | |
usage | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment