#!/bin/sh # # save routes.sh # Copyright (C) 2010 System-Linux.eu # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # PG_NAME=$0 PG_VERSION="0.1a" SAVE_DEFAULT_GW=1 print_help () { printf \ "Usage: %s [OPTION]\n\ Dump the current running routing table\n\n\ \t-n, --no-default-gw\tNot print the default gateway.\n\ \t-h, --help\t\tPrint help\n" $PG_NAME } die () { if [ "$1" ]; then echo -e $1 fi exit 1 } dump_using_nt () { which route >/dev/null || die if [ $SAVE_DEFAULT_GW -eq 1 ] ;then GW_CMD="cat" else GW_CMD="head -n -1" fi route -n | `echo $GW_CMD` | awk '($4 ~ /G/) { if ($3 == "255.255.255.255") { printf "route add -host %s metric %s gw %s\n", $1, $5, $2 } else printf "route add -net %s netmask %s metric %s gw %s\n", $1, $3, $5, $2 }' } for argv in $@ do case "$argv" in --version) echo "$PG_NAME version $PG_VERSION" exit 0 ;; -n | --no-default-gw) SAVE_DEFAULT_GW=0 ;; -h | --help) print_help exit 0 ;; *) print_help exit 1 ;; esac done dump_using_nt exit 0