linux网卡绑定脚本

news/2024/7/7 21:14:23

2013-08-20 15:30:51

此脚本适用于CentOS5.x和CentOS6.x。

#!/bin/bash
#********************************************
#   Copyright (c) Beijing DaoWoo Times Technology Co., Ltd. 2011
#
#   Author      : Wu XuLei (wuxulei@daowoo.com)
#   FILE        : bonding.sh
#   USAGE       : bonding.sh -h
#   DESCRIPTION : A script that will bond the network adapters.
#   REQUIREMENTS: 
#                 1) The network adapters is available.  
#                 2) Please run the script in the console.               
#   HISTORY     :
#       09/26/2011 Wu XuLei written
#********************************************
usage()
{cat <<-END >&2
usage: ${0##*/} -i IPv4 -g IPv4-GATEWAY [ -n NETMASK ] [ -b BOND-NAME ] [ -m BOND-MODE ] [ -e ETHS ][ -I IPv6 ] [ -G IPv6-GATEWAY ] [ -r ] [ -f ] -h-i  IPv4             The local IPv4 address is a necessary option.-g  GATEWAY          Gateway is a necessary option.-n  NETMASK          The default netmask is 255.255.255.0 .-b  BOND-NAME        The default bond name is bond0.-m  BOND-MODE        The optional bond mode is 0 1 2 3 4 5 6.-e  ETHS             The network adapters will be bonded,the default is eth0 and eth1.-I  IPv6             The local IPv6 addresss.-G  IPv6-GATEWAY     The IPv6 gateway.-r                   To clear the all original network configuration.-f                   After running the script,reboot the host.-h                   Display this help and exit.eg: ${0##*/} -i 192.168.1.1 -g 192.168.31.254 -n 255.255.240.0 -b bond0 -m 6 -e "eth0 eth1 eth2"-k -I 2001:da8:3000::183 -G 2001:da8:3000::1
END
exit $E_OPTERROR
}#Defined function error
error()
{
error_num=$1
case $error_num in1)echo "You must specify -$pm parameter!" ;;2)echo "" ;;3)echo "Invalid IP address!" ;;4)echo "The cluster subnet mask is not valid. A subnet mask must be contiguous. Enter a valid subnet mask." ;;5)echo "The optional bond mode must be among 0,1,2,3,4,5 and 6." ;;6)echo "No such network adapter $eth." ;;7)echo "Name must be composed by uppercase and lowercase letters, numbers, dots and underscores, and the length of name ca
n not exceed 32 characters." ;;8)echo "The bond name $bn cat not be in network adapters $eths!" ;;9)echo "The value of -$Option parameter cat not be null!" ;;\*) usage ;;
esac
echo "Try '${0##*/} -h' for more information."
exit $error_num
}nm=255.255.255.0
bn=bond0
bm=6    
eths="eth0 eth1"
f_flag=0 r_flag=0
#Parameter confirmation
while getopts "i:g:n:b:m:e:I:G:fhr" Option
do      case $Option ini)  ip=$OPTARG ;;g)  gw=$OPTARG ;;n)  nm=$OPTARG ;;b)  bn=$OPTARG ;;m)  bm=$OPTARG ;;e)  eths="$OPTARG" ;;I)  ipv6="$OPTARG" ;;G)  ipv6_gw="$OPTARG" ;;f)  f_flag=1;;h)  usage ;;r)  r_flag=1 ;;\?) usage ;;   # DEFAULTesac
done#The nummber of parameters can not be zero.
[ $# -eq 0 ] && usage
ipv4_v()
{[ $# -eq 0 ] && echo "Please input IP." && exit 1for IP in $*do[ `echo $IP|awk -v RS='.' 'END{print NR-1}'` -ne 3 ] && error 3for i in `echo $IP | awk -F "." '{print $1, $2, $3, $4}'`doecho $i|grep "^[0-9]*$" > /dev/null || error 3doneecho $IP | awk -F "." '{if($1<=223&&$1>0&&$2<=255&&$2>=0&&$3<=255&&$3>=0&&$4<=255&&$4>=1&&NF=4){exit 0}else {exi
t 1}}' || error 3done
}   nm_v()  
{   [ $# -ne 1 ] && error 4IP=$1[ `echo $IP|awk -v RS='.' 'END{print NR-1}'` -eq 3 ] || error 4echo $IP | awk -F "." '{if($1<=223&&$1>0&&$2<=255&&$2>=0&&$3<=255&&$3>=0&&$4<=255&&$4>=1&&NF=4){exit 0}else {exit 1}}'IP1=`echo $IP|awk -F "." {'print $1'}`IP2=`echo $IP|awk -F "." {'print $2'}`IP3=`echo $IP|awk -F "." {'print $3'}`IP4=`echo $IP|awk -F "." {'print $4'}`num1=`echo "obase=2;$IP1"|bc|awk '{printf "%08d\n",$0}'`num2=`echo "obase=2;$IP2"|bc|awk '{printf "%08d\n",$0}'`num3=`echo "obase=2;$IP3"|bc|awk '{printf "%08d\n",$0}'`num4=`echo "obase=2;$IP4"|bc|awk '{printf "%08d\n",$0}'`num=$num1$num2$num3$num4flag1=1 flag2=1 NUM=`echo $num|wc -m`for (( i=0;i<`expr $NUM - 1`;i++ ))do if [ `echo ${num:$i:1}` -eq 1 ];then[ $flag1 -eq $flag2 ] || error 4fi[ `echo ${num:$i:1}` -eq 0 ] && flag2=0done
}
name_v()
{[ $# -ne 1 ] && error 7STRING=$1[ `echo $STRING|wc -c` -gt 32 ] && error 7if test "$(expr "${STRING}" : "[A-Za-z0-9_.]*")" -ne "$(expr length "${STRING}")" ; thenerror 7fi
}[ -z $ip ] && pm="i" && error 1
[ -z $gw ] && pm="g" && error 1
ipv4_v $ip
ipv4_v $gw
nm_v $nm
echo $bn|grep "bond[0-9]" > /dev/null || exit 1
if [ ! -z $ipv6 ];then[ ! -f ip_v.pl ] && echo "No such file ip_v.pl" && exit 1which perl > /dev/null 2>&1 || exit 1perl ip_v.pl $ipv6[ $? -ne 6 ] && error 3if [ ! -z $ipv6_gw ];thenperl ip_v.pl $ipv6_gw[ $? -ne 6 ] && error 3fi
else[ ! -z $ipv6_gw ] && echo "You must specify -I parameter!" && exit 1
fi      [ `echo $bm|wc -m` -eq 2 ] && echo $bm|grep "^[0-6]$" > /dev/null 2>&1 || error 5
for eth in $eths
do              ifconfig -a|grep $eth > /dev/null 2>&1 || error 6
done
echo "$eths"|grep $bn > /dev/null 2>&1 && error 8#To modify the modprobe.conf
cat /etc/modprobe.conf |grep "alias $bn bonding" > /dev/null 2>&1
if [ $? -eq 0 ];thensed -i "s/.*alias $bn bonding.*$/alias $bn bonding/g" /etc/modprobe.conf
elseecho "alias $bn bonding" >> /etc/modprobe.conf
fi      
cat /etc/modprobe.conf |grep "options $bn" > /dev/null 2>&1
if [ $? -eq 0 ];thensed -i "s/^options $bn.*$/options $bn miimon=100 mode=$bm/g" /etc/modprobe.conf
elseecho "options $bn miimon=100 mode=$bm" >> /etc/modprobe.conf
fi
echo "/etc/modprobe.conf:"
cat /etc/modprobe.conffor i in $eths
dorm -f /etc/sysconfig/network-scripts/ifcfg-$i
done#To set bond
echo "DEVICE=$bn" > /etc/sysconfig/network-scripts/ifcfg-$bn
echo "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "IPADDR=$ip" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "GATEWAY=$gw" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "NETMASK=$nm" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$bn
#echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$bn
if [ ! -z $ipv6 ] ;thenecho "IPV6INIT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$bnecho "IPV6ADDR=$ipv6" >> /etc/sysconfig/network-scripts/ifcfg-$bnecho "IPV6PREFIX=64" >> /etc/sysconfig/network-scripts/ifcfg-$bnecho "IPV6_AUTOCONF=no" >> /etc/sysconfig/network-scripts/ifcfg-$bngrep NETWORKING_IPV6 /etc/sysconfig/network > /dev/nullif [ $? -eq 0 ];thensed -i "s/NETWORKING_IPV6=no/NETWORKING_IPV6=yes/" /etc/sysconfig/networkelseecho NETWORKING_IPV6=yes >> /etc/sysconfig/networkfi
fi
[ ! -z $ipv6_gw ] && echo "IPV6_DEFAULTGW=$ipv6%$bn" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo    
echo "ifcfg-$bn:"
cat /etc/sysconfig/network-scripts/ifcfg-$bn
#To set eth
for i in $eths
doecho "DEVICE=$i" > /etc/sysconfig/network-scripts/ifcfg-$iecho "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "MASTER=$bn" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$i
#       echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$iechoecho "ifcfg-$i:"cat /etc/sysconfig/network-scripts/ifcfg-$i
done#解除原有bond
if [ ! -z "`ls /sys/class/net`" ];thenfor i in `ls /sys/class/net`do[ ! -d "/sys/class/net/$i" ] || [ "$i" == lo ] || [ "$i" == sit0 ] && breakj="`cat /sys/class/net/$i/bonding/slaves 2> /dev/null`"if [ -z "$j" ] ;thenif [ `echo $i|grep bond` ]  && [ $i != $bn ];thenifdown $i downrm -f /etc/sysconfig/network-scripts/ifcfg-$ised -i "/.*alias $i bonding.*$/d" /etc/modprobe.confsed -i "/^options $i.*$/d" /etc/modprobe.confficontinuefiif [ "$i" == "$bn" ] ;thenfor k in $jdoif [ ! "`echo $eths|grep $k`" ];thenecho "DEVICE=$k" > /etc/sysconfig/network-scripts/ifcfg-$kecho "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$kecho "ONBOOT=no" >> /etc/sysconfig/network-scripts/ifcfg-$kecho "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$k#echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$kfidoneelsefor k in $ethsdoj="`echo $j|sed "s/$k//g"`"l="`echo $j|sed "s/ //g"`"if [ -z "$l" ];thenifdown $i down rm -f /etc/sysconfig/network-scripts/ifcfg-$ised -i "/.*alias $i bonding.*$/d" /etc/modprobe.confsed -i "/^options $i.*$/d" /etc/modprobe.confbreakfidonefidone    
fi                      if [ $r_flag -eq 1 ];then       for i in `ifconfig -a|grep bond|awk {'print $1'}`do  if [ $i != $bn ];thenifconfig $i downrm -f /etc/sysconfig/network-scripts/ifcfg-$ised -i "/.*alias $i bonding.*$/d" /etc/modprobe.confsed -i "/^options $i.*$/d" /etc/modprobe.conffidonefor i in `ifconfig -a|grep eth|awk {'print $1'}`doe_flag=0for j in $ethsdoif [ $i == $j ];thene_flag=1 && breakfidoneif [ $e_flag -eq 0 ];thenecho "DEVICE=$i" > /etc/sysconfig/network-scripts/ifcfg-$iecho "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "ONBOOT=no" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$iecho "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$ifiecho "ifcfg-$i:"cat /etc/sysconfig/network-scripts/ifcfg-$idone    
fi                      chkconfig NetworkManager off
service NetworkManager stop 
chkconfig network on
service network restartif [ $f_flag -eq 1 ] ;thenecho -e "\033[40;31mThe host will reboot.\033[0m"reboot &
else            echo -e "\033[40;31mPlease reboot the host.\033[0m"
fi                              
echo "The network adapters have been bonded."
exit 0  

 

 

 

转载于:https://www.cnblogs.com/wuxulei/p/3270316.html


http://lihuaxi.xjx100.cn/news/276791.html

相关文章

机器学习模型融合实战指南

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达本文转自&#xff1a;机器学习算法那些小事模型融合&#xff1a;通过融合多个不同的模型&#xff0c;可能提升机器学习的性能。这一方法在各种机器学习比赛中广泛应用&…

android 子module混淆_Android 传统(经典)蓝牙框架

功能支持多设备同时连接支持观察者监听或回调方式。注意&#xff1a;观察者监听和回调只能取其一&#xff01;支持使用注解RunOn控制回调线程支持设置回调或观察者的方法默认执行线程配置因为使用了jdk8的一些特性&#xff0c;需要在module的build.gradle里添加如下配置&#x…

从嘈杂视频中提取超清人声,语音增强模型PHASEN已加入微软视频服务丨AAAI 2020...

作者 | 罗翀、银大成来源 | 微软研究院AI头条 &#xff08;ID: MSRAsia&#xff09;编者按&#xff1a;在刚刚落幕的 Ignite 大会上&#xff0c;微软展示了企业视频服务 Microsoft Stream 中的一项新功能——无论你在多么嘈杂的地方录制视频&#xff0c;Microsoft Stream 都能自…

最新版火车头织梦内容发布规则_火车头采集器V8.6发布dedecms自定义字段解决方案...

在使用火车头进行dede采集发布的时候&#xff0c;很多人会遇到如果自己增加了自定义字段该如何采集呢&#xff0c;之前本人也遇到了这种问题&#xff0c;搜索了很多百度文章最后完美解决的自定义字段如何发布成功的问题。1.首先打开你要使用对应版本的火车头发布模版编辑器&…

关于 CPU 的一些基本知识总结

点击上方“方志朋”&#xff0c;选择“设为星标”回复”666“获取新整理的面试资料作者&#xff1a;骏马金龙www.cnblogs.com/f-ck-need-u/p/11141636.htm关于CPU和程序的执行CPU是计算机的大脑。1、程序的运行过程&#xff0c;实际上是程序涉及到的、未涉及到的一大堆的指令的…

sql server 中将由逗号“,”分割的一个字符串,转换为一个表,并应用与 in 条件...

select * from tablenmae where id in(1,2,3) 这样的语句和常用&#xff0c;但是如果in 后面的 1&#xff0c;2&#xff0c;3是变量怎么办呢&#xff0c;一般会用字符串连接的方式构造sql语句 string aa"1,2,3";string sqltxt"select * from tablename where id…

解析激光雷达中时序融合的研究现状和发展方向

本文转自&#xff1a;计算机视觉联盟在自动驾驶领域&#xff0c;基于激光雷达 (LiDAR) 的3D物体检测和运动行为预测是一种普遍的方案。目前绝大部分关于激光雷达的物体检测算法都是基于单帧的。激光雷达的多帧时序数据&#xff0c;提供了对于检测物体的多视角观测 (multiple vi…

女帝

日本的夜生活很感兴趣&#xff0c;因为日本居然有堂堂正正的牛郎店。在里面长得超帅的牛郎陪客人喝酒、聊天&#xff0c;让客人忘记白天工作的辛苦。所以千万不要认为牛郎是从事色情服务的&#xff0c;虽然也有败类&#xff0c;但那是违反行业准则的……也就是&#xff0c;好的…