#!/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
脚本逻辑:
1、从命令行读取用户名密码
挨个登录服务器 -> 切到root--》修改文件--退出root--退出服务器 --- 循环
注意:
如果机器没有安装 except , 需要安装 expect
centos 可以简单安装 yum install expect*