分类 个人笔记 下的文章

#!/bin/bash
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m当前操作系统为$osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94m内核版本为2.6.32系列\e[0m"
elif [ "$oskernel" = "3.10.0" ]; then
echo -e "\e[94m内核版本为3.10.0系列\e[0m"
else
echo -e "\e[94m此脚本不支持当前操作系统!\e[0m"
exit
fi
multipath=`multipath -l |grep dm- |grep [0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z] -o`
if [[ "$multipath" == "" ]];then
echo -e "\e[94m此脚本不支持当前环境!\e[0m"
exit
fi
rm -f /etc/udev/rules.d/99-oracle-asmdevices.rules
if [ "$oskernel" = "2.6.32" ]; then
for dev in $multipath;
do
alias=`multipath -l |grep $dev |awk -F " " '{print$1}'`
echo "KERNEL==\"dm-*\",SUBSYSTEM==\"block\",PROGRAM==\"/sbin/scsi_id --whitelisted --replace-whitespace --device=/dev/\$name\",RESULT==\"$dev\",SYMLINK+=\"asm$alias\",OWNER=\"grid\",GROUP=\"asmadmin\",MODE=\"0660"\" >> /etc/udev/rules.d/99-oracle-asmdevices.rules
done
/sbin/udevadm trigger
elif [ "$oskernel" = "3.10.0" ]; then
for dev in $multipath;
do
alias=`multipath -l |grep $dev |awk -F " " '{print$1}'`
echo "KERNEL==\"dm-*\",SUBSYSTEM==\"block\",PROGRAM==\"/usr/lib/udev/scsi_id --whitelisted --replace-whitespace --device=/dev/\$name\",RESULT==\"$dev\",SYMLINK+=\"asm$alias\",OWNER=\"grid\",GROUP=\"asmadmin\",MODE=\"0660"\" >> /etc/udev/rules.d/99-oracle-asmdevices.rules
done
/usr/sbin/udevadm trigger
else
echo -e "\e[94m此脚本不支持当前操作系统!\e[0m"
exit
fi
if [ "$oskernel" = "2.6.32" ]; then
service multipathd restart > /dev/null 2>&1
sleep 2
elif [ "$oskernel" = "3.10.0" ]; then
systemctl restart multipathd > /dev/null 2>&1
sleep 2
fi
ls -l /dev/dm-*
ls -l /dev/asm*

方法 1

编辑文件/etc/sysctl.conf,

vi /etc/sysctl.conf

添加下面的行:

net.ipv6.conf.all.disable_ipv6 =1
net.ipv6.conf.default.disable_ipv6 =1

如果为特定的网卡禁止IPv6,比如,对于enp0s3,添加下面的行。

net.ipv6.conf.enp0s3.disable_ipv6 =1

保存并退出文件。
执行下面的命令来使设置生效。

sysctl -p

方法 2

要在运行的系统中禁止IPv6,依次输入下面的命令:

echo 1 >/proc/sys/net/ipv6/conf/all/disable_ipv6
echo 1 >/proc/sys/net/ipv6/conf/default/disable_ipv6

或者

sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1

就是这样。现在IPv6已经禁止了。
重启生效

reboot
netstat -lnpt

只需更改mac地址ATTR{address}及网卡名称NAME即可

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:8e:fc:f1", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:8e:0f:fa", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:8e:c5:d5", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:8e:66:bf", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3"

1,定义用户或者用户组

User_Alias NETADMIN = cheng401,chengjianjun,lilongjie,ruili,renc,lab320,lzuprogram,yaozj,kzhan,radio401,sally,ylu,rjsxs,zg501,wanyi,zhgmao,lixiyun,test

2,定义用户可以执行和不能执行的命令

Cmnd_Alias USERADMINCMNDS = /usr/sbin/*,/sbin/*,/usr/bin/*,!/usr/bin/passwd,!/usr/sbin/visudo,!/usr/sbin/useradd,!/usr/sbin/userdel,!/usr/bin/passwd root --stdin

3,将上述内容组合,使其有sudo权限

NETADMIN ALL=(root) USERADMINCMNDS

永久配置,打开/etc/iscsi/iscsid.conf文件,找到如下三项并取消注释:

node.session.auth.authmethod = CHAP   //开启CHAP认证
node.session.auth.username = admint    //配置账号
node.session.auth.password = hech@123  //密码

测试可以用下面的命令

1,发现存储

iscsiadm -m discovery -t st -p 192.168.200.200

2,添加验证信息

1,先开启验证
iscsiadm -m node -o update -p 192.168.200.200 -n node.session.auth.authmethod -v CHAP
2,再添加用户
iscsiadm -m node -o update -p 192.168.200.200 -n node.session.auth.username -v admin
3,添加密码
iscsiadm -m node -o update -p 192.168.200.200 -n node.session.auth.password -v hech@123

或者

1,先开启验证
iscsiadm -m node -T iqn.1992-08.com.netapp:sn.418df88c380811eaa48f00a0b8f70f5c:vs.3 -o update --name node.session.auth.authmethod --value=CHAP
2,再添加用户
iscsiadm -m node -T iqn.1992-08.com.netapp:sn.418df88c380811eaa48f00a0b8f70f5c:vs.3 --op update --name node.session.auth.username --value=admin
3,添加密码
iscsiadm -m node -T iqn.1992-08.com.netapp:sn.418df88c380811eaa48f00a0b8f70f5c:vs.3 --op update --name node.session.auth.password --value=hech@123

3,登录存储

iscsiadm -m node -p 192.168.200.200 -l

双向认证

双向认证就是在Initiator端和target端都进行认证。Initiator 认证:在initiator尝试连接到一个target的时候,initator需要提供一个用户名和密码给target供target进行认证。下面我们称这个用户名密码为incoming账号,即:incoming账号是initiator端提供给target端,供target端认证的账号。target 认证:在initiator尝试连接到一个target的时候,target需要提供一个用户名和密码给initiator供initiator进行认证。与之对应的是outcoming账号,即:outcoming账号是target端提供给initiator端,供initiator认证的账号。双向认证的配置如下:

# To set a CHAP username and password for initiator
# authentication by the target(s), uncomment the following lines:
node.session.auth.username = username
node.session.auth.password = password
# To set a CHAP username and password for target(s)
# authentication by the initiator, uncomment the following lines:
node.session.auth.username_in = username_in
node.session.auth.password_in = password_in

注意事项:修改字符集前先将数据库进行备份

此处演示将ZHS16GBK字符集修改为AL32UTF8

1、修改server端字符集
登录sqlpus查看字符集设置

[oracle@localhost ~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 24 13:55:51 2018
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
SQL> conn /as sysdba
Connected to an idle instance.                  #数据库未启动,先启动数据库。最好将数据库设未开机启动
SQL> startup
SQL> conn /as sysdba
Connected.                                      #连接成功
SQL> select userenv('language') from dual;      #server端字符集查询

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK

依次执行如下命令

SQL>SHUTDOWN IMMEDIATE;
SQL>STARTUP MOUNT;
SQL>ALTER SYSTEM ENABLE RESTRICTED SESSION;
SQL>ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;
SQL>ALTER SYSTEM SET AQ_TM_PROCESSES=0;
SQL>ALTER DATABASE OPEN;
SQL>ALTER DATABASE CHARACTER SET INTERNAL_USE AL32UTF8;
SQL>SHUTDOWN IMMEDIATE;
SQL>STARTUP;
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.AL32UTF8
SQL> 

2、修改client端字符集
查看系统环境变量设置的字符集(client端字符集)

[oracle@localhost ~]$ cat /home/oracle/.bash_profile
...
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export ORACLE_BASE=/usr/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=orcl
export ORACLE_TERM=xterm
export PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export LANG=C
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK                #客户端字符集

进入编辑界面,将ZHS16GBK改为AL32UTF8,保存退出

[oracle@localhost ~]$ vim /home/oracle/.bash_profile

使配置生效

[oracle@localhost ~]$ source /home/oracle/.bash_profile

修改完成

方法一:

1、安装好Oracle数据库后: 执行 dbstart和dbshut

[oracle@localhost ~]$ dbstart
ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener
Usage: /usr/oracle/product/11.2.0/db_1/bin/dbstart ORACLE_HOME

错误原因:dbstart和dbshut脚本文件中ORACLE_HOME_LISTNER的设置有问题

[oracle@localhost ~]$ vim $ORACLE_HOME/bin/dbstart
[oracle@localhost ~]$ vim $ORACLE_HOME/bin/dbshut

分别打开两个文件修改(50行左右)

ORACLE_HOME_LISTNER=$1
修改为:
ORACLE_HOME_LISTNER=$ORACLE_HOME
修改后保存退出,问题解决

2、Linux启动时自动启动Oracle监听和实例
第一步:修改/etc/oratab文件

[oracle@localhost ~]$ vim /etc/oratab
找到:   orcl:/usr/oracle/product/11.2.0/db_1:N   
修改为: orcl:/usr/oracle/product/11.2.0/db_1:Y
(这个路径跟安装路径有关,$ORACLE_SID:$ORACLE_HOME:<N|Y>)
第二步:把lsnrctl start和dbstart添加到rc.local文件中:

root权限执行

[root@localhost oracle]# vim /etc/rc.d/rc.local

添加:

su - oracle -lc "/usr/oracle/product/11.2.0/db_1/bin/lsnrctl start"
su - oracle -lc "/usr/oracle/product/11.2.0/db_1/bin/dbstart"

说明:

第一行为开机启动数据库监听服务,第二行为开机启动数据库。(路径跟安装路径相关)。

注意:CentOs7中/etc/rc.d/rc.local不会开机执行,需添加执行权限。

查看/etc/rc.d/rc.local的权限

[root@localhost oracle]# ll /etc/rc.d/rc.local

添加执行权限

[root@localhost oracle]# chmod +x /etc/rc.d/rc.local

3、重启服务器后查看是否自启动成功
查看监听是否自启动成功

[oracle@localhost ~]$ lsnrctl status LISTENER

查看Oracle服务状态

[oracle@localhost ~]$ ps –ef | grep oracle

4、oracle的启动或关闭管理

[root@localhost oracle]# dbstart                   #启动
[root@localhost oracle]# dbshut                    #停止

方法二:

1、修改/etc/oratab文件
root权限执行

[root@localhost oracle]# vim /etc/oratab
找到:    orcl:/usr/oracle/product/11.2.0/db_1:N   
修改为:  orcl:/usr/oracle/product/11.2.0/db_1:Y

(这个路径跟安装路径有关,$ORACLE_SID:$ORACLE_HOME:<N|Y>)
2、新建Oracle服务自启动脚本

[root@localhost oracle]# vim /etc/init.d/oracle

将以下脚本复制到文件中,保存退出(:wq)

#!/bin/sh
# chkconfig: 2345 61 61
# description: Oracle 11g R2 AutoRun Servimces
# /etc/init.d/oracle
#
# Run-level Startup script for the Oracle Instance, Listener, and
# Web Interface
export ORACLE_BASE=/usr/oracle                                          #oracle安装位置
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1                     #Oracle安装路径
export ORACLE_SID=orcl
export PATH=$PATH:$ORACLE_HOME/bin
ORA_OWNR="oracle"
# if the executables do not exist -- display error
if [ ! -f $ORACLE_HOME/bin/dbstart -o ! -d $ORACLE_HOME ]
then
echo "Oracle startup: cannot start"
exit 1
fi
# depending on parameter -- startup, shutdown, restart
# of the instance and listener or usage display
case "$1" in
start)
# Oracle listener and instance startup
su $ORA_OWNR -lc $ORACLE_HOME/bin/dbstart
echo "Oracle Start Succesful!OK."
;;
stop)
# Oracle listener and instance shutdown
su $ORA_OWNR -lc $ORACLE_HOME/bin/dbshut
echo "Oracle Stop Succesful!OK."
;;
reload|restart)
$0 stop
$0 start
;;
*)
echo $"Usage: `basename $0` {start|stop|reload|reload}"
exit 1
esac
exit 0

3、检查脚本能否执行

[root@localhost oracle]# /etc/init.d/oracle start            #启动oracle脚本
[root@localhost oracle]# /etc/init.d/oracle stop             #关闭oracle脚本
[root@localhost oracle]# /etc/init.d/oracle restart          #重启oracle脚本

4、添加执行权限并建立链接
更改oracle脚本的执行权限

[root@localhost oracle]# chmod a+x /etc/init.d/oracle

建立链接

将启动脚本添加到系统服务并设置自启动

[root@localhost oracle]# chkconfig --add oracle

当这个命令被执行的时候,会去脚本文件oracle中寻找# chkconfig: 2345 61 61这行注释,并解析这行注释,根据解析结果分别在

/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d

中创建符号连接文件S61oracle,此文件在系统启动时根据运行级别执行,此文件是指向/etc/init.d/oracle文件。启动时系统向此文件发送一个start参数,执行oracle文件中的start分支。另外还会在

/etc/rc.d/rc0.d
/etc/rc.d/rc1.d
/etc/rc.d/rc6.d

中创建符号连接文件K61oracle,此文件在系统关闭时执行,此文件也指向/etc/init.d/oracle文件,关闭时系统向此文件发送一个stop参数,执行oracle文件中的stop分支。

chkconfig: 2345 61 61

表明脚本应该在运行级 2, 3, 4, 5 启动,启动优先权为61,停止优先权为 61。

修改服务运行等级(虽然脚本里写过,但还是重新设置一下),可以自行设置oracle脚本的运行级别

[root@localhost oracle]# chkconfig --level 2345 oracle on

说明:设置oracle脚本在运行级别为2、3、4、5时,都是on(开启)状态,off为关闭

查看oracle自动启动设置

[root@localhost oracle]# chkconfig –list oracle
Oracle   0:off 1:off 2:on 3:on 4:on 5:on 6:off
复制代码
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
复制代码
手动创建符号链接文件(执行效果和执行chkconfig --add oracle是一样,作为知识笔记记录,可以不执行)
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc0.d/K61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc1.d/K61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc2.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc3.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc4.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc5.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc6.d/K61oracle

5、oracle的启动或关闭管理

启动
[root@localhost oracle]# service oracle start
停止
[root@localhost oracle]# service oracle stop
重启
[root@localhost oracle]# service oracle restart

#!/bin/bash
if [ $UID -ne 0 ]; then
echo "The current user is not root,please execute this script as the root user!"
exit
fi
rsync -h > /dev/null 2>&1
if [ $? != 0 ];then
echo "rsync命令未找到!请先安装!"
exit
fi

while getopts "s:d:p:z:" opt; do
  case $opt in
    s)
        source=$OPTARG
    ;;
    d)
        target=$OPTARG
    ;;
    p)
        nfsip=$OPTARG
    ;;
    z)
        del=--delete
    ;;
    \?)
echo -e "\e[94m使用方法:./scriptname.sh -s |源绝对路径| -d |目的绝对路径| -p |NFS IP地址|(可选) -z sync|两边数据一致|(可选)\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如果要保证源文件和目标文件完全一致则加 -z sync\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200 -z sync"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如有多个NFS地址,则用 - 分隔\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs -p 192.168.200.200-192.168.200.201-192.168.200.203"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
  esac
done

if [[ -z "$source" ]]||[[ -z "$target" ]];then
echo -e "\e[94m使用方法:./scriptname.sh -s |源绝对路径| -d |目的绝对路径| -p |NFS IP地址|(可选) -z sync|两边数据一致|(可选)\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如果要保证源文件和目标文件完全一致则加 -z sync\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200 -z sync"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如有多个NFS地址,则用 - 分隔\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs -p 192.168.200.200-192.168.200.201-192.168.200.203"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
exit
fi
if [[ -d "$source" ]]||[[ -d "$target" ]];then
echo
else
echo -e "输入的路径不存在!" 
exit
fi
if [[ -n $nfsip ]];then
ip=`echo $nfsip |sed s/-/\ /g`
for tip in $ip
do
ping=`ping -c 5 $tip |awk 'NR==9 {print $4}'`
if [[ $ping -ne 0 ]];then
dect=`/usr/sbin/showmount -e $tip`
if [[ $? != 0 ]];then
echo -e "\e[94mNFS组件不正常!\e[0m"
fi
path=`/usr/sbin/showmount -e $tip |grep -v Export |awk -F " " '{print$1}'|awk -F "/" '{print$2}' |sed '/^$/d'`
mount=`df -h |grep $tip: |awk -F " " '{print$1}' |awk -F "/" '{print$2}'`
if [[ "$path" != "$mount" ]];then
echo -e "$tip NFS挂载不正常,请检查!\e[0m"
exit
fi
else
echo -e "\e[94m当前主机无法ping通NFS $tip 请检查网络配置!\e[0m"
exit
fi
done
fi
mkdir -p /script/logs
mkdir -p /script/scriptlogs
folder=`echo $source |sed s#/#\#g`
rsync -avz $del $source $target > /script/logs/$folder.`date +"%Y-%m-%d-%H-%M-%S"`.log
if [ $? == 0 ];then
echo "已在`date +"%Y-%m-%d-%H-%M-%S"`从$source同步到$target成功,在/script/logs下查看备份信息。"  > /script/scriptlogs/$folder.`date +"%Y-%m-%d-%H-%M-%S"`.log
else
echo "当前备份不成功!"  > /script/scriptlogs/$folder.`date +"%Y-%m-%d-%H-%M-%S"`.log
fi

#!/bin/bash
sttya=`tty`
if [[ "$sttya" =~ "pts" ]];then
stty erase ^?
fi
if [ $UID -ne 0 ]; then
echo "当前用户不是root用户,请以root用户身份执行此脚本!"
echo "The current user is not root,please execute this script as the root user!"
exit
fi
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1.中文                           #\e[0m"
echo -e "\e[94m#                           2.English                        #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m请选择运行脚本所用的语言: \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mPlease select a language to run the script: \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
read -p "Enter the number: " language
if [[ $language == 1 ]];then
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m已选择中文!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
language=1
export LANG="zh_CN.UTF-8"
sleep 1
break;
elif [[ $language == 2 ]];then
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mEnglish selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
language=2
export LANG="en_US.UTF-8"
sleep 1
break;
else
echo -e "\e[94m请选择语言!\e[0m"
echo -e "\e[94mPlease select a language!\e[0m"
fi
done 
if [ $language == 1 ];then
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#         1.Linux网卡绑定脚本,支持bond和team绑定方式        #\e[0m"
echo -e "\e[94m#         2.支持CentOS 6.x/7.x/8.x RedHat 6.x/7.x/8.x        #\e[0m"
echo -e "\e[94m#         3.作者:猫先生                                     #\e[0m"
echo -e "\e[94m#         4.编写时间:2020年3月14日                          #\e[0m"
echo -e "\e[94m#         5.更多内容访问 https://www.mr-mao.cn               #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;m提示:如按Backspace键无法正常删除内容时,可按 Ctrl+Backspace\e[0m"
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前操作系统为$osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94m内核版本为 2.6.32 系列\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前系统只支持bond绑定模式!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
service NetworkManager stop > /dev/null 2>&1
chkconfig NetworkManager off > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m当前系统网络服务运行不正常,请先检查网络服务!\e[0m"
systemctl status network
exit
fi
echo -e "\e[94m内核版本为 3.10.0 系列\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1.Bond模式                       #\e[0m"
echo -e "\e[94m#                           2.Team模式                       #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;m提示:bond和team二选一,不推荐两种模式共存!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前系统支持team和bond两种绑定模式,请选择:\e[0m"
while true
do
read -p "选择绑定模式,输入数字即可: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94m已选择Bond绑定模式:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#当前系统无法支持team绑定命令!请检查系统网络管理组件!\e[0m"
exit
fi
echo -e "\e[94m已选择Team绑定模式:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94m请选择正确的绑定模式!\e[0m"
fi
done
elif [ "$oskernel" = "4.18.0" ]; then
echo -e "\e[94m内核版本为 4.18.0 系列\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
systemctl start network > /dev/null 2>&1
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m当前系统只支持Team模式\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m当前系统无法支持team绑定命令!请检查系统网络管理组件!#\e[0m"
exit
fi
sleep 1
break;
else
echo -e "\e[94m#####################################################\e[0m"
echo -e "\e[94m#                    1.Bond模式                     #\e[0m"
echo -e "\e[94m#                    2.Team模式                     #\e[0m"
echo -e "\e[94m#####################################################\e[0m"
echo -e "\e[94m提示:bond和team二选一,不推荐两种模式共存!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前系统支持team和bond两种绑定模式,推荐选择Team模式:\e[0m"
read -p "选择绑定模式,输入数字即可: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94m已选择Bond绑定模式!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#当前系统无法支持team绑定命令!请检查系统网络管理组件!#\e[0m"
exit
fi
echo -e "\e[94m已选择Team绑定模式!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
break;
else
echo -e "\e[94m请选择正确的绑定模式!\e[0m"
fi
fi
done
else
echo -e "\e[94m此脚本不支持当前操作系统!\e[0m"
exit
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
mkdir -p /etc/sysconfig/network-scripts/nicsbak
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicall
for selnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr|: team|: bond" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}';`
do
selnicip=`ip a s $selnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selnicgateway=`ip route |grep default |grep $selnic |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selnic  $selnicip  $selnicgateway >> /tmp/nic
done
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
for selbond in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' |grep -E "team|bond"`;
do
selbondip=`ip a s $selbond | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selbondgateway=`ip route |grep default |grep $selbond |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selbond  $selbondip  $selbondgateway >> /tmp/presentbondall
done
nicall=`cat /tmp/nicall`
nicallnum=`cat /tmp/nic |wc -l`
if [ "$nicallnum" == 1 ];then
echo -e "\e[94m当前主机只有一块网卡,无需做绑定!\e[0m"
exit
fi
if [[ "$nicall" =~ "bond" ]];then
echo -e "\e[94m当前系统已存在bond!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: bond|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$detectnic" ];then
bondsubniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic`
if [[ "$bondsubniccontent" =~ "bond" ]];then
presentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94m$detectnic 已是 $presentbond 成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
fi
done
fi
if [[ "$nicall" =~ "team" ]];then
echo -e "\e[94m当前系统已存在team!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ls /etc/sysconfig/network-scripts/ifcfg-team*-* | xargs -i basename {\}`;
do
teamsubniccontent=`cat /etc/sysconfig/network-scripts/$detectnic`
if [[ "$teamsubniccontent" =~ "team" ]];then
presentteam=`cat /etc/sysconfig/network-scripts/$detectnic |grep TEAM_MASTER |awk -F "=" '{print$2}'`
teamport=`cat /etc/sysconfig/network-scripts/$detectnic |grep '\<DEVICE\>' |awk -F "=" '{print$2}'`
echo $teamport $presentteam >> /tmp/teams
echo -e "\e[94m$teamport 已是 $presentteam 成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
fi
if [[ -f "/tmp/presentbondall" ]];then
echo -e "\e[94m当前主机已有网卡聚合组:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/presentbondall
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
echo -e "\e[94m当前主机所有网卡如下:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/nic |awk '{print NR,$0}'
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
read -p "选择用来绑定的网卡,输入数字即可,多个网卡,用空格隔开: " nicnum
selectnum=`echo $nicnum |awk -F " " '{print NF}'`
if [ $selectnum -lt 2 ];then
echo -e "\e[94m最少选择两块网卡,请重新选择!\e[0m"
else
break;
fi
done
for num in $nicnum;
do
nicx=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
if [[ "$nicx" == "" ]];then
echo -e "\e[94m请选择正确的网卡!\e[0m"
exit
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicip=`ip a s $selectnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
if [ -n "$selectnicip" ];then
echo -e "\e[94m网卡 $selectnic 已有IP地址 $selectnicip \e[0m"
read -p "是否继续?如果继续,$selectnic 的IP配置会被删除,主机网络可能会中断!按n退出,按其他按键继续:" answer0
answer0lower=`echo $answer0 | tr [A-Z] [a-z]`
if [[ "$answer0lower" == "n" ]]; then
echo -e "\e[94m如需绑定,请重新执行脚本,选择其他网卡!\e[0m"
exit
fi
fi
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$selectnic" ];then
sed -i '/ONBOOT/s/yes/no/' /etc/sysconfig/network-scripts/ifcfg-$selectnic
selectniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic`
selectnicinpresentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94m已选择网卡 $selectnic \e[0m"
if [[ "$selectniccontent" =~ "bond" ]];then
echo -e "\e[94m网卡 $selectnic 已是$selectnicinpresentbond成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "是否继续?如果继续,$selectnicinpresentbond 会被删除!按n退出,按其他按键继续:" answer1
answer1lower=`echo $answer1 | tr [A-Z] [a-z]`
if [[ "$answer1lower" == "n" ]]; then
echo -e "\e[94m如需绑定,请重新执行脚本,选择其他网卡!\e[0m"
exit
else
echo -e "\e[94m网卡 $selectnic 已选定!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentbond" ]];then
ifdown $selectnicinpresentbond > /dev/null 2>&1
echo -e "\e[94m已将现有 $selectnicinpresentbond 禁用!\e[0m"
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond
echo -$selectnicinpresentbond > /sys/class/net/bonding_masters
echo -e "\e[94m已将现有 $selectnicinpresentbond 配置文件备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnic /etc/sysconfig/network-scripts/ifcfg-$selectnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnic
echo -e "\e[94m网卡 $selectnic 配置文件已备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
sleep 1
else
echo -e "\e[94m网卡 $selectnic 已选定!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
if [ -f "/tmp/teams" ];then
for num in $nicnum;
do
selectteamnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicinpresentteam=`cat /tmp/teams |grep $selectteamnic |awk -F " " '{print$2}'`
echo -e "\e[94m已选择网卡 $selectteamnic \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [[ "$selectnicinpresentteam" =~ "team" ]];then
echo -e "\e[94m网卡 $selectteamnic 已是 $selectnicinpresentteam 成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "是否继续?如果继续,$selectnicinpresentteam 会被删除!按n退出,按其他按键继续:" answer2
answer2lower=`echo $answer2 | tr [A-Z] [a-z]`
if [[ "$answer2lower" == "n" ]]; then
echo -e "\e[94m如需绑定,请重新执行脚本,选择其他网卡!\e[0m"
exit
else
echo -e "\e[94m网卡 $selectteamnic 已选定!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentteam" ]];then
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* /etc/sysconfig/network-scripts/nicsbak/
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
for disteam in `ls /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* | xargs -i basename {\} |awk -F "ifcfg-" '{print$2}'`
do
cp -f /etc/sysconfig/network-scripts/ifcfg-$disteam /etc/sysconfig/network-scripts/ifcfg-$disteam-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
nmcli con delete $disteam > /dev/null 2>&1
done 
echo -e "\e[94m已将现有 $selectnicinpresentteam 禁用!\e[0m"
echo -e "\e[94m已将现有 $selectnicinpresentteam 配置文件备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectteamnic /etc/sysconfig/network-scripts/ifcfg-$selectteamnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
echo -e "\e[94m网卡 $selectteamnic 配置文件已备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
else
echo -e "\e[94m网卡 $selectteamnic 已选定!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
sleep 1
fi
done
fi
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
while true
do
read -p "请输入bond名称,如 bond0 : " bondname1
bondname=`echo ${bondname1,,}`
for ifpresentbond in `cat /tmp/nicnew`
do
if [[ "$ifpresentbond" == "$bondname" ]];then
echo -e "\e[94m当前已存在 $bondname 请重新输入!\e[0m"
read -p "请输入bond名称,如 bond0 : " bondname
echo -e "\e[94m输入的bond名称为:$bondname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $bondname ];then
echo -e "\e[94mbond名称不能为空,请重新输入\e[0m"
else
echo -e "\e[94m输入的bond名称为:$bondname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $bondname IP地址: " bondip
ipcalc -cs $bondip
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的IP地址\e[0m"
else
echo -e "\e[94m输入的 $bondname IP地址为 $bondip \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $bondname IP地址掩码或者掩码前缀: " bondnetmask
if [[ -n "$bondnetmask" && "$bondnetmask" =~ "." ]];then
ipcalc -p $bondip $bondnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
else
bondmask=`ipcalc -p $bondip $bondnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94m输入的掩码前缀为 $bondmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $bondnetmask ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
fi
if [ -n "$bondnetmask" ];then
if [[ "$bondnetmask" -gt 1 && "$bondnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94m输入的掩码前缀为 $bondnetmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
bondmask=$bondnetmask
break;
else 
echo -e "\e[94m输入的子网掩码有误,请重新输入!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94m提示:当前系统已存在默认网关 $defaultgateway 如果配置bond网关,可能会导致网络不通,如需配置,请尝试添加静态路由!\e[0m"
sleep 3
fi
while true
do
read -p "请输入 $bondname 网关地址,如无网关,则直接回车: " bondgateway
if [ -n "$bondgateway" ];then
ipcalc -cs $bondgateway
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的网关地址\e[0m"
else
echo -e "\e[94m输入的为 $bondname 的网关为 $bondgateway \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
else
echo -e "\e[94m已配置 $bondname 网关为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
done
while true
do
read -p "请输入 $bondname 第一个DNS地址,如无,则直接回车: " bonddns1
if [ -n "$bonddns1" ];then
ipcalc -cs $bonddns1
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第一个DNS地址为 $bonddns1 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$bonddns1
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "请输入 $bondname 第二个DNS地址,如无,则直接回车: " bonddns2
if [ -n "$bonddns2" ];then
ipcalc -cs $bonddns2
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第二个DNS地址为 $bonddns2 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$bonddns2
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                      0,平衡轮循环策略                     #\e[0m"
echo -e "\e[94m#                      1,主备策略                           #\e[0m"
echo -e "\e[94m#                      2,平衡策略                           #\e[0m"
echo -e "\e[94m#                      3,广播策略                           #\e[0m"
echo -e "\e[94m#                      4,802.3ad动态链接聚合                #\e[0m"
echo -e "\e[94m#                      5,适配器传输负载均衡                 #\e[0m"
echo -e "\e[94m#                      6,适配器适应性负载均衡               #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "请选择绑定模式,输入数字即可: " bondmode
if [ "$bondmode" == 0 ];then
echo -e "\e[94m已选择bond模式为 0 即:(balance-rr0)(平衡轮循环策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 1 ];then
echo -e "\e[94m已选择bond模式为 1 即:(active-backup)(主备策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 2 ];then
echo -e "\e[94m已选择bond模式为 2 即:(balance-xor)(平衡策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 3 ];then
echo -e "\e[94m已选择bond模式为 3 即:(broadcast)(广播策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 4 ];then
echo -e "\e[94m已选择bond模式为 4 即:(802.3ad)(IEEE 802.3ad 动态链接聚合)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 5 ];then
echo -e "\e[94m已选择bond模式为 5 即:(balance-tlb)(适配器传输负载均衡)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 6 ];then
echo -e "\e[94m已选择bond模式为 6 即:(balance-alb)(适配器适应性负载均衡)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94m请选择正确的bond模式! \e[0m"
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
cat > /etc/sysconfig/network-scripts/ifcfg-$selectnic << EOF
DEVICE=$selectnic
BOOTPROTO=none
ONBOOT=yes
MASTER=$bondname
SLAVE=yes
USERCTL=no
NM_CONTROLLED=no
EOF
echo -e "\e[94m已配置 $bondname 成员 $selectnic \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
done
cat > /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF  
DEVICE=$bondname
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
NM_CONTROLLED=no
IPADDR=$bondip
PREFIX=$bondmask
EOF
if [ -n "$gateway" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
GATEWAY=$gateway
EOF
fi
if [ -n "$dns1" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS1=$dns1
EOF
fi
if [ -n "$dns2" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS2=$dns2
EOF
fi
if [ "$bondmode" == 4 ];then 
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer2+3"
EOF
else
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=$bondmode miimon=100"
EOF
fi
if [ "$oskernel" = "2.6.32" ]; then
service network restart > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl restart network > /dev/null 2>&1
elif [ "$oskernel" = "4.18.0" ]; then
systemctl restart network > /dev/null 2>&1
fi
echo -e "\e[94m$bondname信息\e[0m"
ip a s $bondname
bondinfo=`cat /proc/net/bonding/$bondname |grep Mode |awk -F ": " '{print$2}'`
echo -e "\e[94m已配置的 $bondname 模式为 $bondinfo \e[0m"
echo
cat /proc/net/bonding/$bondname
ls /etc/sysconfig/network-scripts/ifcfg-bond0 > /dev/null 2>&1
if [ $? -ne 0 ];then
for niclist in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [[ "$niclist" == "bond0" && "$bondname" != "bond0" ]];then
echo -bond0 > /sys/class/net/bonding_masters
fi
done
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m网卡聚合 $bondname 配置已完成!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
if [[ $selectmode == 2 ]];then
systemctl start NetworkManager > /dev/null 2>&1
systemctl enable NetworkManager > /dev/null 2>&1
while true
do
read -p "请输入team名称,如 team0 : " teamname1
teamname=`echo ${teamname1,,}`
for ifpresentteam in `cat /tmp/nicnew`
do
if [[ "$ifpresentteam" == "$teamname" ]];then
echo -e "\e[94m当前已存在 $teamname 请重新输入!\e[0m"
read -p "请输入team名称,如 team0 : " teamname
echo -e "\e[94m输入的team名称为:$teamname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $teamname ];then
echo -e "\e[94mteam名称不能为空,请重新输入\e[0m"
else
echo -e "\e[94m输入的team名称为:$teamname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $teamname IP地址: " teamip
ipcalc -cs $teamip
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的IP地址\e[0m"
else
echo -e "\e[94m输入的 $teamname IP地址为 $teamip \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $teamname IP地址掩码或者掩码前缀: " teamnetmask
if [[ -n "$teamnetmask" && "$teamnetmask" =~ "." ]];then
ipcalc -c $teamip $teamnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
else
teammask=`ipcalc -p $teamip $teamnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94m输入的掩码前缀为 $teammask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $teamnetmask ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
fi
if [ -n "$teamnetmask" ];then
if [[ "$teamnetmask" -gt 1 && "$teamnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94m输入的掩码前缀为 $teamnetmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammask=$teamnetmask
break;
else 
echo -e "\e[94m输入的子网掩码有误,请重新输入!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94m提示:当前系统已存在默认网关 $defaultgateway 如果配置team网关,可能会导致网络不通,如需配置,请尝试添加静态路由!\e[0m"
sleep 3
fi
while true
do
read -p "请输入 $teamname 网关地址,如无网关,则直接回车: " teamgateway
if [ -n "$teamgateway" ];then
ipcalc -cs $teamgateway
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的网关地址\e[0m"
else
echo -e "\e[94m输入的为 $teamname 的网关为 $teamgateway \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
else
echo -e "\e[94m已配置 $teamname 网关为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
done
while true
do
read -p "请输入 $teamname 第一个DNS地址,如无,则直接回车: " teamdns1
if [ -n "$teamdns1" ];then
ipcalc -cs $teamdns1
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第一个DNS地址为 $teamdns1 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$teamdns1
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "请输入 $teamname 第二个DNS地址,如无,则直接回车: " teamdns2
if [ -n "$teamdns2" ];then
ipcalc -cs $teamdns2
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第二个DNS地址为 $teamdns2 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$teamdns2
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1,广播容错                      #\e[0m"
echo -e "\e[94m#                           2,负载轮询                      #\e[0m"
echo -e "\e[94m#                           3,主备模式                      #\e[0m"
echo -e "\e[94m#                           4,负载均衡                      #\e[0m"
echo -e "\e[94m#                           5,LACP                          #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "请选择绑定模式,输入数字即可: " teammode
if [ "$teammode" == 1 ];then
echo -e "\e[94m已选择team模式为:(broadcast)(广播容错)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=broadcast
break;
elif [ "$teammode" == 2 ];then
echo -e "\e[94m已选择team模式为:(roundrobin)(负载轮询)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=roundrobin
break;
elif [ "$teammode" == 3 ];then
echo -e "\e[94m已选择team模式为:(activebackup)(主备模式)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=activebackup
break;
elif [ "$teammode" == 4 ];then
echo -e "\e[94m已选择team模式为:(loadbalance)(负载均衡)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=loadbalance
break;
elif [ "$teammode" == 5 ];then
echo -e "\e[94m已选择team模式为:(lacp)(LACP)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=lacp
break;
else
echo -e "\e[94m请选择正确的team模式! \e[0m"
fi
done
nmcli con add con-name $teamname type team ifname $teamname config '{"runner":{"name":"'$teammode'"}}' > /dev/null 2>&1
if [[ -n "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
nmcli con down $selectnic > /dev/null 2>&1
nmcli con add type team-slave con-name $teamname-port-$selectnic ifname $selectnic master $teamname > /dev/null 2>&1
done
nmcli con reload > /dev/null 2>&1
nmcli con up $teamname > /dev/null 2>&1
teamdctl $teamname state
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
systemctl restart network > /dev/null 2>&1
echo -e "\e[94m网卡聚合 $teamname 配置已完成!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
 
if [ $language == 2 ];then
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#   1.Linux NIC binding script,support bond and team mode    #\e[0m"
echo -e "\e[94m#   2.Support CentOS 6.x/7.x/8.x RedHat 6.x/7.x/8.x          #\e[0m"
echo -e "\e[94m#   3.Author: Mr.Mao                                         #\e[0m"
echo -e "\e[94m#   4.Compiled: March 14,2020                                #\e[0m"
echo -e "\e[94m#   5.More view https://www.mr-mao.cn                        #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;mTip:If the Backspace key does not work,press Ctrl+Backspace\e[0m"
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current operating system is $osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94mKernel version is 2.6.32 Series\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS only supports the bond binding mode!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
service NetworkManager stop > /dev/null 2>&1
chkconfig NetworkManager off > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mThe network service is not running normally. Please check the network service first!\e[0m"
systemctl status network
exit
fi
echo -e "\e[94mKernel version is 3.10.0 Series\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1.Bond Mode                      #\e[0m"
echo -e "\e[94m#                           2.Team Mode                      #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;mTip:choose one of them.It is not recommended to coexist in both modes!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS supports two binding modes:team and bond.Choose one: \e[0m"
while true
do
read -p "Select the mode and enter the number: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94mBond binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#Please check the NetworkManager!\e[0m"
exit
fi
echo -e "\e[94mTeam binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94mPlease select the correct binding mode!\e[0m"
fi
done
elif [ "$oskernel" = "4.18.0" ]; then
echo -e "\e[94mKernel version is 4.18.0 Series\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
systemctl start network > /dev/null 2>&1
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mThe current OS only supports the team binding mode!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mPlease check the NetworkManager!\e[0m"
exit
fi
sleep 1
break;
else
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                          1.Bond Mode                       #\e[0m"
echo -e "\e[94m#                          2.Team Mode                       #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94mTip:choose one of them. It is not recommended to coexist in both modes!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS supports two binding modes:team and bond.Choose one: \e[0m"
read -p "Select the mode and enter the number: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94mBond binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#Please check the NetworkManager!#\e[0m"
exit
fi
echo -e "\e[94mTeam binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
break;
else
echo -e "\e[94mPlease select the correct binding mode!\e[0m"
fi
fi
done
else
echo -e "\e[94mThis script does not support the current OS!\e[0m"
exit
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
mkdir -p /etc/sysconfig/network-scripts/nicsbak
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicall
for selnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr|: team|: bond" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}';`
do
selnicip=`ip a s $selnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selnicgateway=`ip route |grep default |grep $selnic |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selnic  $selnicip  $selnicgateway >> /tmp/nic
done
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
for selbond in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' |grep -E "team|bond"`;
do
selbondip=`ip a s $selbond | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selbondgateway=`ip route |grep default |grep $selbond |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selbond  $selbondip  $selbondgateway >> /tmp/presentbondall
done
nicall=`cat /tmp/nicall`
nicallnum=`cat /tmp/nic |wc -l`
if [ "$nicallnum" == 1 ];then
echo -e "\e[94mThe current host has only one NIC, no binding is required!\e[0m"
exit
fi
if [[ "$nicall" =~ "bond" ]];then
echo -e "\e[94mBond already exists in the current system!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: bond|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$detectnic" ];then
bondsubniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic`
if [[ "$bondsubniccontent" =~ "bond" ]];then
presentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94m$detectnic is already a member of $presentbond!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
fi
done
fi
if [[ "$nicall" =~ "team" ]];then
echo -e "\e[94mteam already exists in the current system!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ls /etc/sysconfig/network-scripts/ifcfg-team*-* | xargs -i basename {\}`;
do
teamsubniccontent=`cat /etc/sysconfig/network-scripts/$detectnic`
if [[ "$teamsubniccontent" =~ "team" ]];then
presentteam=`cat /etc/sysconfig/network-scripts/$detectnic |grep TEAM_MASTER |awk -F "=" '{print$2}'`
teamport=`cat /etc/sysconfig/network-scripts/$detectnic |grep '\<DEVICE\>' |awk -F "=" '{print$2}'`
echo $teamport $presentteam >> /tmp/teams
echo -e "\e[94m$teamport is already a member of $presentteam!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
fi
if [[ -f "/tmp/presentbondall" ]];then
echo -e "\e[94mThe current host already has a NIC aggregation group:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/presentbondall
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
echo -e "\e[94mAll NICs of the current host are as follows:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/nic |awk '{print NR,$0}'
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
read -p "Select the NIC used for binding, enter the number, multiple NIC, separated by spaces: " nicnum
selectnum=`echo $nicnum |awk -F " " '{print NF}'`
if [ $selectnum -lt 2 ];then
echo -e "\e[94mSelect at least two NICs, please choose again!\e[0m"
else
break;
fi
done
for num in $nicnum;
do
nicx=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
if [[ "$nicx" == "" ]];then
echo -e "\e[94mPlease select correct NIC!\e[0m"
exit
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicip=`ip a s $selectnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
if [ -n "$selectnicip" ];then
echo -e "\e[94mNIC $selectnic already has an IP address $selectnicip\e[0m"
read -p "Whether to continue? If you continue, the IP configuration of $selectnic will be deleted and the host communication may be interrupted! Press n to exit, press other keys to continue: " answer0
answer0lower=`echo $answer0 | tr [A-Z] [a-z]`
if [[ "$answer0lower" == "n" ]]; then
echo -e "\e[94mIf you need to bind, please re-execute the script and select another NICs!\e[0m"
exit
fi
fi
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$selectnic" ];then
sed -i '/ONBOOT/s/yes/no/' /etc/sysconfig/network-scripts/ifcfg-$selectnic
selectniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic`
selectnicinpresentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94mChosen NIC $selectnic\e[0m"
if [[ "$selectniccontent" =~ "bond" ]];then
echo -e "\e[94mNIC $selectnic is already a member of $selectnicinpresentbond!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "Whether to continue? If you continue, $selectnicinpresentbond will be deleted! Press n to exit, press other keys to continue: " answer1
answer1lower=`echo $answer1 | tr [A-Z] [a-z]`
if [[ "$answer1lower" == "n" ]]; then
echo -e "\e[94mIf you need to bind, please re-execute the script and select another NICs!\e[0m"
exit
else
echo -e "\e[94mNIC $selectnic has been selected!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentbond" ]];then
ifdown $selectnicinpresentbond > /dev/null 2>&1
echo -e "\e[94mNIC $selectnicinpresentbond has been disabled!\e[0m"
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond
echo -$selectnicinpresentbond > /sys/class/net/bonding_masters
echo -e "\e[94mNIC $selectnicinpresentbond configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnic /etc/sysconfig/network-scripts/ifcfg-$selectnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnic
echo -e "\e[94mNIC $selectnic configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
sleep 1
else
echo -e "\e[94mNIC $selectnic has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
if [ -f "/tmp/teams" ];then
for num in $nicnum;
do
selectteamnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicinpresentteam=`cat /tmp/teams |grep $selectteamnic |awk -F " " '{print$2}'`
echo -e "\e[94mChosen NIC $selectteamnic\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [[ "$selectnicinpresentteam" =~ "team" ]];then
echo -e "\e[94mNIC $selectteamnic is already a member of $selectnicinpresentteam!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "Whether to continue? If you continue, $selectnicinpresentteam will be deleted! Press n to exit, press other keys to continue:" answer2
answer2lower=`echo $answer2 | tr [A-Z] [a-z]`
if [[ "$answer2lower" == "n" ]]; then
echo -e "\e[94mIf you need to bind, please re-execute the script and select another NICs!\e[0m"
exit
else
echo -e "\e[94mNIC $selectteamnic has been selected!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentteam" ]];then
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* /etc/sysconfig/network-scripts/nicsbak/
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
for disteam in `ls /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* | xargs -i basename {\} |awk -F "ifcfg-" '{print$2}'`
do
cp -f /etc/sysconfig/network-scripts/ifcfg-$disteam /etc/sysconfig/network-scripts/ifcfg-$disteam-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
nmcli con delete $disteam > /dev/null 2>&1
done 
echo -e "\e[94mNIC $selectnicinpresentteam has been disabled!\e[0m"
echo -e "\e[94mNIC $selectnicinpresentteam configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectteamnic /etc/sysconfig/network-scripts/ifcfg-$selectteamnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
echo -e "\e[94mNIC $selectteamnic configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
else
echo -e "\e[94mNIC $selectteamnic has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
sleep 1
fi
done
fi
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
while true
do
read -p "Please enter a bond name, such as bond0: " bondname1
bondname=`echo ${bondname1,,}`
for ifpresentbond in `cat /tmp/nicnew`
do
if [[ "$ifpresentbond" == "$bondname" ]];then
echo -e "\e[94m$bondname currently exists Please re-enter!\e[0m"
read -p "Please enter a bond name, such as bond0: " bondname
echo -e "\e[94mThe bond name entered is: $bondname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $bondname ];then
echo -e "\e[94mbond name cannot be empty, please re-enter!\e[0m"
else
echo -e "\e[94mThe bond name entered is: $bondname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $bondname IP address: " bondip
ipcalc -cs $bondip
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid IP address!\e[0m"
else
echo -e "\e[94mThe entered IP address of $bondname is $bondip\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $bondname IP netmask address or netmask prefix: " bondnetmask
if [[ -n "$bondnetmask" && "$bondnetmask" =~ "." ]];then
ipcalc -p $bondip $bondnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
else
bondmask=`ipcalc -p $bondip $bondnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94mThe entered netmask prefix is $bondmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $bondnetmask ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi
if [ -n "$bondnetmask" ];then
if [[ "$bondnetmask" -gt 1 && "$bondnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94mThe entered netmask prefix is $bondmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
bondmask=$bondnetmask
break;
else 
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94mTip: A default gateway already exists in the current system:$defaultgateway,If the bond gateway is configured, it may cause network disconnection. If you need to configure it, please try to add a static route!\e[0m"
sleep 3
fi
while true
do
read -p "Please enter the $bondname gateway address, if there is no gateway, press Enter: " bondgateway
if [ -n "$bondgateway" ];then
ipcalc -cs $bondgateway
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid gateway address!\e[0m"
else
echo -e "\e[94mThe gateway for $bondname entered is $bondgateway\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
else
echo -e "\e[94mThe configured $bondname gateway is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
done
while true
do
read -p "Please enter the first DNS address of $bondname, if there is no DNS, press Enter: " bonddns1
if [ -n "$bonddns1" ];then
ipcalc -cs $bonddns1
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe first DNS address entered is $bonddns1\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$bonddns1
break;
fi
else
echo -e "\e[94mThe configured $bondname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "Please enter the second DNS address of $bondname, if there is no DNS, press Enter: " bonddns2
if [ -n "$bonddns2" ];then
ipcalc -cs $bonddns2
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe second DNS address entered is $bonddns1\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$bonddns2
break;
fi
else
echo -e "\e[94mThe configured $bondname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#         0,Round-robin policy                              #\e[0m"
echo -e "\e[94m#         1,Active-backup policy                            #\e[0m"
echo -e "\e[94m#         2,XOR policy                                      #\e[0m"
echo -e "\e[94m#         3,Broadcast policy                                #\e[0m"
echo -e "\e[94m#         4,802.3ad Dynamic link aggregation(LACP)          #\e[0m"
echo -e "\e[94m#         5,Adaptive transmit load balancing policy         #\e[0m"
echo -e "\e[94m#         6,Adaptive load balancing policy                  #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "Please select the binding mode and enter the number: " bondmode
if [ "$bondmode" == 0 ];then
echo -e "\e[94mThe selected bond mode is (balance-rr0)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 1 ];then
echo -e "\e[94mThe selected bond mode is (active-backup)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 2 ];then
echo -e "\e[94mThe selected bond mode is (balance-xor)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 3 ];then
echo -e "\e[94mThe selected bond mode is (broadcast)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 4 ];then
echo -e "\e[94mThe selected bond mode is (LACP)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 5 ];then
echo -e "\e[94mThe selected bond mode is (balance-tlb)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 6 ];then
echo -e "\e[94mThe selected bond mode is (balance-alb)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94mPlease select the correct bond mode!\e[0m"
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
cat > /etc/sysconfig/network-scripts/ifcfg-$selectnic << EOF
DEVICE=$selectnic
BOOTPROTO=none
ONBOOT=yes
MASTER=$bondname
SLAVE=yes
USERCTL=no
NM_CONTROLLED=no
EOF
echo -e "\e[94m$bondname member $selectnic has been configured\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
done
cat > /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF  
DEVICE=$bondname
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
NM_CONTROLLED=no
IPADDR=$bondip
PREFIX=$bondmask
EOF
if [ -n "$gateway" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
GATEWAY=$gateway
EOF
fi
if [ -n "$dns1" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS1=$dns1
EOF
fi
if [ -n "$dns2" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS2=$dns2
EOF
fi
if [ "$bondmode" == 4 ];then 
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer2+3"
EOF
else
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=$bondmode miimon=100"
EOF
fi
if [ "$oskernel" = "2.6.32" ]; then
service network restart > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl restart network > /dev/null 2>&1
elif [ "$oskernel" = "4.18.0" ]; then
systemctl restart network > /dev/null 2>&1
fi
echo -e "\e[94m$bondname information\e[0m"
ip a s $bondname
bondinfo=`cat /proc/net/bonding/$bondname |grep Mode |awk -F ": " '{print$2}'`
echo -e "\e[94mThe configured $bondname mode is $bondinfo\e[0m"
echo
cat /proc/net/bonding/$bondname
ls /etc/sysconfig/network-scripts/ifcfg-bond0 > /dev/null 2>&1
if [ $? -ne 0 ];then
for niclist in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [[ "$niclist" == "bond0" && "$bondname" != "bond0" ]];then
echo -bond0 > /sys/class/net/bonding_masters
fi
done
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mNIC aggregation $bondname configuration is completed!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
if [[ $selectmode == 2 ]];then
systemctl start NetworkManager > /dev/null 2>&1
systemctl enable NetworkManager > /dev/null 2>&1
while true
do
read -p "Please enter a team name, such as team0: " teamname1
teamname=`echo ${teamname1,,}`
for ifpresentteam in `cat /tmp/nicnew`
do
if [[ "$ifpresentteam" == "$teamname" ]];then
echo -e "\e[94m$teamname currently exists Please re-enter!\e[0m"
read -p "Please enter a team name, such as team0: " teamname
echo -e "\e[94mThe team name entered is: $teamname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $teamname ];then
echo -e "\e[94mteamname cannot be empty, please re-enter!\e[0m"
else
echo -e "\e[94mThe teamname entered is: $teamname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $teamname IP address: " teamip
ipcalc -cs $teamip
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid IP address!\e[0m"
else
echo -e "\e[94mThe entered IP address of $teamname is $teamip \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $teamname netmask or netmask prefix: " teamnetmask
if [[ -n "$teamnetmask" && "$teamnetmask" =~ "." ]];then
ipcalc -c $teamip $teamnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
else
teammask=`ipcalc -p $teamip $teamnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94mThe entered netmask prefix is $teammask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $teamnetmask ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi
if [ -n "$teamnetmask" ];then
if [[ "$teamnetmask" -gt 1 && "$teamnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94mThe entered netmask prefix is $teamnetmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammask=$teamnetmask
break;
else 
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94mTip: A default gateway already exists in the current system:$defaultgateway,If the team gateway is configured, it may cause network disconnection. If you need to configure it, please try to add a static route!\e[0m"
sleep 3
fi
while true
do
read -p "Please enter the $teamname gateway address, if there is no gateway, press Enter: " teamgateway
if [ -n "$teamgateway" ];then
ipcalc -cs $teamgateway
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid gateway address!\e[0m"
else
echo -e "\e[94mThe gateway for $teamname entered is $teamgateway\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
else
echo -e "\e[94mThe configured $teamname gateway is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
done
while true
do
read -p "Please enter the first DNS address of $teamname, if there is no DNS, press Enter: " teamdns1
if [ -n "$teamdns1" ];then
ipcalc -cs $teamdns1
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe first DNS address entered is $teamdns1\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$teamdns1
break;
fi
else
echo -e "\e[94mThe configured $bondname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "Please enter the second DNS address of $teamname, if there is no DNS, press Enter: " teamdns2
if [ -n "$teamdns2" ];then
ipcalc -cs $teamdns2
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe second DNS address entered is $teamdns2 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$teamdns2
break;
fi
else
echo -e "\e[94mThe configured $teamname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                    1,broadcast policy                     #\e[0m"
echo -e "\e[94m#                    2,roundrobin policy                    #\e[0m"
echo -e "\e[94m#                    3,activebackup policy                  #\e[0m"
echo -e "\e[94m#                    4,loadbalance policy                   #\e[0m"
echo -e "\e[94m#                    5,lacp policy                          #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "Please select the binding mode and enter the number: " teammode
if [ "$teammode" == 1 ];then
echo -e "\e[94mThe selected team mode is (broadcast)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=broadcast
break;
elif [ "$teammode" == 2 ];then
echo -e "\e[94mThe selected team mode is (roundrobin)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=roundrobin
break;
elif [ "$teammode" == 3 ];then
echo -e "\e[94mThe selected team mode is (activebackup)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=activebackup
break;
elif [ "$teammode" == 4 ];then
echo -e "\e[94mThe selected team mode is (loadbalance)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=loadbalance
break;
elif [ "$teammode" == 5 ];then
echo -e "\e[94mThe selected team mode is (lacp)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=lacp
break;
else
echo -e "\e[94mPlease select the correct team mode!\e[0m"
fi
done
nmcli con add con-name $teamname type team ifname $teamname config '{"runner":{"name":"'$teammode'"}}' > /dev/null 2>&1
if [[ -n "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
nmcli con down $selectnic > /dev/null 2>&1
nmcli con add type team-slave con-name $teamname-port-$selectnic ifname $selectnic master $teamname > /dev/null 2>&1
done
nmcli con reload > /dev/null 2>&1
nmcli con up $teamname > /dev/null 2>&1
teamdctl $teamname state
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
systemctl restart network > /dev/null 2>&1
echo -e "\e[94mNIC aggregation $teamname configuration is completed!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi