shell-read交互配置hostname、bond、salt、ip

2023-02-24 17:13:31 浏览数 (1)

​ 公司生产环境每次安装新服务器之后都会安装salt,配置hostname、bond等。刚好自己最近在学习shell。然后就有了下面的脚本。(如果有需要还可以扩展安装zabbix-agent、Megacli等。)

代码语言:javascript复制
#!/bin/bash
#=========set hostname====================
stty erase ^H		#避免read交互是按退格键出现 ^H
read -p "Please enter hostname:" hostname
hostnamectl set-hostname $hostname

#=========config bond=====================
stty erase ^H
read -p "Please enter your IP:" ip
GW=`echo $ip |awk -F "." '{print $1"."$2"."$3"."1}'`

/usr/bin/ping -c 3 $ip > /dev/null 2>&1
if [ $? -eq 0 ];then
        echo "$ip existing"
        exit 1
else
        echo "ip ok!"
fi

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
MASTER=bond0
SLAVE=yes
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << EOF
DEVICE=bond0
ONBOOT=yes
BOOTPROTO=static
IPADDR=$ip
NETMASK=255.255.255.0
GATEWAY=$GW
DNS1=114.114.114.114
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-eth1 << EOF
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
MASTER=bond1
SLAVE=yes
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-bond1 << EOF
DEVICE=bond1
ONBOOT=yes
BOOTPROTO=static
EOF

cat > /etc/modprobe.d/bonding.conf << EOF
alias bond0 bonding
options bond0 miimon=100 mode=0
alias bond1 bonding
options bond1 miimon=100 mode=0
EOF

systemctl stop NetworkManager
systemctl disable NetworkManager
systemctl restart network
sleep 3

#===================install salt=====================
yum install -y salt-minion
sleep 1
sed -i 's/#master: salt/master: ip/g' /etc/salt/minion	#这里的ip替换成你的masterip地址
sed -i "s/#id:/id: $hostname/g" /etc/salt/minion
sleep 1

systemctl enable salt-minion
systemctl start salt-minion

0 人点赞