Last active
December 28, 2015 15:19
Part II of network scripts. Back-routing behing FW
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
#! /bin/bash | |
IFNAME=${1} | |
NIC_IPNR=$(ifconfig | \ | |
grep $IFNAME -A3 | \ | |
grep "inet addr" | \ | |
cut -f2 -d":" | \ | |
cut -f1 -d " ") | |
LNET=$(echo $NIC_IPNR | cut -f1-3 -d".").0 | |
GW=$(echo $NIC_IPNR | cut -f1-3 -d".").1 | |
source logger.sh | |
if [ "X$IFNAME" == "X" ]; then | |
logger "First argument (IFNAME) must be present. Exiting..." | |
exit 1 | |
fi | |
if [ "X$NIC_IPNR" == "X" ]; then | |
logger "IF [$IFNAME] not up or doesn't exist. Exiting..." | |
exit 1 | |
fi | |
logger "$(basename $0): Setting up return-route for IF: [$IFNAME]" | |
logger " NIC: [$NIC_IPNR]" | |
logger " NET: [$LNET]" | |
logger " GW: [$GW]" | |
#Check if there exist a specific table for the interface | |
EXIST_TABLE=$(cat /etc/iproute2/rt_tables | \ | |
grep -Ev '^#' | \ | |
grep $IFNAME) | |
OTHER_TABLE_IDS=$(cat /etc/iproute2/rt_tables | \ | |
grep -Ev '^#' | \ | |
grep -vE 'local|main|default|unspec' | \ | |
cut -f1 -d" " | \ | |
sort -n) | |
NEXT_ID=$(( $((sed -e 's/[[:space:]]\+/\n/g' | tail -n1) <<< $OTHER_TABLE_IDS) + 1)) | |
if [ "X${EXIST_TABLE}" == "X" ]; then | |
logger "Table missing for interface [$IFNAME]. Creating one as ID [$NEXT_ID]" | |
echo "$NEXT_ID $IFNAME" >> /etc/iproute2/rt_tables | |
fi | |
logger ">>> Table:" | |
cat /etc/iproute2/rt_tables | \ | |
grep -Ev '^#' | \ | |
grep $IFNAME | \ | |
logger | |
if [ "X$(ip route show table ${IFNAME})" == "X" ]; then | |
logger "Table is not set, setting" | |
ip route add $LNET/24 dev $IFNAME src $NIC_IPNR table $IFNAME | logger | |
ip route add default via $GW dev $IFNAME table $IFNAME | logger | |
fi | |
logger ">>> Routes in table [${IFNAME}]:" | |
ip route show table ${IFNAME} | logger | |
if [ "X$(ip rule | grep $IFNAME)" == "X" ]; then | |
logger "Rule is not set, settig" | |
ip rule add from $NIC_IPNR/32 table $IFNAME | logger | |
ip rule add to $NIC_IPNR/32 table $IFNAME | logger | |
fi | |
logger ">>> Rules fo table [${IFNAME}]:" | |
ip rule | grep $IFNAME | logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment