expect 批量修改root密码

oadb01:/root/sbin/expect# cat expect.sh 
while read line
do
user=`echo $line | awk '{print $1}'`
ip=`echo $line |awk '{print $2}'`
passwd=`echo $line | awk '{print $3}'`
newpasswd=`echo $line | awk '{print $4}'`
#把shell中的$user $ip $passwd参数传递给expect脚本
expect   pass.exp $user $ip $passwd $newpasswd
done < name.tmp

oadb01:/root/sbin/expect# cat name.tmp 
root  121.40.31.108  xxxxx xxx
root  121.40.116.25  xxxx xxxxx
oadb01:/root/sbin/expect# cat pass.exp 
#!/usr/bin/expect
 # 设置超时时间为 60 秒
# set timeout  60
 # 设置要登录的主机 IP 地址
# set host=$host
 # 设置以什么名字的用户登录
# set ip=$ip
 # 设置用户名的登录密码
# set passwd $passwd
 
#把shell的参数传递给expect脚本
set user [lindex $argv 0]
set ip  [lindex $argv 1]
set passwd  [lindex $argv 2]
set newpasswd  [lindex $argv 3]

 # 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;等到回答后,在提示输出密码;之后就直接提示输入密码
spawn ssh $ip "echo '$newpasswd'| passwd --stdin root" 
expect {
    "(yes/no)?" {
        send "yes\n"
        expect "password:"
        send "$passwd\n"
    }
        "password:" {
        send "$passwd\n"
    }
 }
send "exit\r"
expect eof

oadb01:/root/sbin/expect# cat expect.sh 
while read line
do
user=`echo $line | awk '{print $1}'`
ip=`echo $line |awk '{print $2}'`
passwd=`echo $line | awk '{print $3}'`
newpasswd=`echo $line | awk '{print $4}'`
#把shell中的$user $ip $passwd参数传递给expect脚本
expect   pass.exp $user $ip $passwd $newpasswd
done < name.tmp

oadb01:/root/sbin/expect# cat name.tmp 
root  x.x.x.x 1111111 222222
root  x.x.x.x  11111111 222222

你可能感兴趣的:(expect 批量修改root密码)