第三方工具expect,专门用来实现需要交互式操作的命令的自动无人工干预操作。这些命令常见的有:passwd、fsck、telnet、ftp等。
1、创建用户/密码对文件
# cat user.password
usr2:abcd2
usr3:abcd3
#=========END=======================================================
2、创建expect.sh脚本文件
# cat expect.sh
#!/usr/local/bin/expect
if {$argc<2} {
send_user "usage: $argv0 username password\n"
exit
}
set username [lindex $argv 0]
set password [lindex $argv 1]
send_user "execute:$argv0 $username $password\n"
spawn -noecho passwd [lindex $argv 0] #-noecho不回显spawn正在调用的命令
expect {
"*口令*" {send "$password\n"}
"*word*" {send "$password\n"}
}
expect {
"*口令*" {send "$password\n"}
"*word*" {send "$password\n"}
}
#expect "*word*"
#send "$password\n"
expect eof
exit
#=========END=======================================================
3、创建autochg.sh文件,在此文件中调用expect.sh
# cat autochg.sh
#!/usr/bin/bash
for i in `cat $1`
do
USERNAME=${i%:*} #自右起第一个冒号后的字符都不要
PASSWORD=${i#*:} #自左起第一个冒号前的字符都不要
./expect.sh $USERNAME $PASSWORD
done
#=========END=======================================================
# chmod u+x expect.sh autochg.sh
# ./autochg.sh user.password
阅读(1908) | 评论(0) | 转发(0) |