marty001 发表于 2018-8-28 09:31:40

shell脚本自动化--bond

#!/bin/bash  
###version 1.1####
  
if [ `whoami` = "root" ];then
  
echo "user:root"
  
   else
  
echo "you need root."
  
exit
  
fi
  
####获取IP,NETMASK,GATEWAY等信息###
  
read -p"please input IPaddr:" IPaddr
  
read -p"please input netmask:" Netmask
  
read -p"please input gateway:" Gateway
  

  
###关闭NetworkManager###
  
systemctl stop NetworkManager.service
  
systemctl disable NetworkManager.service
  
echo "NetworkManager.service have stopped"
  

  
####获取两个网卡的名称和MAC####
  
cd /etc/sysconfig/network-scripts/
  
eth=`ls | grep ifcfg | grep e | awk 'NR==1'| cut -b 7-9`
  
eth_1=`ifconfig -a | grep $eth| awk -F: '{print $1}' | awk 'NR==1'`
  
eth_2=`ifconfig -a | grep $eth| awk -F: '{print $1}' | awk 'NR==2'`
  
eth_1_MAC=`ifconfig -a | grep ether | awk '{print $2}' | awk 'NR==1'`
  
eth_2_MAC=`ifconfig -a | grep ether | awk '{print $2}' | awk 'NR==2'`
  

  
####写入配置文件######
  
###bond0###
  
echo "###bond0###"> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "DEVICE=bond0" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "IPADDR=$IPaddr" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "NETMASK=$Netmask" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "GATEWAY=$Gateway" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "BONDING_OPTS="mode=1 miimon=100"" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-bond0
  
echo "already config ifcfg-bond0!"
  

  
####eth_1####
  
echo "###ifcfg_$eth_1###"> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "NAME=$eth_1" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "HWADDR=$eth_1_MAC" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "TYPE="Ethernet"" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "ONBOOT="yes"" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "MASTER=bond0" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-$eth_1
  
echo "already config ifcfg-$eth_1!"
  

  
####eth_2####
  
echo "###ifcfg_$eth_2###"> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "NAME=$eth_2" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "HWADDR=$eth_2_MAC" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "TYPE="Ethernet"" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "ONBOOT="yes"" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "MASTER=bond0" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "SLAVE=yes" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-$eth_2
  
echo "already config ifcfg-$eth_2!"
  

  
#####alias###########
  
echo "alias bond0 bonding">> /etc/modprobe.d/bond0.conf
  

  
#####重启network服务####
  
echo "system will reboot the network....."
  
chkconfig network on
  
/etc/init.d/network restart
  

  
echo "you need to reboot your system!"


页: [1]
查看完整版本: shell脚本自动化--bond