2011-04-07 17:42:48 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# IBM(c) 2011EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
#=head1 routeop
|
|
|
|
#=head2 routeop is called by makeroutes command and setuproutes postscript to
|
|
|
|
# setup a route on a node.
|
|
|
|
# The syntax is:
|
|
|
|
# routeop add/delete net mask gateway ifname
|
|
|
|
#=cut
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
op=$1
|
|
|
|
|
|
|
|
net=$2
|
|
|
|
mask=$3
|
2011-04-07 19:04:05 +00:00
|
|
|
gw=$4
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ -n "$5" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
ifname=$5
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
route_exists()
|
|
|
|
{
|
|
|
|
net=$1
|
|
|
|
mask=$2
|
|
|
|
gw=$3
|
|
|
|
ret=0
|
|
|
|
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ -n "$4" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
ifname=$4
|
|
|
|
fi
|
|
|
|
|
2011-08-22 01:08:40 +00:00
|
|
|
os_type=$(uname -s)
|
|
|
|
|
2011-04-07 17:42:48 +00:00
|
|
|
result=`netstat -nr|grep $net`;
|
|
|
|
if [ $? -eq 0 ] && [ -n "$result" ]; then
|
|
|
|
for x in `echo "$result"|tr -s " " ","`
|
|
|
|
do
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ "$os_type" = "Linux" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
net1=`echo $x|cut -d',' -f1`
|
|
|
|
gw1=`echo $x|cut -d',' -f2`
|
|
|
|
mask1=`echo $x|cut -d',' -f3`
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ "$net" = "$net1" ] && [ "$mask" = "$mask1" ] && [ "$gw" = "$gw1" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
ret=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
tmp1=`echo $x|cut -d',' -f1`
|
|
|
|
gw1=`echo $x|cut -d',' -f2`
|
|
|
|
|
|
|
|
n1=`echo $net |cut -d'.' -f1`
|
|
|
|
n2=`echo $net |cut -d'.' -f2`
|
|
|
|
n3=`echo $net |cut -d'.' -f3`
|
|
|
|
n4=`echo $net |cut -d'.' -f4`
|
|
|
|
|
2011-08-22 01:08:40 +00:00
|
|
|
netnum="$(( ($n1 << 24) + ($n2 << 16) + ($n3 << 8) + $n4 ))"
|
2011-04-07 17:42:48 +00:00
|
|
|
bits=32
|
|
|
|
while [ `expr $netnum % 2` -eq 0 ]
|
|
|
|
do
|
2011-08-22 01:08:40 +00:00
|
|
|
bits="$(( $bits - 1 ))"
|
|
|
|
netnum="$(( $netnum >> 1 ))"
|
2011-04-07 17:42:48 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
tmp2="$net/$bits";
|
|
|
|
#echo "$tmp2=$tmp2"
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ "$tmp1" = "$tmp2" ] && [ "$gw" = "$gw1" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
ret=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $ret
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:04:05 +00:00
|
|
|
result=$(route_exists $net $mask $gw)
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ "$result" = "0" ]; then
|
|
|
|
if [ "$op" = "add" ]; then
|
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
cmd="route add -net $net netmask $mask gw $gw"
|
|
|
|
else
|
2011-08-19 13:54:19 +00:00
|
|
|
cmd="route add -net $net -netmask $mask $gw"
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
2011-04-07 19:04:05 +00:00
|
|
|
echo "$cmd"
|
2011-04-07 17:42:48 +00:00
|
|
|
result=`$cmd 2>&1`
|
2011-04-07 19:04:05 +00:00
|
|
|
code=$?
|
|
|
|
if [ $code -ne 0 ]; then
|
2012-05-15 07:03:50 +00:00
|
|
|
logger -t xCAT -p local4.err "$cmd\nerror code=$code, result=$result."
|
2011-04-07 19:04:05 +00:00
|
|
|
echo " error code=$code, result=$result."
|
|
|
|
exit 1;
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
2011-04-07 19:04:05 +00:00
|
|
|
else
|
|
|
|
echo "The route ($net $mask $gw) does not exist."
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
|
|
|
else
|
2011-08-22 01:08:40 +00:00
|
|
|
if [ "$op" = "delete" ]; then
|
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
2011-04-07 17:42:48 +00:00
|
|
|
cmd="route delete -net $net netmask $mask gw $gw"
|
|
|
|
else
|
2011-08-16 17:52:17 +00:00
|
|
|
cmd="route delete -net $net -netmask $mask $gw"
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
2011-04-07 19:04:05 +00:00
|
|
|
echo "$cmd"
|
2011-04-07 17:42:48 +00:00
|
|
|
result=`$cmd 2>&1`
|
2011-04-07 19:04:05 +00:00
|
|
|
code=$?
|
|
|
|
if [ $code -ne 0 ]; then
|
2012-05-15 07:03:50 +00:00
|
|
|
logger -t xCAT -p local4.err "$cmd\nerror code=$code, result=$result."
|
2011-04-07 19:04:05 +00:00
|
|
|
echo " error code=$code, result=$result."
|
|
|
|
exit 1;
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
2011-04-07 19:04:05 +00:00
|
|
|
else
|
|
|
|
echo "The route ($net $mask $gw) already exists."
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
exit 0
|