ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux通过脚本实现多台主机统一部署

2022-09-12 21:31:29  阅读:331  来源: 互联网

标签:grep 主机 Linux echo start host sh onlyip 多台


该脚本分成三部分,一部分是获取信息的脚本: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`)

 

 

这仨脚本放一个文件夹就行,有问题请留言!

 

标签:grep,主机,Linux,echo,start,host,sh,onlyip,多台
来源: https://www.cnblogs.com/loe-home/p/16654100.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有