1. 程式人生 > >centos7--shell腳本自動實現bond配置-第二版

centos7--shell腳本自動實現bond配置-第二版

eth int 一個 ip地址 pts ger probe 輸入 shel

#!/bin/bash
#定義函數
function bond0()
{
cat > /etc/sysconfig/network-scripts/ifcfg-bond0 <<EOF
DEVICE=bond0
TYPE=bond
NAME=bond0
BONDING_MASTER=yes
BOOTPROTO=static
USERCTL=no
ONBOOT=yes
IPADDR=$bond0_ipaddr
PREFIX=24
BONDING_OPTS="mode=4 miimon=100"
EOF
cat > /etc/sysconfig/network << EOF
GATEWAY=$bond0_gateway
EOF
}
function eth1()
{
cat > /etc/sysconfig/network-scripts/ifcfg-$ETH1 <<EOF
TYPE=Ethernet
BOOTPROTO=none
DEVICE=$ETH1
ONBOOT=yes
MASTER=bond0
SLAVE=yes
EOF
}

function eth2()
{
cat > /etc/sysconfig/network-scripts/ifcfg-$ETH2 <<EOF
TYPE=Ethernet
BOOTPROTO=none
DEVICE=$ETH2
ONBOOT=yes
MASTER=bond0
SLAVE=yes
EOF

}
#加載bonding模塊
modprobe bonding
#獲取基本信息
echo -n "請輸入需要綁定第一個網卡名稱(如:eth1):"
read ETH1
echo -n "請輸入需要綁定第二個網卡名稱(如:eth2):"
read ETH2
echo -n "請輸入bond0IP地址:"
read bond0_ipaddr
echo -n "請輸入bond0網關地址:"
read bond0_gateway
#確定網卡端口是否是萬兆
a=$(ethtool $ETH1 |grep ‘Supported ports‘ |awk ‘{print $(NF-1)}‘)
b=$(ethtool $ETH2 |grep ‘Supported ports‘ |awk ‘{print $(NF-1)}‘)
if [ $a = $b ]
then
bond0
eth1
eth2
else
echo "網卡非萬兆,請重新運行程序"
bash bond.sh
fi
systemctl stop NetworkManager.service
systemctl disable NetworkManager.service
systemctl restart network.service
ping $bond0_gateway -c 1

centos7--shell腳本自動實現bond配置-第二版