知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

lvs dr tunl真实机脚本

发布时间:2014-09-05 16:53:19作者:知识屋

#!/bin/bash
#create lvs dr or tunl realservr scripts.
#chenjian 2011-09-23
 
VIP_LIST="192.168.116.20 192.168.116.21"
USAGE="Usage: $0 {start|stop|status} {dr|tunl}"
NAME=tunl
NUM=0
 
 
if [ -z $1 ]
then
    echo $USAGE 
    exit 2
fi
 
ACTION=`echo $1|tr A-Z a-z`
 
if ! [ $1 == status ]
then
    if [ -z $2 ]
    then
        echo $USAGE
        exit 2
    fi
fi
 
TYPE=`echo $2|tr A-Z a-z`
 
case "$1" in
start)
 
if [ $TYPE == dr ]
then
    if /sbin/ifconfig |grep lo: > /dev/null
    then
        /sbin/ifdown lo
        /sbin/ifup lo
    fi
    for vip in `echo $VIP_LIST`;do
        /sbin/ifconfig lo:$NUM  $vip netmask 255.255.255.255 broadcast $vip
        /sbin/route add -host $vip dev lo:$NUM
        NUM=`echo $[$NUM+1]`
    done
 
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 
    sysctl -p >/dev/null 2>&1 
    echo "$TYPE RealServer Start [ OK ]"
elif  [ $TYPE == tunl ]
then
    for vip in `echo $VIP_LIST`;do
        if [ $NAME == tunl ]
        then
            NAME=tunl0
            /sbin/ifconfig "$NAME" $vip netmask 255.255.255.255
            /sbin/route add -host $vip dev $NAME
        else
            /sbin/ifconfig "$NAME":"$NUM" $vip netmask 255.255.255.255
            /sbin/route add -host $vip dev $NAME
        fi
        NUM=`echo $[$NUM+1]`
    done
    echo 1 > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/tunl0/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/tunl0/arp_filter
    echo 1 > /proc/sys/net/ipv4/conf/tunl0/forwarding
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
 
    sysctl -p >/dev/null 2>&1 
    echo "$TYPE RealServer Start [ OK ]"
else
    echo $USAGE
    exit 2
fi
;;
stop)
if [ $TYPE == dr ]
then
    for vip in `echo $VIP_LIST`;do
        /sbin/ifconfig lo:$NUM down
        /sbin/route del $vip
        NUM=`echo $[$NUM+1]`
    done
    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 
    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 
 
    sysctl -p >/dev/null 2>&1 
    echo "$TYPE RealServer Stop [ OK ]"
elif  [ $TYPE == tunl ]
then
        /sbin/ifconfig tunl0 down
 
        echo 0 > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/tunl0/arp_announce
        echo 1 > /proc/sys/net/ipv4/conf/tunl0/arp_filter
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
 
else
    echo $USAGE
    exit 2
fi
;;
status)
if /sbin/ifconfig |grep -q 'lo:'
then
    TYPE=dr
elif /sbin/ifconfig |grep -q 'tunl'
then
    TYPE=tunl
else
    exit 2
fi
    
for vip in `echo $VIP_LIST`;do
 
    if [ ! `/sbin/ifconfig |grep -q $vip` -o ! `netstat -rn |grep $vip` ];
    then
        echo "LVS-$TYPE real server stopped,vip = $vip"
    else 
        echo "LVS-$TYPE Running,vip = $vip"
    fi
done
 
;;
*)
echo "$USAGE"
exit 1
;;
esac 
 
作者“If one day”

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜