Problem with an expect script

心不动则不痛 提交于 2019-12-13 01:28:41

问题


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


回答1:


Maybe the password policy is different on the server?




回答2:


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.



来源:https://stackoverflow.com/questions/385980/problem-with-an-expect-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!