check_net/netconfig.sh
2017-02-20 02:56:35 +01:00

994 lines
36 KiB
Bash
Executable File

#!/usr/bin/env bash
## -------------------------------------------------------------------
## --- All Configurations ill be done in /etc/check_net/check_net.conf
## -------------------------------------------------------------------
## - Load Configuration
## -
source /etc/check_net/check_net.conf
## ------------------
## --- Some functions
## ------------------
## - Check if a given array (parameter 2) contains a given string (parameter 1)
## -
fatal(){
echo ""
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m"
echo ""
echo "" >> $log_file
echo "[ Error ]: $*" >> $log_file
echo "" >> $log_file
echo " Script is canceled." >> $log_file
echo "" >> $log_file
exit 1
}
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
usage() {
echo
[ -n "$1" ] && echo -e "[ \033[1;31mError\033[m ] : $1\n"
echo -e " Usage:"
echo -e " \033[1m`basename $0` [OPTIONS] <device1> <device2> ..\033[m"
echo ""
echo -e " This script configures the default route, especially if more than one"
echo -e " route to the internet exists. Also the routing tables are managed by this"
echo -e " script."
echo ""
echo -e " The Parameter \033[1mdevice list\033[m contains all network devices, which should have"
echo -e " a connection to the Internet. Tha can be DSL lines as well as static lines."
echo -e " The declaration of the device list is mandatory."
echo ""
echo -e " \033[1mNotice\033[m"
echo -e " Declare the device list in the order of your preferred default gatway devices."
echo ""
echo -e " \033[1mNotice\033[m"
echo -e " On static line devices \033[1mappend \":<gateway>\033[m. This is very important,"
echo -e " otherwise this script will \033[1mNOT work as expected\033[m."
echo ""
echo -e " If this script is invoked with option \033[1m-m\033[m, another script called \033[1m`basename $check_script`\033[m"
echo -e " will be triigered to monitor the devices and informs about changes (online/offline"
echo -e " status) of the given devices. If the status of a line has changed, this script"
echo -e " is reinvoked by the monitoring script \033[1m`basename $check_script`\033[m to reconfigure"
echo -e " the routing."
echo ""
echo -e " Options:"
echo ""
echo -e " \033[1m-h\033[m"
echo -e " Prints this help\033[m"
echo ""
echo -e " \033[1m-l <list of online devices>\033[m"
echo -e " List of all (internet) devices known as online. Usually, this option will"
echo -e " be used by triggering this script from check script \033[1m`basename $check_script`\033[m."
echo ""
echo -e " \033[1m-m\033[m"
echo -e " Activates monitoring of the given network devices."
echo ""
echo -e " Example:"
echo -e " - Simply configure routing for devices \"$_INITIAL_DEVICE_LIST\""
echo -e " \033[1m`basename $0` $_INITIAL_DEVICE_LIST\033[m"
echo ""
echo -e " - Configure routing for devices \"$_INITIAL_DEVICE_LIST\" and activate monitoring"
echo -e " \033[1m`basename $0` -m $_INITIAL_DEVICE_LIST\033[m"
echo ""
exit 1
}
if [[ ! -f "$check_script" ]] ; then
fatal "Check script \033[1m$check_script\033[m not found!"
fi
if [[ "`which sipcalc`" == "" ]]; then
fatal "\033[1msipcalc\033[m must be installed to run this script"
fi
if [[ $EUID -ne 0 ]]; then
fatal "This script must be run as root" 1>&2
fi
## ---
## --- Configure
## ---
_monitoring=false
ONLINE_DEVICE_LIST=
while getopts hl:m opt ; do
case $opt in
h) usage
;;
l) ONLINE_DEVICE_LIST=$OPTARG
;;
m) _monitoring=true
;;
esac
done
shift `expr $OPTIND - 1`
INITIAL_DEVICE_LIST="$@"
if [[ -z "$INITIAL_DEVICE_LIST" ]]; then
INITIAL_DEVICE_LIST=$_INITIAL_DEVICE_LIST
fi
[[ -z "$INITIAL_DEVICE_LIST" ]] && usage "No device list given"
## - Define (non associative) array
## -
declare -a inet_devices_arr
declare -a dsl_devices_arr
declare -a static_devices_arr
declare -a online_devices_arr
declare -A static_gw_arr
for _device in $INITIAL_DEVICE_LIST ; do
if [[ "$_device" =~ : ]]; then
static_gateway="${_device##*:}"
_device="${_device%:*}"
static_gw_arr[$_device]="$static_gateway"
static_devices_arr+=("$_device")
else
dsl_devices_arr+=("$_device")
fi
inet_devices_arr+=("$_device")
done
for _online_device in $ONLINE_DEVICE_LIST ; do
online_devices_arr+=("$_online_device")
done
#echo "All Devices:"
#for _device in "${inet_devices_arr[@]}" ; do
# echo -e "\t$_device"
#done
#echo "Online Devices:"
#for _device in "${online_devices_arr[@]}" ; do
# echo -e "\t$_device"
#done
#
#for inet_device in "${inet_devices_arr[@]}" ; do
# if [ -n "$ONLINE_DEVICE_LIST" ]; then
# if ! containsElement "$inet_device" "${online_devices_arr[@]}" ; then
# echo "$inet_device is offline"
# continue
# fi
# fi
#done
#
#echo ""
#exit
## - Define associative arrays
## -
declare -A default_gw_arr
declare -A gw_connection_arr
declare -i number_rt_table=0
## ---
## --- Start
## ---
#echo "" >> $log_file
#echo "" >> $log_file
#echo "#############################" >> $log_file
#echo "### ---" >> $log_file
#echo "### --- [ `date +'%Y-%m-%d %H:%M'` ]: Starting Script `basename $0`.." >> $log_file
#echo "### --- Devices all: $INITIAL_DEVICE_LIST" >> $log_file
#echo "### --- Devices online: $ONLINE_DEVICE_LIST" >> $log_file
#echo "### ---" >> $log_file
#echo "### ---" >> $log_file
#echo "#############################" >> $log_file
echo "" >> $log_file
echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Starting Script `basename $0`.." >> $log_file
echo " Devices all: $INITIAL_DEVICE_LIST" >> $log_file
echo " Devices online: $ONLINE_DEVICE_LIST" >> $log_file
configured=false
if $_monitoring ; then
max_attempts=20
else
max_attempts=1
fi
declare -i _try_number=0
declare -i prio=0
while ! $configured && [ $_try_number -lt $max_attempts ] ; do
let _try_number++
if [ $_try_number -gt 1 ]; then
echo "" >> $log_file
echo "# --- sleeping 2 seconds before attempt number $_try_number" >> $log_file
sleep 2
fi
number_rt_table=0
#for inet_device in "${dsl_devices_arr[@]}" ; do
for inet_device in "${inet_devices_arr[@]}" ; do
## - Create routing table name
## -
if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then
__name=`echo $inet_device | cut -d '-' -f2`
rt_name="dsl_$__name"
else
__name=`echo $inet_device | cut -d '-' -f1`
rt_name="static_$__name"
fi
if ! $_monitoring ; then
## - Check if device was reported (from check script) as offline
## -
_offline=false
if [ -n "$ONLINE_DEVICE_LIST" ]; then
if ! containsElement "$inet_device" "${online_devices_arr[@]}" ; then
_offline=true
fi
else
_offline=true
fi
## - Cleanup routing tables
## -
if $_offline ; then
if $LOGGING_CONSOLE ; then
echo -e "\n\t[ Error ]: Connection at interface \"$inet_device\" was reported to be down !"
echo -e "\t So device \"$inet_device\" will be excluded from routing."
fi
echo -e "\n\t[ Error ]: Connection at interface \"$inet_device\" was reported to be down !" >> $log_file
echo -e "\t So device \"$inet_device\" will be excluded from routing." >> $log_file
## - Delete all existing entries of this routing table
## -
echo "" >> $log_file
echo "## - Delete all existing entries of routing table \"$rt_name\"" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route flush table $rt_name" >> $log_file
/sbin/ip route flush table $rt_name >> $log_file 2>&1
if /sbin/ip rule | grep $rt_name > /dev/null 2>&1 ; then
## - Delete all rules concerning table $rt_name
## -
echo "" >> $log_file
echo "## - Delete all rules concerning routing table $rt_name" >> $log_file
echo "## -" >> $log_file
while read line ; do
direction=`echo $line | awk '{print$2}'`
ip=`echo $line | awk '{print$3}'`
echo "/sbin/ip rule delete $direction $ip table $rt_name" >> $log_file
/sbin/ip rule delete $direction $ip table $rt_name
done < <(/sbin/ip rule | grep $rt_name)
echo "" >> $log_file
fi # End: if /sbin/ip rule | grep $rt_name > /dev/null 2>&1
continue
fi # End: if $_offline
fi # End: if ! $_monitoring ; then
let number_rt_table="$number_rt_table+100"
prio=0
## - Add new routing table to /etc/iproute2/rt_tables
## - if not yet exists
## -
if ! grep $rt_name /etc/iproute2/rt_tables > /dev/null 2>&1 ; then
echo "" >> $log_file
echo "## - Add new routing table to /etc/iproute2/rt_tables" >> $log_file
echo "## -" >> $log_file
echo "echo \"$number_rt_table $rt_name\" >> /etc/iproute2/rt_tables" >> $log_file
echo -e "$number_rt_table\t$rt_name" >> /etc/iproute2/rt_tables
fi
## - Is the device present and has local Address ?
## -
local_gw_address=`ifconfig $inet_device 2> /dev/null | grep "inet Adresse" | cut -d":" -f2 | cut -d" " -f1`
if [ -z $local_gw_address ]; then
if $LOGGING_CONSOLE ; then
echo -e "\n\t[ Error ]: Connection at interface \"$inet_device\" seems to be down !"
echo -e "\t No local address was found."
fi
echo -e "\n\t[ Error ]: Connection at interface \"$inet_device\" seems to be down !" >> $log_file
echo -e "\t No local address was found." >> $log_file
## - Cleanup routing tables
## -
## - Delete all existing entries of this routing table
## -
echo "" >> $log_file
echo "## - Delete all existing entries of this routing table" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route flush table $rt_name" >> $log_file
/sbin/ip route flush table $rt_name >> $log_file 2>&1
if /sbin/ip rule | grep $rt_name > /dev/null 2>&1 ; then
## - Delete all rules concerning table $rt_name
## -
echo "" >> $log_file
echo "## - Delete all rules concerning routing table $rt_name" >> $log_file
echo "## -" >> $log_file
while read line ; do
direction=`echo $line | awk '{print$2}'`
ip=`echo $line | awk '{print$3}'`
echo "/sbin/ip rule delete $direction $ip table $rt_name" >> $log_file
/sbin/ip rule delete $direction $ip table $rt_name
done < <(/sbin/ip rule | grep $rt_name)
echo "" >> $log_file
fi
continue
fi # End: if [ -z $local_gw_address ]
## - Is the DSL-device known and has remote Address ?
## -
if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then
remote_gw_address=`ifconfig $inet_device 2> /dev/null | grep "inet Adresse" | cut -d":" -f3 | cut -d" " -f1`
remote_gw_net="$remote_gw_address/32"
else
net_address=`sipcalc $inet_device 2> /dev/null | grep -i -e "^network\s*address\s*-" | awk '{print$4}'`
remote_gw_address=${static_gw_arr[$inet_device]}
_netmask_bits=`sipcalc $inet_device 2> /dev/null | grep -i -e "Network\s*mask\s*(bits)" | awk '{print$5}'`
remote_gw_net="${net_address}/$_netmask_bits"
fi
if [ -z $remote_gw_address ]; then
if $LOGGING_CONSOLE ; then
echo -e "\n\t[ Error ]: Connection at interface \"$inet_device\" seems to be down !"
echo -e "\t No remote gateway was found."
fi
echo -e "\n\t[ Error ]: Connection at interface \"$inet_device\" seems to be down !" >> $log_file
echo -e "\t No remote gateway was found." >> $log_file
## - Cleanup routing tables
## -
## - Delete all existing entries of this routing table
## -
echo "" >> $log_file
echo "## - Delete all existing entries of this routing table" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route flush table $rt_name" >> $log_file
/sbin/ip route flush table $rt_name >> $log_file 2>&1
if /sbin/ip rule | grep $rt_name > /dev/null 2>&1 ; then
## - Delete all rules concerning table $rt_name
## -
echo "" >> $log_file
echo "## - Delete all rules concerning routing table $rt_name" >> $log_file
echo "## -" >> $log_file
while read line ; do
direction=`echo $line | awk '{print$2}'`
ip=`echo $line | awk '{print$3}'`
echo "/sbin/ip rule delete $direction $ip table $rt_name" >> $log_file
/sbin/ip rule delete $direction $ip table $rt_name
done < <(/sbin/ip rule | grep $rt_name)
echo "" >> $log_file
fi
continue
fi
## - Device already configured by that script?
## -
if [ ${default_gw_arr[$inet_device]+_} ] ; then
continue
fi
# -
# - Ready to start configuration for that device
# -
echo "" >> $log_file
echo "# ---" >> $log_file
if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then
echo "# --- Routing Table for (DSL) network device \"$inet_device\" was created" >> $log_file
else
echo "# --- Routing Table for (static line) network device \"$inet_device\"" >> $log_file
fi
echo "# ---" >> $log_file
if $LOGGING_CONSOLE ; then
echo
echo
if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then
echo -e "\t--- DSL Connection at interface $inet_device"
else
echo -e "\t--- Static Connection at interface $inet_device"
fi
echo -e "\t---"
echo -e "\tRouting Table Name..: $rt_name"
echo
echo -e "\tInterface...........: $inet_device"
echo
echo -e "\tLocal GW address....: $local_gw_address"
echo -e "\tRemote GW address...: $remote_gw_address"
echo -e "\tRemote network......: $remote_gw_net"
echo
fi
echo "# --- Routing Table Name..: $rt_name" >> $log_file
echo "# --- " >> $log_file
echo "# --- Interface...........: $inet_device" >> $log_file
echo "# --- " >> $log_file
echo "# --- Local GW address....: $local_gw_address" >> $log_file
echo "# --- Remote GW address...: $remote_gw_address" >> $log_file
echo "# --- Remote network......: $remote_gw_net" >> $log_file
echo "# --- " >> $log_file
## - Read routing table from output of "netstat -rn"
## -
routing_table_main_arr=()
while read _destination _gateway _genmask _flags _mss _window _irtt _iface; do
if [ "$_destination" = "Destination" -o "$_destination" = "Kernel" \
-o "$_destination" = "Ziel" -o "$_destination" = "Kernel-IP-Routentabelle" ]; then
continue
fi
routing_table_main_arr+=("$_destination $_gateway $_genmask $_iface")
done < <(netstat -rn)
## - First delete all existing entries of this routing table
## -
echo "" >> $log_file
echo "## - First delete all existing entries of this routing table" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route flush table $rt_name" >> $log_file
/sbin/ip route flush table $rt_name >> $log_file 2>&1
## - Add loopback device to routing table $rt_name
## -
echo "" >> $log_file
echo "## - Add loopback device to routing table $rt_name " >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route add 127.0.0.0/8 dev lo table table $rt_name" >> $log_file
/sbin/ip route add 127.0.0.0/8 dev lo table $rt_name >> $log_file 2>&1
## - Add routing tables of all (local) network interfaces
## -
echo "" >> $log_file
echo "## - Add routing tables of all (local) network interfaces" >> $log_file
echo "## -" >> $log_file
for _entry in "${routing_table_main_arr[@]}" ; do
dest=`echo $_entry | cut -d " " -f1`
gateway=`echo $_entry | cut -d " " -f2`
genmask=`echo $_entry | cut -d " " -f3`
iface=`echo $_entry | cut -d " " -f4`
## - We will set default route later..
## -
if [ "$dest" = "0.0.0.0" ]; then
continue
fi
## - Is this a "ppp"-device ?
## -
if [[ "$iface" =~ "ppp" ]]; then
continue
fi
if [ "$dest" = "$remote_gw_address" ]; then
continue
fi
if [ "$gateway" = "0.0.0.0" ]; then
echo "/sbin/ip route add ${dest}/$genmask dev $iface table $rt_name" >> $log_file
/sbin/ip route add ${dest}/$genmask dev $iface table $rt_name >> $log_file 2>&1
else
echo "/sbin/ip route add ${dest}/$genmask via $gateway table $rt_name" >> $log_file
/sbin/ip route add ${dest}/$genmask via $gateway table $rt_name >> $log_file 2>&1
fi
done
## - Add this connection to the routing table
## -
echo "" >> $log_file
echo "## - Add this connection to the routing table $rt_name" >> $log_file
echo "## -" >> $log_file
if $USE_REMOTE_GATEWAY_ADDRESS ; then
## - Remote Network: $remote_gw_net
## -
echo "/sbin/ip route add $remote_gw_net dev $inet_device src $local_gw_address table $rt_name" >> $log_file
/sbin/ip route add $remote_gw_net dev $inet_device src $local_gw_address table $rt_name >> $log_file 2>&1
else
## - Remote Network: 0.0.0.0
## -
echo "/sbin/ip route add 0.0.0.0 dev $inet_device src $local_gw_address table $rt_name" >> $log_file
/sbin/ip route add 0.0.0.0 dev $inet_device src $local_gw_address table $rt_name >> $log_file 2>&1
fi
if $SET_MULTIPLE_DEFAULT_GW ; then
if /sbin/ip route show table main | grep -e "^$remote_gw_address" | grep $inet_device > /dev/null 2>&1 ; then
echo "" >> $log_file
echo "## - Delete route via (dsl remote) host $remote_gw_address" >> $log_file
echo "## -"
echo "/sbin/ip route delete $remote_gw_address dev $inet_device" >> $log_file
/sbin/ip route delete $remote_gw_address dev $inet_device >> $log_file 2>&1
fi
echo "" >> $log_file
echo "## - Add this connection also to the main routing table" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route add $remote_gw_net dev $inet_device src $local_gw_address" >> $log_file
/sbin/ip route add $remote_gw_net dev $inet_device src $local_gw_address >> $log_file 2>&1
fi
## - Remeber that route in order to add it to the routing table
## - of other connections
## -
gw_connection_arr[$inet_device]="$remote_gw_net $local_gw_address"
## - Add the connections associated gateway as default gateway for this
## - routing table
## -
echo "" >> $log_file
echo "## - Add the connections associated gateway as default gateway for this" >> $log_file
echo "## - routing table" >> $log_file
echo "## -" >> $log_file
if $USE_REMOTE_GATEWAY_ADDRESS ; then
## - Default Gatway: $remote_gw_address
## -
#echo "/sbin/ip route add default via $remote_gw_address dev $inet_device table $rt_name" >> $log_file
#/sbin/ip route add default via $remote_gw_address dev $inet_device table $rt_name >> $log_file 2>&1
echo "/sbin/ip route add default via $remote_gw_address table $rt_name" >> $log_file
/sbin/ip route add default via $remote_gw_address table $rt_name >> $log_file 2>&1
else
## - Default Gatway: 0.0.0.0
## -
echo "/sbin/ip route add default via 0.0.0.0 dev $inet_device table $rt_name" >> $log_file
/sbin/ip route add default via 0.0.0.0 dev $inet_device table $rt_name >> $log_file 2>&1
fi
## - Make sure that a reply goes out over the same connection as came in
## -
echo "" >> $log_file
echo "## - Make sure that a reply goes out over the same connection as came in" >> $log_file
echo "## -" >> $log_file
if ! /sbin/ip rule | grep "from $local_gw_address" > /dev/null 2>&1 ; then
let prio="$number_rt_table"
echo "/sbin/ip rule add from $local_gw_address table $rt_name prio $prio" >> $log_file
/sbin/ip rule add from $local_gw_address table $rt_name prio $prio >> $log_file 2>&1
#let prio="10+$prio"
#echo "/sbin/ip rule add to $local_gw_address table $rt_name prio $prio" >> $log_file
#/sbin/ip rule add to $local_gw_address table $rt_name prio $prio >> $log_file 2>&1
else
let prio="1010+$number_rt_table"
echo -e "#\t[ info ]: Rule already exists.." >> $log_file
fi
## ---
## --- Special Routing (local) IP-Address OUT
## ---
if [[ ${#rule_local_ip_arr[@]} -gt 0 ]] ; then
let prio="1000+${number_rt_table}+10"
for _val in "${rule_local_ip_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
if [[ "${_val_arr[0]}" = "$inet_device" ]]; then
echo "" >> $log_file
echo "## - Rule ${prio}: from ${_val_arr[1]} through ${_val_arr[0]}" >> $log_file
echo "## -" >> $log_file
if ! /sbin/ip rule | grep "from ${_val_arr[1]} " > /dev/null 2>&1 ; then
echo "/sbin/ip rule add from ${_val_arr[1]} table $rt_name prio $prio" >> $log_file
/sbin/ip rule add from ${_val_arr[1]} table $rt_name prio $prio >> $log_file 2>&1
else
echo "# Rule already exists" >> $log_file
fi
prio="10+$prio"
fi
done
fi
## ---
## --- Special Routing (remote) Services
## ---
if [[ ${#rule_remote_ip_arr[@]} -gt 0 ]] ; then
let prio="5000+${number_rt_table}+10"
for _val in "${rule_remote_ip_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
if [[ "${_val_arr[0]}" = "$inet_device" ]]; then
echo "" >> $log_file
echo "## - Rule ${prio}: to ${_val_arr[1]} through ${_val_arr[0]}" >> $log_file
echo "## -" >> $log_file
if ! /sbin/ip rule | grep "to ${_val_arr[1]} " > /dev/null 2>&1 ; then
echo "/sbin/ip rule add to ${_val_arr[1]} table $rt_name prio $prio" >> $log_file
/sbin/ip rule add to ${_val_arr[1]} table $rt_name prio $prio >> $log_file 2>&1
else
echo "# Rule already exists" >> $log_file
fi
prio="10+$prio"
fi
done
fi
## ---
## --- Special Routing Networks
## ---
if [[ ${#rule_local_net_arr[@]} -gt 0 ]] ; then
let prio="10000+${number_rt_table}+10"
for _val in "${rule_local_net_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
if [[ "${_val_arr[0]}" = "$inet_device" ]]; then
echo "" >> $log_file
echo "## - Rule ${prio}: from ${_val_arr[1]} through ${_val_arr[0]}" >> $log_file
echo "## -" >> $log_file
if ! /sbin/ip rule | grep "from ${_val_arr[1]} " > /dev/null 2>&1 ; then
echo "/sbin/ip rule add from ${_val_arr[1]} table $rt_name prio $prio" >> $log_file
/sbin/ip rule add from ${_val_arr[1]} table $rt_name prio $prio >> $log_file 2>&1
else
echo "# Rule already exists" >> $log_file
fi
prio="10+$prio"
fi
done
fi
## - Add this connection to the routing tables of other already configured dsl-connections
## -
## - Note:
## - Connections which will be configured later at this loop will
## - not have that connection in their routing tables. So you have
## - to add missing routes at the end (after that loop has finisched).
## -
## - _key is eqal to the ppp-device
## -
for _key in "${!gw_connection_arr[@]}"; do
if containsElement "$_key" "${dsl_devices_arr[@]}" ; then
__name=`echo $_key | cut -d '-' -f2`
_rt_name="dsl_$__name"
else
__name=`echo $_key | cut -d '-' -f1`
_rt_name="static_$__name"
fi
if [[ "$_rt_name" == "$rt_name" ]]; then
continue
fi
_local_gw_address=`echo ${gw_connection_arr[$_key]} | cut -d " " -f2`
_remote_gw_net=`echo ${gw_connection_arr[$_key]} | cut -d " " -f1`
echo "" >> $log_file
echo "## - Add this connection to the routing table \"$_rt_name\"" >> $log_file
echo "## -" >> $log_file
if $USE_REMOTE_GATEWAY_ADDRESS ; then
## - Remote Network: $_remote_gw_net
## -
if ! /sbin/ip route show table $_rt_name | egrep "^$_remote_gw_net\s+dev\s+$_key" >/dev/null 2>&1 ; then
_remote_gw=`echo $_remote_gw_net | cut -d "/" -f1`
if ! /sbin/ip route show table $_rt_name | egrep "^$_remote_gw\s+dev\s+$_key" >/dev/null 2>&1 ; then
echo "/sbin/ip route add $_remote_gw_net dev $_key src $_local_gw_address table $_rt_name" >> $log_file
/sbin/ip route add $_remote_gw_net dev $_key src $_local_gw_address table $_rt_name >> $log_file 2>&1
else
echo -e "#\t[ info ]: Connection through $_key is already part of table $_rt_name" >> $log_file
fi
fi
else
## - Remote Network: 0.0.0.0
## -
if ! /sbin/ip route show table $_rt_name | egrep "^0.0.0.0\s+dev\s+$_key" >/dev/null 2>&1 ; then
_remote_gw=`echo $_remote_gw_net | cut -d "/" -f1`
if ! /sbin/ip route show table $_rt_name | egrep "^0.0.0.0\s+dev\s+$_key" >/dev/null 2>&1 ; then
echo "/sbin/ip route add 0.0.0.0 dev $_key src $_local_gw_address table $_rt_name" >> $log_file
/sbin/ip route add 0.0.0.0 dev $_key src $_local_gw_address table $_rt_name >> $log_file 2>&1
else
echo -e "#\t[ info ]: Connection through $_key is already part of table $_rt_name" >> $log_file
fi
fi
fi
done
## - Add this gateway data to the array concerning all outgoing gatways
## -
#default_gw_arr[$inet_device]="$local_gw_address"
default_gw_arr[$inet_device]="$remote_gw_address"
if $SET_MULTIPLE_DEFAULT_GW ; then
default_gw_arg=""
for _key in "${!default_gw_arr[@]}"; do
if $USE_DEFAULT_GW_ADDRESS ; then
## - Default Gateway: $remote_gw_address
## -
default_gw_arg="$default_gw_arg nexthop via ${default_gw_arr[$_key]} dev $_key weight 1"
else
## - Default Gateway: 0.0.0.0
## -
default_gw_arg="$default_gw_arg nexthop via 0.0.0.0 dev $_key weight 1"
fi
done
if [ -n "$default_gw_arg" ] ; then
echo "" >> $log_file
echo "## - Add multiple default gateways" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route delete default" >> $log_file
/sbin/ip route delete default >> $log_file 2>&1
echo "/sbin/ip route add default scope global $default_gw_arg" >> $log_file
/sbin/ip route add default scope global $default_gw_arg >> $log_file 2>&1
else
echo "" >> $log_file
echo "## -" >> $log_file
echo "## - [ Warning]: No default gateway found!" >> $log_file
echo "## -" >> $log_file
fi
fi
## - Notice:
## - It is possible to first make a number of changes and then flush
## - the cache so that all of the changes will be implemented simultaneously.
## - This is actually convenient when working on an active router.
## -
echo "" >> $log_file
echo "## - Flush table cache" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route flush table cache" >> $log_file
/sbin/ip route flush table cache >> $log_file 2>&1
echo "" >> $log_file
if [ ${#default_gw_arr[@]} -eq ${#inet_devices_arr[@]} ]; then
configured=true
fi
done
done
## - Some dsl-connections maybe not known to all routing tables. So add
## - the missing routes to the appropriate tables..
## -
echo "" >> $log_file
echo "" >> $log_file
echo "## - Some dsl-connections maybe not known to all routing tables. So add" >> $log_file
echo "## - the missing routes to the appropriate tables.." >> $log_file
echo "## -" >> $log_file
_changed=false
if $USE_REMOTE_GATEWAY_ADDRESS ; then
## - Remote Network: $_remote_gw_net
## -
for _key in "${!gw_connection_arr[@]}"; do
if containsElement "$_key" "${dsl_devices_arr[@]}" ; then
__name=`echo $_key | cut -d '-' -f2`
_rt_name="dsl_$__name"
else
__name=`echo $_key | cut -d '-' -f1`
_rt_name="static_$__name"
fi
echo "# Routing Table \"$_rt_name\"" >> $log_file
for __key in "${!gw_connection_arr[@]}"; do
_local_gw_address=`echo ${gw_connection_arr[$__key]} | cut -d " " -f2`
_remote_gw_net=`echo ${gw_connection_arr[$__key]} | cut -d " " -f1`
if ! /sbin/ip route show table $_rt_name | egrep "^$_remote_gw_net\s+dev\s+$__key" >/dev/null 2>&1 ; then
_remote_gw=`echo $_remote_gw_net | cut -d "/" -f1`
if ! /sbin/ip route show table $_rt_name | egrep "^$_remote_gw\s+dev\s+$__key" >/dev/null 2>&1 ; then
#echo "/sbin/ip route add $_remote_gw_net dev $_key src $_local_gw_address table $_rt_name" >> $log_file
#/sbin/ip route add $_remote_gw_net dev $__key src $_local_gw_address table $_rt_name >> $log_file 2>&1
echo "/sbin/ip route add $_remote_gw dev $__key table $_rt_name" >> $log_file
/sbin/ip route add $_remote_gw dev $__key table $_rt_name >> $log_file 2>&1
_changed=true
else
echo -e "#\t[ info ]: Connection through $__key is already part of table $_rt_name" >> $log_file
fi
fi
done
done
else
## - Remote Network: 0.0.0.0
## -
for _key in "${!gw_connection_arr[@]}"; do
if containsElement "$_key" "${dsl_devices_arr[@]}" ; then
__name=`echo $_key | cut -d '-' -f2`
_rt_name="dsl_$__name"
else
__name=`echo $_key | cut -d '-' -f1`
_rt_name="static_$__name"
fi
echo "# Routing Table \"$_rt_name\"" >> $log_file
for __key in "${!gw_connection_arr[@]}"; do
_local_gw_address=`echo ${gw_connection_arr[$__key]} | cut -d " " -f2`
_remote_gw_net=`echo ${gw_connection_arr[$__key]} | cut -d " " -f1`
if ! /sbin/ip route show table $_rt_name | egrep "^0.0.0.0\s+dev\s+$__key" >/dev/null 2>&1 ; then
_remote_gw=`echo $_remote_gw_net | cut -d "/" -f1`
if ! /sbin/ip route show table $_rt_name | egrep "^0.0.0.0\s+dev\s+$__key" >/dev/null 2>&1 ; then
echo "/sbin/ip route add 0.0.0.0 dev $_key src $_local_gw_address table $_rt_name" >> $log_file
/sbin/ip route add 0.0.0.0 dev $__key src $_local_gw_address table $_rt_name >> $log_file 2>&1
_changed=true
else
echo -e "#\t[ info ]: Connection through $__key is already part of table $_rt_name" >> $log_file
fi
fi
done
done
fi
## - If not using multiple default gatways, set the default gateway here
## -
if ! $SET_MULTIPLE_DEFAULT_GW ; then
__set_default_gatway=false
echo "" >> $log_file
echo "" >> $log_file
echo "## ---" >> $log_file
echo "## --- Add default gateway" >> $log_file
echo "## ---" >> $log_file
## - Note: the first online device will become default route
## -
for _device in "${inet_devices_arr[@]}" ; do
## - Device online ?
if [ -n "${default_gw_arr[$_device]}" ]; then
echo "/sbin/ip route delete default" >> $log_file
/sbin/ip route delete default >> $log_file 2>&1
if $USE_REMOTE_GATEWAY_ADDRESS ; then
echo "/sbin/ip route add default via ${default_gw_arr[$_device]} dev $_device" >> $log_file
/sbin/ip route add default via ${default_gw_arr[$_device]} dev $_device >> $log_file 2>&1
else
echo "/sbin/ip route add default via 0.0.0.0 dev $_device" >> $log_file
/sbin/ip route add default via 0.0.0.0 dev $_device >> $log_file 2>&1
fi
__set_default_gatway=true
_changed=true
break
else
echo "" >> $log_file
echo -e "\t[ Warning ]: $_device is OFFLINE ! Trying next.." >> $log_file
fi
done
if ! $__set_default_gatway ; then
echo "" >> $log_file
echo -e "\t[ Error ]: No connection is online!" >> $log_file
echo -e "\t Try to set default gateway from an existing static line .." >> $log_file
## - Notice:
## -
## - If no connection is available (the machine is fully offline), the check script will not
## - recognize, if the static line becomes online. A way to handle this is to let the
## - default gateway active.
## -
default_gw_deleted=false
for _device in "${inet_devices_arr[@]}" ; do
if containsElement "$_device" "${static_devices_arr[@]}" ; then
## - Delete old default route
## -
if ! $default_gw_deleted ; then
echo "" >> $log_file
echo "## - Delete existing default gatewy" >> $log_file
echo "## - " >> $log_file
echo "/sbin/ip route delete default" >> $log_file
/sbin/ip route delete default >> $log_file 2>&1
default_gw_deleted=true
fi
## - Set new default route
## -
echo "" >> $log_file
echo "## - Try to set default gateway to ${static_gw_arr[$_device]}.." >> $log_file
echo "## - " >> $log_file
echo "/sbin/ip route add default via ${static_gw_arr[$_device]} dev $_device" >> $log_file
/sbin/ip route add default via ${static_gw_arr[$_device]} dev $_device >> $log_file 2>&1
if [[ "$?" == 0 ]] ; then
__set_default_gatway=true
break
fi
fi
done
if ! $__set_default_gatway ; then
echo "" >> $log_file
echo -e "\t[ Error ]: No default gateway is set!" >> $log_file
fi
fi
fi
## - Flush the routing tables cache if somethimg has changed
## -
if $_changed ; then
echo "" >> $log_file
echo "" >> $log_file
echo "## - Some Routing tables has changed, so flush table cache" >> $log_file
echo "## -" >> $log_file
echo "/sbin/ip route flush table cache" >> $log_file
/sbin/ip route flush table cache >> $log_file 2>&1
fi
if $_monitoring ; then
echo "" >> $log_file
echo "" >> $log_file
echo "## - Starting monitoring script to check dsl connections.." >> $log_file
echo "## -" >> $log_file
if [[ -z "${!default_gw_arr[@]}" ]] ; then
echo "$check_script $INITIAL_DEVICE_LIST &" >> $log_file 2>&1
$check_script $INITIAL_DEVICE_LIST &
else
_LIST=
for _device in ${!default_gw_arr[@]} ; do
_LIST="$_LIST $_device"
done
_LIST=`echo "${_LIST}" | sed -e 's/^[ \t]*//'`
echo "$check_script -l \"$_LIST\" $INITIAL_DEVICE_LIST &" >> $log_file 2>&1
$check_script -l "$_LIST" $INITIAL_DEVICE_LIST &
fi
fi
echo "" >> $log_file
echo "### -------------------------" >> $log_file
exit 0