问题
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