期望脚本的问题

问题描述 投票:1回答:2

我正在尝试创建一个脚本以非交互方式更新密码。它在我的笔记本电脑上运行但在我的服务器上失败了。两者都使用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

在服务器上,输出如下所示:

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

所以不起作用,因为这对夫妇期望发送似乎不同步。

但奇怪的是,在我的笔记本电脑上工作:

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

任何想法为什么它在服务器上出错?谢谢

expect passwd getch
2个回答
0
投票

也许服务器上的密码策略不同?


0
投票

这是一个老问题,但无论如何......

尝试在脚本中添加“exp_internal 1”。这将为您提供更详细的信息,说明您的预期不匹配的原因。

对expect命令使用“-re”选项。您使用'$'来锚定模式,但expect的默认匹配是-glob,其中'$'并不特殊。

© www.soinside.com 2019 - 2024. All rights reserved.