Cover V11, I09
sep2002.tar

Listing 3 Realserver routing script

#!/bin/sh

# This script is designed to alter the network routing table on a
# real-server node in an LVS load balancing cluster. It assumes for
# the moment that there is only one network interface on each of the
# LVS real-servers, as well as the director.

# Each real-server must be configured such that the director is the
# gateway between the real-server and the rest of the network. This
# means that the director must be the default, and only, route for
# network traffic in and out of the LVS cluster. The requires that one
# of the routes created when a real-server is powered on be deleted.

NET_INTERFACE=0

get_network() {

IP=$1
MASK=$2

#Parse the netmask into 4 separate octets
if [ -n "$(echo $MASK | /bin/grep \\.)" ]; then
j=1
for i in $(echo $MASK|/bin/sed s/\\./\ /g); do
local MK_$j=$i
j=$(($j+1))
done
elif [ $(echo $MASK|/usr/bin/cut -b 1-2) = "0x" ]; then
#In order to maintain platform portability, it is necessary to
#convert the netmask from hex to decimal. For some reason, certain
#shells cannot bitwise AND hex numbers, while others can.

#This conversion is probably not the most efficient, but it does work.
for i in 1 2 3 4; do
MASK=$(echo $MASK| /usr/bin/cut -b 3-)

tmp1=0
for j in 1 2; do
tmp=$(echo $MASK|/usr/bin/cut -b $j)
[ $j -eq 1 ] && k=16 || k=1

case $tmp in
[0-9])
tmp1=$(($tmp1 + (($k*$tmp))))
;;
a|A)
tmp1=$(($tmp1 + (($k*$tmp))))
;;
b|B)
tmp1=$(($tmp1 + (($k*$tmp))))
;;
c|C)
tmp1=$(($tmp1 + (($k*$tmp))))
;;
d|D)
tmp1=$(($tmp1 + (($k*$tmp))))
;;
e|E)
tmp1=$(($tmp1 + (($k*$tmp))))
;;
f|F)
tmp1=$(($tmp1 + (($k*15))))
;;
esac
done

local MK_$i=$tmp1;
done
else
echo "ERROR: Netmask is not in a recognised format"
exit 1
fi

#Parse the IP Address into 4 separate octets
if [ -n "$(echo $IP | /bin/grep \\.)" ]; then
j=1
for i in $(echo $IP|/bin/sed s/\\./\ /g); do
local NT_$j=$i
j=$(($j+1))
done
else
echo "ERROR: IP Address is not in a recognised format"
exit 1
fi

#Bitwise "AND" each octet.
NET=$(($MK_1&$NT_1)).$(($MK_2&$NT_2)).$(($MK_3&$NT_3)).$(($MK_4&$NT_4))

}

# Determine operating system.
OS=$(/bin/uname -s)

# Source the appropriate network configuraton file. Cuurently
# supported platforms are HP-UX and Red Hat Linux.

case $OS in
"HP-UX")
. /etc/rc.config.d/netconf
IP_ADDRESS=${IP_ADDRESS[$NET_INTERFACE]}
NETMASK=${SUBNET_MASK[$NET_INTERFACE]}
get_network $IP_ADDRESS $NETMASK
/usr/sbin/route delete net $NET $IP_ADDRESS
;;
"Linux")
. /etc/sysconfig/network-scripts/ifcfg-eth$NET_INTERFACE
IP_ADDRESS=$IPADDR
# Redundant, but self-documenting:
NETMASK=$NETMASK
get_network $IP_ADDRESS $NETMASK
/sbin/route delete -net $NET netmask $NETMASK
;;
esac

exit;