Expect 脚本出现问题

发布于 2024-07-10 20:29:43 字数 1938 浏览 15 评论 0原文

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

知道为什么服务器上会出错吗? 谢谢

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.

This is the script:

#!/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤凫 2024-07-17 20:29:43

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

Maybe the password policy is different on the server?

怀念你的温柔 2024-07-17 20:29:43

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

尝试将“exp_internal 1”添加到您的脚本中。 这将为您提供更详细的信息,说明为什么您的期望不匹配。

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

This is an old question, but anyway...

Try adding "exp_internal 1" to your script. That will give you more verbose information about why your expects aren't matching.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文