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

linux网卡绑定脚本相关推荐

  1. 【干货】Linux 网卡绑定的相关知识和技巧

    原理知识 Linux 多网卡的7种bond模式原理 Linux 多网卡绑定 网卡绑定mode共有七种(0~6) bond0.bond1.bond2.bond3.bond4.bond5.bond6 常用 ...

  2. Linux网卡绑定(bonding)配置

    Linux网卡绑定(bonding)配置 目录 1 网卡绑定(Bonding)概述... 4 2 Linux下bonding配置... 6 2.1 建立bonding网卡.. 6 2.2 配置开机自动 ...

  3. linux更换网卡不识别_详解Linux双网卡绑定脚本的方法示例

    概述 linux运维及配置工作中,常常会用到双网卡绑定,少数几台服务器的配置还好,如果是需要配置几十甚至上百台,难免会枯燥乏味,易于出错,我编写了这个双网卡绑定的辅助脚本,可傻瓜式地完成linux双网 ...

  4. linux 网卡绑定解绑,网卡绑定开机绑定,关机解绑

    Linux下实现双网卡负载均衡系统环境:RHEL5根据甘肃兰州实际情况的双网卡绑定方案, 方案思路:开机绑定,关机解绑 一.建立虚拟网络接口ifcfg-bond0文件[root@yangwenjun ...

  5. linux 网卡流量脚本,每5分钟统计Linux 网卡流量的脚本

    每5分钟统计Linux 网卡流量的脚本 -- 脚本内容 #!/bin/bash eth="eth0" sec=300 echo -ne "date       start ...

  6. linux 网卡绑定updelay,Linux 配置双网卡绑定实现负载均衡

    Linux 配置双网卡绑定,实现负载均衡 1.Bond的工作模式 Linux bonding驱动提供了一个把多个网络接口设备捆绑为单个的网络接口设置来使用,用于网络负载均衡及网络冗余. bonding ...

  7. Gnu/Linux网卡绑定bonding

    系统:    CentOS或RHEL5系列系统 配置文件:/etc/modprobe.conf ---------------------------------------- 系统:    Cent ...

  8. linux系统子接口配置文件,Linux网卡绑定、子接口-IP别名

    查看网卡线缆状态: mii-tool eth0 查看网卡底层信息: ethtool eth0 ethtool -i eth0   查看网卡驱动信息 ethtool -S eth0  查看网卡状态统计信 ...

  9. linux网卡顺序问题,linux网卡绑定及网卡顺序变更测试.docx

    Linux网卡顺序变更导致网卡绑定出错及解决办法测试 2012/2/21 描述:linux中新安装网卡会导致原网卡识别顺序紊乱,影响网络及网卡绑定正常工作,此时可以更改/etc/sysconfig/n ...

最新文章

  1. 代码测试意味着完全消灭了Bug?
  2. Error loading WebappClassLoader
  3. Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
  4. 如何自动保存邮件草稿
  5. 5918. 统计字符串中的元音子字符串
  6. 【LeetCode-SQL每日一练】—— 627. 变更性别
  7. [转】:Android调试工具及方法
  8. 得罪全球首富的下场?曝光贝佐斯婚外情丑闻的小报将被卖身...
  9. 95-190-454-源码-window-Trigger-Flink 自定义trigger
  10. JSP和JSTL获取服务器参数
  11. Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value:
  12. 设计模式-------建造者模式(生成器模式)
  13. 初次接触ruby的困惑
  14. 16.2. jps - Java Virtual Machine Process Status Tool
  15. Source Insight的应用技巧、宏功能
  16. 《七周七并发模型》作者Paul Butcher、阿里云研究员余锋(褚霸)——QCon北京2016前瞻...
  17. 《财富自由子路》李笑来
  18. linux steam大屏幕模式,Steam 大屏幕模式 - Steam Support
  19. Linux下动态库so查找与函数列表
  20. java cursor_cursor的基本使用方法

热门文章

  1. 在7分钟内深刻理解咖喱
  2. linux消息通信无法接收,进程间通信:消息队列有关问题:进程1接收不到进程2的消息...
  3. Unity3D笔记 GUI 一
  4. Python 安装selenium
  5. 2.最详细的WSDD配置文件注释
  6. C#:将另一个应用程序的主窗口移至屏幕最前
  7. 一种无需留坑为页面动态添加View方案
  8. 新手小白 python之路 Day1 (三级菜单功能实现)
  9. Alisql源码编译安装(详细篇)
  10. Excel导入SQL数据库完整代码