期望脚本有问题 [英] Problem with an expect script
问题描述
我正在尝试创建脚本来更新服务器中的密码 非交互方式.它可以在我的笔记本电脑上运行,但是在我的服务器上无法运行. 两者都使用Etch运行相同的配置.
I'm trying to create a script to update a password in a non-interactive way. It's working on my laptop but fails on my server. Both are running the same configuration, using Etch.
这是脚本:
#!/usr/bin/expect -f
# Change user passwd
set timeout 30
strace 4
set password [lindex $argv 1]
set old_password [lindex $argv 2]
spawn passwd [lindex $argv 0]
sleep 1
expect "(current) UNIX password: $"
send "$old_password\r"
expect "Enter new UNIX password: $"
send "$password\r"
expect "Retype new UNIX password: $"
send "$password\r"
expect eof
在服务器上,输出如下所示:
On the server the output looks like this:
myuser@server:~$ /home/myuser/adm/chpasswd myuser NewPasswd OldPasswd
2 lindex $argv 0
1 set username [lindex $argv 0]
2 lindex $argv 1
1 set password [lindex $argv 1]
2 lindex $argv 2
1 set old_password [lindex $argv 2]
1 spawn passwd $username
spawn passwd myuser
1 sleep 1
1 expect "(current) UNIX password: $"
Changing password for myuser
(current) UNIX password: 1 send "$old_password\r"
1 expect "Enter new UNIX password: $"
OldPasswd
Enter new UNIX password: 1 send "$password\r"
1 expect "Retype new UNIX password: $"
NewPasswd
1 send "$password\r"
1 expect eof
Retype new UNIX password: 1 exit 0
因此无法正常工作,因为这对夫妻的期望发送似乎不同步.
So is not working, as the couple expect-send seems unsync.
但奇怪的是,在我的笔记本电脑上它可以工作:
But strangely enough, on my laptop it works:
test@mars:/home/test/adm$ ./chpasswd test NewPasswd OldPasswd
2 lindex $argv 1
1 set password [lindex $argv 1]
2 lindex $argv 2
1 set old_password [lindex $argv 2]
2 lindex $argv 0
1 spawn passwd [lindex $argv 0]
spawn passwd test
1 sleep 1
1 expect "(current) UNIX password: $"
Changing password for test
(current) UNIX password: 1 send "$old_password\r"
1 expect "Enter new UNIX password: $"
OldPasswd
Enter new UNIX password: 1 send "$password\r"
1 expect "Retype new UNIX password: $"
NewPasswd
Retype new UNIX password: 1 send "$password\r"
1 expect eof
NewPasswd
passwd: password updated successfully
1 exit 0
任何想法为什么服务器上会出错? 谢谢
Any ideas why it's going wrong on the server ? Thanks
推荐答案
这是一个老问题,但无论如何...
This is an old question, but anyway...
尝试在脚本中添加"exp_internal 1".这样可以为您提供更多详细信息,说明您的期望为何不匹配.
Try adding "exp_internal 1" to your script. That will give you more verbose information about why your expects aren't matching.
为您的期望命令使用"-re"选项.您正在使用"$"锚定模式,但是Expect的默认匹配项是-glob,其中"$"并不特殊.
Use the "-re" option for your expect commands. You're using '$' to anchor the pattern, but the default matching for expect is -glob where '$' is not special.
这篇关于期望脚本有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!