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

 

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

最新文章

  1. linux中grep的应用
  2. soapUI测试webservice(参数为xml格式的处理方式)
  3. 【elasticsearch】python下的使用
  4. iOS-Xcode上传后iTunes Connect构建版本不显示
  5. UWP开发中的流媒体
  6. C# 小型资源管理器
  7. WPF 面试题及答案(三)
  8. 触发器创建及Navicat中使用
  9. C语言中的宏总结
  10. 使用PULL方式解析XML资源文件下面的xml文件
  11. HtmlAgilityPack 抓取页面的乱码处理
  12. angular-ui-bootstrap插件API - Pager
  13. Maven pom.xml配置详解
  14. Alpha冲刺Day8
  15. CentOS 7 下面使用 sendMail 发送邮件
  16. Properties类学习笔记
  17. 实现类QQ的编辑框
  18. PhpStorm,Pycharm,Goland破解
  19. Linux下C语言操作MySQL数据库
  20. 最简单的记录程序运行时间的方法:[记录PHP程序运行消耗时间]

热门文章

  1. a += 20 和 a = a+20前者不报错,后者报错的原因
  2. 使用 Windows 包管理器 (winget) 安装 .Net
  3. WPF开发经验-实现自带触控键盘的TextBox
  4. Go中的闭包、递归
  5. IPv4 与 IPv6的区别
  6. 小白转行入门STM32----手机蓝牙控制STM32单片机点亮LED
  7. AgileBoot - 项目内统一的错误码设计
  8. Dapr v1.9.0 版本已发布
  9. Doris开发手记4:倍速性能提升,向量化导入的性能调优实践
  10. c语言KMP匹配算法与字符串替换算法