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)
|
|
|
|
|
2013-02-28 02:32:24 +00:00
|
|
|
# ipv6
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
|
|
|
result=`ip -6 route show $net/$mask`
|
|
|
|
if [ $? -ne 0 ] || [ -z "$result" ]
|
|
|
|
then
|
|
|
|
ret=0
|
|
|
|
else
|
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
result=`netstat -nr|grep $net`;
|
|
|
|
if [ $? -eq 0 ] && [ -n "$result" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
for x in `echo "$result"|tr -s " " ","`
|
|
|
|
do
|
|
|
|
if [ "$os_type" = "Linux" ]; then
|
|
|
|
net1=`echo $x|cut -d',' -f1`
|
|
|
|
gw1=`echo $x|cut -d',' -f2`
|
|
|
|
mask1=`echo $x|cut -d',' -f3`
|
|
|
|
if [ "$net" = "$net1" ] && [ "$mask" = "$mask1" ] && [ "$gw" = "$gw1" ]; then
|
|
|
|
ret=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
tmp1=`echo $x|cut -d',' -f1`
|
|
|
|
gw1=`echo $x|cut -d',' -f2`
|
2011-04-07 17:42:48 +00:00
|
|
|
|
2013-05-24 06:20:34 +00:00
|
|
|
n1=`echo $net |cut -d'.' -f1`
|
|
|
|
n2=`echo $net |cut -d'.' -f2`
|
|
|
|
n3=`echo $net |cut -d'.' -f3`
|
2013-02-28 02:32:24 +00:00
|
|
|
n4=`echo $net |cut -d'.' -f4`
|
2011-04-07 17:42:48 +00:00
|
|
|
|
2013-05-24 06:20:34 +00:00
|
|
|
netnum="$(( ($n1 << 24) + ($n2 << 16) + ($n3 << 8) + $n4 ))"
|
2013-02-28 02:32:24 +00:00
|
|
|
bits=32
|
2013-05-24 06:20:34 +00:00
|
|
|
while [ `expr $netnum % 2` -eq 0 ]
|
|
|
|
do
|
|
|
|
bits="$(( $bits - 1 ))"
|
|
|
|
netnum="$(( $netnum >> 1 ))"
|
|
|
|
done
|
2011-04-07 17:42:48 +00:00
|
|
|
|
2013-02-28 02:32:24 +00:00
|
|
|
tmp2="$net/$bits";
|
2013-05-24 06:20:34 +00:00
|
|
|
#echo "$tmp2=$tmp2"
|
|
|
|
if [ "$tmp1" = "$tmp2" ] && [ "$gw" = "$gw1" ]; then
|
|
|
|
ret=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo $ret
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:36:42 +00:00
|
|
|
add_persistent_route()
|
|
|
|
{
|
|
|
|
net=$1;
|
|
|
|
mask=$2;
|
|
|
|
gw=$3;
|
|
|
|
if [ -n "$4" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
ifname=$4
|
2013-01-23 18:36:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
xcat_config_start="# xCAT_CONFIG_START";
|
|
|
|
xcat_config_end="# xCAT_CONFIG_END";
|
|
|
|
|
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
#determine the os name
|
|
|
|
OS_name="something"
|
|
|
|
if [ -f /etc/redhat-release ]
|
|
|
|
then
|
2013-01-23 18:36:42 +00:00
|
|
|
OS_name="redhat" #it can be RedHatFerdora or CentOS
|
2013-05-24 06:20:34 +00:00
|
|
|
elif [ -f /etc/SuSE-release ]
|
|
|
|
then
|
2013-01-23 18:36:42 +00:00
|
|
|
OS_name="sles"
|
2013-05-24 06:20:34 +00:00
|
|
|
fi
|
2013-01-23 18:36:42 +00:00
|
|
|
|
2013-05-24 06:20:34 +00:00
|
|
|
case $OS_name in
|
|
|
|
sles)
|
|
|
|
#echo "sles"
|
2013-02-28 02:32:24 +00:00
|
|
|
# ipv6 net
|
2013-05-24 06:20:34 +00:00
|
|
|
filename="/etc/sysconfig/network/routes";
|
2013-02-28 02:32:24 +00:00
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
|
|
|
route="$net/$mask $gw - -"
|
|
|
|
route1="$net\/$mask $gw - -";
|
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
route="$net $gw $mask $ifname";
|
|
|
|
route1="$net $gw $mask $ifname";
|
|
|
|
fi
|
|
|
|
if [ -f $filename ]; then
|
|
|
|
grep "$route" $filename 2>&1 1>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then #route does not exist
|
|
|
|
grep "$xcat_config_start" $filename 2>&1 1>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then #no xCAT section
|
|
|
|
echo $xcat_config_start >> $filename
|
|
|
|
echo $route >> $filename
|
|
|
|
echo $xcat_config_end >> $filename
|
|
|
|
else
|
|
|
|
sed -i -e s/"$xcat_config_end"/"$route1\n$xcat_config_end"/g $filename
|
|
|
|
fi
|
|
|
|
echo "Persistent route \"$route\" added in $filename."
|
|
|
|
else
|
|
|
|
echo "Persistent route \"$route\" already exists in $filename."
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
#echo "got here"
|
|
|
|
echo "$xcat_config_start" > $filename
|
|
|
|
echo "$route" >> $filename
|
|
|
|
echo "$xcat_config_end" >> $filename
|
|
|
|
echo "Route \"$route\" added in $filename."
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2013-01-23 18:36:42 +00:00
|
|
|
|
2013-05-24 06:20:34 +00:00
|
|
|
;;
|
|
|
|
ubuntu*)
|
|
|
|
echo "Adding persistent route on Ubuntu is not supported yet."
|
|
|
|
;;
|
|
|
|
redhat)
|
|
|
|
#echo "rh/fedora/centos"
|
2013-02-28 02:32:24 +00:00
|
|
|
# ipv6 net
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
2013-05-24 06:20:34 +00:00
|
|
|
filename="/etc/sysconfig/static-routes-ipv6";
|
|
|
|
route="$ifname $net/$mask $gw";
|
2013-02-28 02:32:24 +00:00
|
|
|
# $net/mask does not work with sed cmd
|
2013-05-24 06:20:34 +00:00
|
|
|
route1="$ifname $net\/$mask $gw";
|
2013-02-28 02:32:24 +00:00
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
filename="/etc/sysconfig/static-routes";
|
|
|
|
route="any net $net netmask $mask gw $gw $ifname";
|
|
|
|
route1="any net $net netmask $mask gw $gw $ifname";
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
if [ -f $filename ]; then
|
|
|
|
grep "$route" $filename 2>&1 1>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then #route does not exist
|
|
|
|
grep "$xcat_config_start" $filename 2>&1 1>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then #no xCAT section
|
|
|
|
echo $xcat_config_start >> $filename
|
|
|
|
echo $route >> $filename
|
|
|
|
echo $xcat_config_end >> $filename
|
|
|
|
else
|
|
|
|
sed -i -e s/"$xcat_config_end"/"$route1\n$xcat_config_end"/g $filename
|
|
|
|
fi
|
|
|
|
echo "Persistent route \"$route\" added in $filename."
|
|
|
|
else
|
|
|
|
echo "Persistent route \"$route\" already exists in $filename."
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
#echo "got here"
|
|
|
|
echo "$xcat_config_start" > $filename
|
|
|
|
echo "$route" >> $filename
|
|
|
|
echo "$xcat_config_end" >> $filename
|
|
|
|
echo "Persistent route \"$route\" added in $filename."
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
2013-01-23 18:36:42 +00:00
|
|
|
else #AIX
|
2013-05-24 06:20:34 +00:00
|
|
|
echo "Adding persistent route on AIX is not supported yet."
|
2013-01-23 18:36:42 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
rm_persistent_route()
|
|
|
|
{
|
|
|
|
net=$1;
|
|
|
|
mask=$2;
|
|
|
|
gw=$3;
|
|
|
|
if [ -n "$4" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
ifname=$4
|
2013-01-23 18:36:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
#determine the os name
|
|
|
|
OS_name="something"
|
|
|
|
if [ -f /etc/redhat-release ]
|
|
|
|
then
|
2013-01-23 18:36:42 +00:00
|
|
|
OS_name="redhat" #it can be RedHatFerdora or CentOS
|
2013-05-24 06:20:34 +00:00
|
|
|
elif [ -f /etc/SuSE-release ]
|
|
|
|
then
|
2013-01-23 18:36:42 +00:00
|
|
|
OS_name="sles"
|
2013-05-24 06:20:34 +00:00
|
|
|
fi
|
2013-01-23 18:36:42 +00:00
|
|
|
|
2013-05-24 06:20:34 +00:00
|
|
|
case $OS_name in
|
|
|
|
sles)
|
|
|
|
#echo "sles"
|
|
|
|
filename="/etc/sysconfig/network/routes";
|
2013-02-28 02:32:24 +00:00
|
|
|
# ipv6 net
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
|
|
|
route="$net/$mask $gw - -";
|
|
|
|
route1="$net\/$mask $gw - -";
|
|
|
|
else
|
2013-04-08 07:28:55 +00:00
|
|
|
if [ -n "$ifname" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
route="$net $gw $mask $ifname";
|
|
|
|
route1="$net $gw $mask $ifname";
|
2013-04-08 07:28:55 +00:00
|
|
|
else
|
|
|
|
route="$net $gw $mask";
|
|
|
|
route1="$net $gw $mask";
|
|
|
|
fi
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
if [ -f $filename ]; then
|
2013-04-08 07:12:02 +00:00
|
|
|
grep "$route" $filename 2>&1 1>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then # Does not exist in file
|
|
|
|
echo "Persistent route \"$route\" does not exist in $filename."
|
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
sed -i -e s/"$route1"//g $filename
|
2013-04-08 07:12:02 +00:00
|
|
|
echo "Persistent route \"$route\" removed from $filename."
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Persistent route file $filename does not exist."
|
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
;;
|
|
|
|
ubuntu*)
|
|
|
|
echo "Removing persistent route on Ubuntu is not supported yet."
|
|
|
|
;;
|
|
|
|
redhat)
|
|
|
|
#echo "rh/fedora/centos"
|
2013-02-28 02:32:24 +00:00
|
|
|
#ipv6
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
|
|
|
filename="/etc/sysconfig/static-routes-ipv6";
|
|
|
|
# $net/$mask does not work with sed
|
|
|
|
route="$ifname $net\/$mask $gw"
|
|
|
|
route1="$ifname $net/$mask $gw"
|
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
filename="/etc/sysconfig/static-routes";
|
|
|
|
route="any net $net netmask $mask gw $gw $ifname";
|
|
|
|
route1="any net $net netmask $mask gw $gw $ifname";
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
if [ -f $filename ]; then
|
2013-04-08 07:12:02 +00:00
|
|
|
grep "$route" $filename 2>&1 1>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then # Does not exist in file
|
|
|
|
echo "Persistent route \"$route1\" does not exist in $filename."
|
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
sed -i -e s/"$route"//g $filename
|
2013-04-08 07:12:02 +00:00
|
|
|
echo "Persistent route \"$route1\" removed from $filename."
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Persistent route file $filename does not exist."
|
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
;;
|
|
|
|
esac
|
2013-01-23 18:36:42 +00:00
|
|
|
else #AIX
|
2013-05-24 06:20:34 +00:00
|
|
|
echo "Removing persistent route on AIX is not supported yet."
|
2013-01-23 18:36:42 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [ "$op" = "add" ]; then
|
|
|
|
result=$(route_exists $net $mask $gw)
|
|
|
|
if [ "$result" = "0" ]; then
|
2013-02-28 02:32:24 +00:00
|
|
|
#ipv6
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
2013-05-24 06:20:34 +00:00
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
|
|
|
cmd="ip -6 route add $net/$mask via $gw"
|
|
|
|
else
|
2013-02-28 02:32:24 +00:00
|
|
|
# AIX TODO
|
2013-05-24 06:20:34 +00:00
|
|
|
cmd="ip -6 route add $net/$mask via $gw"
|
|
|
|
fi
|
2013-02-28 02:32:24 +00:00
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
|
|
|
cmd="route add -net $net netmask $mask gw $gw"
|
|
|
|
else
|
|
|
|
cmd="route add -net $net -netmask $mask $gw"
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
fi
|
|
|
|
echo "Adding temporary route: $cmd"
|
|
|
|
result=`$cmd 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ $code -ne 0 ]; then
|
|
|
|
logger -t xCAT -p local4.err "$cmd\nerror code=$code, result=$result."
|
|
|
|
echo " error code=$code, result=$result."
|
|
|
|
#exit 1;
|
|
|
|
fi
|
2011-04-07 19:04:05 +00:00
|
|
|
else
|
2013-02-28 02:32:24 +00:00
|
|
|
#ipv6
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
|
|
|
echo "The temporary route ($net/$mask $gw) already exists."
|
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
echo "The temporary route ($net $mask $gw) already exists."
|
2013-02-28 02:32:24 +00:00
|
|
|
fi
|
2013-01-23 18:36:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
#add persistent route
|
|
|
|
add_persistent_route $net $mask $gw $ifname
|
|
|
|
elif [ "$op" = "delete" ]; then
|
|
|
|
result=$(route_exists $net $mask $gw)
|
|
|
|
if [ "$result" = "1" ]; then
|
2013-05-24 06:20:34 +00:00
|
|
|
# ipv6
|
|
|
|
if echo $net | grep : 2>&1 1>/dev/null
|
|
|
|
then
|
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
|
|
|
cmd="ip -6 route delete $net/$mask via $gw"
|
|
|
|
else
|
|
|
|
# AIX TODO
|
|
|
|
cmd="ip -6 route delete $net/$mask via $gw"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ "$(uname -s)" = "Linux" ]; then
|
|
|
|
cmd="route delete -net $net netmask $mask gw $gw"
|
|
|
|
else
|
|
|
|
cmd="route delete -net $net -netmask $mask $gw"
|
|
|
|
fi
|
|
|
|
fi
|
2013-02-28 02:32:24 +00:00
|
|
|
|
2013-05-24 06:20:34 +00:00
|
|
|
echo "Removing temporary route: $cmd"
|
|
|
|
result=`$cmd 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ $code -ne 0 ]; then
|
|
|
|
logger -t xCAT -p local4.err "$cmd\nerror code=$code, result=$result."
|
|
|
|
echo " error code=$code, result=$result."
|
|
|
|
fi
|
2011-04-07 19:04:05 +00:00
|
|
|
else
|
2013-05-24 06:20:34 +00:00
|
|
|
echo "The temporary route ($net $mask $gw) does not exist."
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
2013-05-24 06:20:34 +00:00
|
|
|
|
2013-01-23 18:36:42 +00:00
|
|
|
#remove the persistent route
|
|
|
|
rm_persistent_route $net $mask $gw $ifname
|
2011-04-07 17:42:48 +00:00
|
|
|
fi
|
|
|
|
exit 0
|