#!/bin/bash echo "enter heno's password" read henoPassword echo "enter root's password" read rootPassword for i in 11 12 13 14 15 16 17 18 19 20 21; do ip="192.168.10."$i comand="ssh -o StrictHostKeyChecking=no heno@$ip" expect -c " set timeout 60; spawn $comand; expect { \"terry@$ip's password:\" {send \"$henoPassword\r\"; exp_continue} \"terry\" {send \"su -\r\"; exp_continue} \"Password:\" {send \"$rootPassword\r\"; exp_continue} \"root@\" {send \"sed -i 's/192.168.* analysis.xxxx.com/192.168.10.221 analysis.xxxx.com/g' /etc/hosts\rexit\rexit\r\"; exp_continue} } " done
脚本逻辑:
shell
一、从命令行读取用户名密码
centos
挨个登陆服务器 -> 切到root--》修改文件--退出root--退出服务器 --- 循环bash
注意:服务器
若是机器没有安装 except , 须要安装 expect
ssh
centos 能够简单安装 yum install expect*
spa