#!/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