Linux通过脚本实现多台主机统一部署
该脚本分成三部分,一部分是获取信息的脚本:getInfo.sh 一个是main脚本:main.sh、一个是ssh连接主机脚本:sshing.sh
main.sh
#是否检查主机是否存活
host_check=`cat ./install.command | grep "ENABLE" | cut -d"=" -f2`
#从哪个主机开始发送命令 start_host=`cat ./install.command | grep "START" | cut -d"=" -f2`
#结束的主机 end_host=`cat ./install.command | grep "END" | cut -d"=" -f2`
#其他主机root的密码 default_passwd=`cat ./install.command | grep "PASSWD" | cut -d"=" -f2`
#要执行的命令
command=`cat ./install.command | grep "COMMAND" | cut -d"=" -f2` echo "the start host: $start_host" echo "the end host: $end_host" echo "check the host? $host_check" #if check the host if [ $host_check = yes ] then while [ $start_host -le $end_host ] do hostname="192.168.3.$start_host" ping -c 1 $hostname start_host=$[$start_host+1] done . ./getinfo.sh onlyip=() #去重 for i in "${live_host[@]}" do if [ ${#onlyip[*]} -eq 0 ] then onlyip[ ${#onlyip[*]} ]=$i else index=1 for op in "${onlyip[@]}" do if [ "$i" = "$op" ] then break fi if [ $index -eq ${#onlyip[*]} ] then onlyip[ ${#onlyip[*]} ]=$i break fi index=$[$index+1] done fi done echo "the live host: " echo "${onlyip[@]}" echo "start run!!!!" for ip in "${onlyip[@]}" do . ./sshing.sh done echo "end" else echo "start run!!!!" while [ $start_host -le $end_host ] do ip="192.168.3.$start_host" . ./sshing.sh start_host=$[$start_host+1] done echo "end" fi
sshing.sh
/usr/bin/expect <<EOF spawn ssh -o "StrictHostKeyChecking no" root@$ip expect "password:" send "$default_passwd\r" expect "#" send "$command\r" expect "#" send "exit\r" expect eof EOF
getinfo.sh
#将存活的主机ip存在数组中 live_host=(`sudo cat /var/log/kern | grep "Live-host" | grep "SRC=" | awk {'print $9'} | cut -d"=" -f2`)
这仨脚本放一个文件夹就行,有问题请留言!