利用expect做密码自动更改随机密码

回复 收藏
每天给所有的主机更改一个随机密码:
在模版机上:
vim gm.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd [lindex $argv 1]
set cm [lindex $argv 2]
spawn ssh root@$host

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "passwd\r"
expect "assword:"
send "$cm\r"
expect "new password:"
send "$cm\r"
expect "]*"
send "exit\r"

写一个shell脚本
vim gm.sh
#!/bin/bash
if [ ! -f /tmp/zidongmima ]
then
echo “原始密码” > /tmp/zidongmima
fi
passwd=linux$RANDOM
echo $passwd >> /tmp/zidongmima
passwd2=tail -2 zidongmima |sed -n ‘1’p
for ip in `cat ip.txt`
do
echo $ip
sh gm.expect $ip $passwd2 $passwd
done
然后把我们的shell脚本加入到计划任务中就可以了
2016-03-23 16:52 举报
已邀请:
0

李宏伟

赞同来自:

没有测试过,突发奇想来的
0

刘野

赞同来自:

在吗

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: