Linux通过脚本实现多台主机统一部署
2024-10-20 16:49:37
该脚本分成三部分,一部分是获取信息的脚本: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`)
这仨脚本放一个文件夹就行,有问题请留言!
最新文章
- linux中grep的应用
- soapUI测试webservice(参数为xml格式的处理方式)
- 【elasticsearch】python下的使用
- iOS-Xcode上传后iTunes Connect构建版本不显示
- UWP开发中的流媒体
- C# 小型资源管理器
- WPF 面试题及答案(三)
- 触发器创建及Navicat中使用
- C语言中的宏总结
- 使用PULL方式解析XML资源文件下面的xml文件
- HtmlAgilityPack 抓取页面的乱码处理
- angular-ui-bootstrap插件API - Pager
- Maven pom.xml配置详解
- Alpha冲刺Day8
- CentOS 7 下面使用 sendMail 发送邮件
- Properties类学习笔记
- 实现类QQ的编辑框
- PhpStorm,Pycharm,Goland破解
- Linux下C语言操作MySQL数据库
- 最简单的记录程序运行时间的方法:[记录PHP程序运行消耗时间]