红联Linux门户
Linux帮助

简单SHELL程序

发布时间:2006-11-05 01:21:02来源:红联作者:pinkfirex
利用expect编写一个修改用户密码的程序changepassword.sh,该程序运行过程如下:

1)输入下面的命令:
./changepassword.sh root 123456

2)程序输出:
spawn passwd root
Enter new UNIX password:123456
Retype new UNIX password:123456
passwd: password updated successfully

答案:

#!/usr/bin/expect -f
set root [lindex $argv 0]
set passwd [lindex $argv 1]
spawn passwd $root #相当于 passwd root
expect "word:" #屏莫出现第一个输入时
send "$passwd\r" #输入变量$passwd
expect "word:" #屏莫出现第二个输入时
send "$passwd\r" #输入变量$passwd
expect eof
文章评论

共有 0 条评论