Last active
October 25, 2018 07:43
-
-
Save rightson/6000ebd816c506a43a0ccf01f4959b05 to your computer and use it in GitHub Desktop.
A helper for simplifying and remembering the add/route default gateway commands
This file contains hidden or 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
#!/bin/bash | |
CONFIG=~/.route.conf | |
function usage() { | |
echo -e "Usage: `basename $0` <options>\noptions:" | |
grep "^function" $0 | sed 's/function/ /g' | sed 's/[{()]//g' | |
} | |
function list() { | |
route -n | |
} | |
function reset () { | |
echo "" > $CONFIG | |
} | |
function cache () { | |
cat $CONFIG | |
} | |
config () { | |
local op=$1 | |
local gateway=$2 | |
local interface=$3 | |
local metric=$4 | |
if [ ! -f $CONFIG ]; then | |
touch $CONFIG | |
fi | |
if [ "$gateway" = "" ]; then | |
gateway=$(grep gateway -A 1 $CONFIG | tail -1) | |
if [ "$gateway" = "" ] || [ "$gateway" = "[gateway]" ]; then | |
read -p "Enter gateway: " gateway | |
fi | |
fi | |
if [ "$interface" = "" ]; then | |
interface=$(grep interface -A 1 $CONFIG | tail -1) | |
if [ "$interface" = "" ] || [ "$interface" = "[interface]" ]; then | |
read -p "Enter interface: " interface | |
fi | |
fi | |
if [ "$metric" = "" ]; then | |
metric=$(grep metric -A 1 $CONFIG | tail -1) | |
if [ "$metric" = "" ] || [ "$metric" = "[metric]" ]; then | |
read -p "Enter metric: " metric | |
fi | |
fi | |
echo -e "[gateway]\n$gateway" > $CONFIG | |
echo -e "[interface]\n$interface" >> $CONFIG | |
echo -e "[metric]\n$metric" >> $CONFIG | |
local cmd="sudo route $op -net default gw $gateway netmask 0.0.0.0 dev $interface metric $metric" | |
echo -e "$cmd\n" | |
$cmd | |
} | |
function add() { | |
config add $@ | |
list | |
} | |
function del() { | |
config del $@ | |
list | |
} | |
if [ -z "$1" ]; then | |
usage | |
else | |
$@ | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment