linux网卡绑定脚本
2013-08-20 15:30:51
此脚本适用于CentOS5.x和CentOS6.x。
#!/bin/bash
#********************************************
# Copyright (c) Beijing DaoWoo Times Technology Co., Ltd.
#
# Author : Wu XuLei (wuxulei@daowoo.com)
# FILE : bonding.sh
# USAGE : bonding.sh -h
# DESCRIPTION : A script that will bond the network adapters.
# REQUIREMENTS:
# ) The network adapters is available.
# ) Please run the script in the console.
# HISTORY :
# // Wu XuLei written
#********************************************
usage()
{
cat <<-END >&
usage: ${##*/} -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 .
-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: ${##*/} -i 192.168.1.1 -g 192.168.31.254 -n 255.255.240.0 -b bond0 -m -e "eth0 eth1 eth2"
-k -I :da8::: -G :da8:::
END
exit $E_OPTERROR
} #Defined function error
error()
{
error_num=$
case $error_num in
)
echo "You must specify -$pm parameter!" ;;
)
echo "" ;;
)
echo "Invalid IP address!" ;;
)
echo "The cluster subnet mask is not valid. A subnet mask must be contiguous. Enter a valid subnet mask." ;;
)
echo "The optional bond mode must be among 0,1,2,3,4,5 and 6." ;;
)
echo "No such network adapter $eth." ;;
)
echo "Name must be composed by uppercase and lowercase letters, numbers, dots and underscores, and the length of name ca
n not exceed characters." ;;
)
echo "The bond name $bn cat not be in network adapters $eths!" ;;
)
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=
eths="eth0 eth1"
f_flag= r_flag=
#Parameter confirmation
while getopts "i:g:n:b:m:e:I:G:fhr" Option
do
case $Option in
i) 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=;;
h) usage ;;
r) r_flag= ;;
\?) usage ;; # DEFAULT
esac
done #The nummber of parameters can not be zero.
[ $# -eq ] && usage
ipv4_v()
{
[ $# -eq ] && echo "Please input IP." && exit
for IP in $*
do
[ `echo $IP|awk -v RS='.' 'END{print NR-1}'` -ne ] && error
for i in `echo $IP | awk -F "." '{print $1, $2, $3, $4}'`
do
echo $i|grep "^[0-9]*$" > /dev/null || error
done
echo $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 }}' || error 3
done
} nm_v()
{
[ $# -ne ] && error
IP=$
[ `echo $IP|awk -v RS='.' 'END{print NR-1}'` -eq ] || error
echo $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$num4
flag1=
flag2=
NUM=`echo $num|wc -m`
for (( i=;i<`expr $NUM - `;i++ ))
do
if [ `echo ${num:$i:}` -eq ];then
[ $flag1 -eq $flag2 ] || error
fi
[ `echo ${num:$i:}` -eq ] && flag2=
done
}
name_v()
{
[ $# -ne ] && error
STRING=$
[ `echo $STRING|wc -c` -gt ] && error
if test "$(expr "${STRING}" : "[A-Za-z0-9_.]*")" -ne "$(expr length "${STRING}")" ; then
error
fi
} [ -z $ip ] && pm="i" && error
[ -z $gw ] && pm="g" && error
ipv4_v $ip
ipv4_v $gw
nm_v $nm
echo $bn|grep "bond[0-9]" > /dev/null || exit
if [ ! -z $ipv6 ];then
[ ! -f ip_v.pl ] && echo "No such file ip_v.pl" && exit
which perl > /dev/null >& || exit
perl ip_v.pl $ipv6
[ $? -ne ] && error
if [ ! -z $ipv6_gw ];then
perl ip_v.pl $ipv6_gw
[ $? -ne ] && error
fi
else
[ ! -z $ipv6_gw ] && echo "You must specify -I parameter!" && exit
fi [ `echo $bm|wc -m` -eq ] && echo $bm|grep "^[0-6]$" > /dev/null >& || error
for eth in $eths
do
ifconfig -a|grep $eth > /dev/null >& || error
done
echo "$eths"|grep $bn > /dev/null >& && error #To modify the modprobe.conf
cat /etc/modprobe.conf |grep "alias $bn bonding" > /dev/null >&
if [ $? -eq ];then
sed -i "s/.*alias $bn bonding.*$/alias $bn bonding/g" /etc/modprobe.conf
else
echo "alias $bn bonding" >> /etc/modprobe.conf
fi
cat /etc/modprobe.conf |grep "options $bn" > /dev/null >&
if [ $? -eq ];then
sed -i "s/^options $bn.*$/options $bn miimon=100 mode=$bm/g" /etc/modprobe.conf
else
echo "options $bn miimon=100 mode=$bm" >> /etc/modprobe.conf
fi
echo "/etc/modprobe.conf:"
cat /etc/modprobe.conf for i in $eths
do
rm -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 ] ;then
echo "IPV6INIT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "IPV6ADDR=$ipv6" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "IPV6PREFIX=64" >> /etc/sysconfig/network-scripts/ifcfg-$bn
echo "IPV6_AUTOCONF=no" >> /etc/sysconfig/network-scripts/ifcfg-$bn
grep NETWORKING_IPV6 /etc/sysconfig/network > /dev/null
if [ $? -eq ];then
sed -i "s/NETWORKING_IPV6=no/NETWORKING_IPV6=yes/" /etc/sysconfig/network
else
echo NETWORKING_IPV6=yes >> /etc/sysconfig/network
fi
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
do
echo "DEVICE=$i" > /etc/sysconfig/network-scripts/ifcfg-$i
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "MASTER=$bn" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$i
# echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo
echo "ifcfg-$i:"
cat /etc/sysconfig/network-scripts/ifcfg-$i
done #解除原有bond
if [ ! -z "`ls /sys/class/net`" ];then
for i in `ls /sys/class/net`
do
[ ! -d "/sys/class/net/$i" ] || [ "$i" == lo ] || [ "$i" == sit0 ] && break
j="`cat /sys/class/net/$i/bonding/slaves 2> /dev/null`"
if [ -z "$j" ] ;then
if [ `echo $i|grep bond` ] && [ $i != $bn ];then
ifdown $i down
rm -f /etc/sysconfig/network-scripts/ifcfg-$i
sed -i "/.*alias $i bonding.*$/d" /etc/modprobe.conf
sed -i "/^options $i.*$/d" /etc/modprobe.conf
fi
continue
fi
if [ "$i" == "$bn" ] ;then
for k in $j
do
if [ ! "`echo $eths|grep $k`" ];then
echo "DEVICE=$k" > /etc/sysconfig/network-scripts/ifcfg-$k
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$k
echo "ONBOOT=no" >> /etc/sysconfig/network-scripts/ifcfg-$k
echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$k
#echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$k
fi
done
else
for k in $eths
do
j="`echo $j|sed "s/$k//g"`"
l="`echo $j|sed "s/ //g"`"
if [ -z "$l" ];then
ifdown $i down
rm -f /etc/sysconfig/network-scripts/ifcfg-$i
sed -i "/.*alias $i bonding.*$/d" /etc/modprobe.conf
sed -i "/^options $i.*$/d" /etc/modprobe.conf
break
fi
done
fi
done
fi if [ $r_flag -eq ];then
for i in `ifconfig -a|grep bond|awk {'print $1'}`
do
if [ $i != $bn ];then
ifconfig $i down
rm -f /etc/sysconfig/network-scripts/ifcfg-$i
sed -i "/.*alias $i bonding.*$/d" /etc/modprobe.conf
sed -i "/^options $i.*$/d" /etc/modprobe.conf
fi
done
for i in `ifconfig -a|grep eth|awk {'print $1'}`
do
e_flag=
for j in $eths
do
if [ $i == $j ];then
e_flag= && break
fi
done
if [ $e_flag -eq ];then
echo "DEVICE=$i" > /etc/sysconfig/network-scripts/ifcfg-$i
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "ONBOOT=no" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-$i
echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-$i
fi
echo "ifcfg-$i:"
cat /etc/sysconfig/network-scripts/ifcfg-$i
done
fi chkconfig NetworkManager off
service NetworkManager stop
chkconfig network on
service network restart if [ $f_flag -eq ] ;then
echo -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
linux网卡绑定脚本的更多相关文章
- Linux网卡绑定探析
2013-08-20 15:39:31 现在很多服务器都自带双千兆网口,利用网卡绑定既能增加网络带宽,同时又能做相应的冗余,目前应用于很多的场景.linux操作系统下自带的网卡绑定模式,Linux b ...
- Linux网卡绑定bond0-实验
虚拟机添加网卡 ip addr 查看新增的网卡是否读取 添加bonding接口 [root@centos8~]$nmcli con add type bond con-name mybond0 ifn ...
- linux网卡绑定
- Linux下双网卡绑定(bonding技术)
Linux网卡绑定探析 2013-08-20 15:39:31 现在很多服务器都自带双千兆网口,利用网卡绑定既能增加网络带宽,同时又能做相应的冗余,目前应用于很多的场景.linux操作系统下自带的 ...
- 深度分析Linux下双网卡绑定七种模式 多网卡的7种bond模式原理
http://blog.csdn.net/abc_ii/article/details/9991845多网卡的7种bond模式原理 Linux网卡绑定mode共有七种(~) bond0.bond1.b ...
- Linux 双网卡绑定技术
bond技术是在linux2.4以后加入内核. 一般步骤是1.把bonding模块加入内核, 2 编辑要绑定的网卡设置,去除地址设定 3 添加bond设备,设置地址等配置 4 重启网络 5 在交换机 ...
- CentOS6.2网卡绑定配置
下面主要介绍在CentOS6.2下使用系统自带的bonding进行网卡绑定的详细步骤,在此之前你可以看一下Linux网卡绑定探析,你也可以使用网卡绑定的脚本进行网卡绑定操作. 注意:请在配置前关闭Ne ...
- 多网卡绑定(bond)
通过以下命令查看bond0的工作状态查询能详细的掌握bonding的工作状态,如这个绑定各网卡的工作状态.主备关系.链路侦测时间[root@ASMTS ~]# cat /proc/net/bondin ...
- Centos 7.6 双网卡绑定实现高可用
Centos 7.6 双网卡绑定实现高可用 作者:尹正杰 版权声明:原创作品, 谢绝转载!否则将追究法律责任. 一.Bond模式概述 当linux系统上有多个单独网卡,又想充分利用这些网卡,同时对外提 ...
随机推荐
- Out of range value for column 'phon' at row 1
解决方法 : int(11) 改成 bigint(50)
- tensorflow 之tensorboard 对比不同超参数训练结果
我们通常使用tensorboard 统计我们的accurate ,loss等,并绘制曲线,通常是使用一次训练中的, 但是,机器学习中通常要对比不同的 ‘超参数’给模型训练和预测能力的不同这时候如何整合 ...
- Python中struct.pack()和struct.unpack()
https://blog.csdn.net/tjuyanming/article/details/79700601 https://www.cnblogs.com/yezl/p/5861787.htm ...
- SQL语句中LEFT JOIN、JOIN、INNER JOIN、RIGHT JOIN的区别?
w3school的一套sql教程: http://www.w3school.com.cn/sql/index.asp left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录.r ...
- SpringBoot(十一)过滤器和拦截器
v博客前言 在做web开发的时候,过滤器(Filter)和拦截器(Interceptor)很常见,通俗的讲,过滤器可以简单理解为“取你所想取”,忽视掉那些你不想要的东西:拦截器可以简单理解为“拒你所想 ...
- 接收Android数据 递归显示表格数据
<html> <head> <title>展示</title> <script type="text/javascript" ...
- 什么是webFlux
什么是webFlux 左侧是传统的基于Servlet的Spring Web MVC框架,右侧是5.0版本新引入的基于Reactive Streams的Spring WebFlux框架,从上到下依次是R ...
- python文件操作 二
问题:在传输的时候对方不知道是以什么格式传给用户,传来传去彼此都忘了什么格式: 在编码的时候:utf8:是3个字符,gbk是2个字符,每个字符数字对应的字符,一段2进制串,如果10个字节全部都是中文, ...
- 【OpenGL】纹理(Texture)
纹理是一个2D图片(也有1D和3D),它用来添加物体的细节:这就像有一张绘有砖块的图片贴到你的3D的房子上,你的房子看起来就有了一个砖墙.因为我们可以在一张图片上插入足够多的细节,这样物体就会拥有很多 ...
- ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
这是因为我装了tensorflow-gpu 1.5版本,而我用的是cuda 8.0和cudnn6.0,1.5版本要求cuda 9.0,我的做法就是回滚tensorflow版本.