Link to home
Start Free TrialLog in
Avatar of afpcos
afpcos

asked on

passwd and expect

jlevie...

Thanks for all your help....

I have a couple of more questions until my expect book gets in...

I am using the same passwd.cgi that comes with expect.  I am running on a Solaris 5.7 machine.

I have to change the following line:

spawn /bin/su $var(name) -c "/bin/yppasswd $var(name)"

to

spawn /bin/su $var(name) -c "/bin/passwd $var(name)"

to get the script to start working.

However, I am getting the following response in my browser.

Passwd Change Acknowledgment
Error: Password:

Do you have any ideas...??

Thanks again...
Avatar of jlevie
jlevie

I'm looking at it...
I found it. Which version of expect do you have? I'm looking at 5.31 and I can post the change if we're both using the same version.
Avatar of afpcos

ASKER

Yes.. I am using 5.31...
Avatar of afpcos

ASKER

Yes.. I am using 5.31...
Okay, I'll put the change up in a bit.
Avatar of afpcos

ASKER

jlevie...

Good morning....

Were you going to post the change here or on the expect web site.

Thanks...!!!
It'll be here, I ran out of time last night and didn't get to test it as a cgi... very soon, I promise...
Avatar of afpcos

ASKER

Thanks...

I just wanted to make sure I was looking in the right place...

See ya
Avatar of afpcos

ASKER

Thanks...

I just wanted to make sure I was looking in the right place...

See ya
Okay, here tis. Replace the expect code in passwd.cgi with what follows.

---Snip, Snip..---
# Change following line appropriately for your site.
# Solaris 2.6 & later needs the -r option to specify which
# password service (files, nis, nisplus) see man passwd.
spawn /bin/su $var(name) -c "/bin/passwd -r files $var(name)"

expect {
    "Unknown login:" {
      errormsg "unknown user: $var(name)"
      exit
    } default {
      errormsg "$expect_out(buffer)"
      exit
    } "Password:"
}
send "$var(old)\r"
expect {
    "unknown user" {
      errormsg "unknown user: $var(name)"
      exit
    } "Sorry" {
      errormsg "Old password incorrect"
      exit
    } default {
      errormsg "$expect_out(buffer)"
      exit
    } -re "(New P|Enter login p)assword:"
}
send "$var(old)\r"
expect {
    default {
      errormsg "$expect_out(buffer)"
    } "New password:"
}
send "$var(new1)\r"
expect -re "(N|Re-enter n)ew password:"
send "$var(new2)\r"
expect -re (.*)\r\n {
      set error $expect_out(1,string)
}
close
wait
Avatar of afpcos

ASKER

jlevie...

I made the changes you suggested.

The script seems to be executing all the way through the code now.

However, the password is not being changes.

Unless you have any other ideas, I guess I will have to wait for the book.
Are you using files, NIS, or NIS+?
(keep me informed;-)
Avatar of afpcos

ASKER

I am using files.

I think I have narrowed it down.

After this line:

expect -re (.*)\r\n {
set error $expect_out(1,string)
}

I had to type:

wait
expect eof

For it to work.

However, the code you suggested cut out the last display that tells the user if there was an error or if it was successfull.  I need this for the users.

When I add that back in between the wait and the expect eof, it still changes the password, but it does not display the message.

I am still working on this.

If you have a solution in the next day or so I will increase your points and accept the answer.  If not,  in a couple of days...I will accept the answer as is because it was a very good lead to the answer.

Again...Thank you!!!

I am new to Unix and expect...you were a great help.
Do you have these lines:

if {[info exists error]} {
        errormsg "$error"
} else {
        successmsg "Password changed successfully."
}

after the code I sent? There were in the original and are responsible for the displaying the last error or the success message.
Avatar of afpcos

ASKER

Yes...

It looks like the problem is that even when the password is changed, it is going through the first part of the last if and displaying

Error:

I tried displaying the error variable prior to going into the if statement and it is null.

It should be going through the else statement and displaying "Password changed successfully.".
Avatar of afpcos

ASKER

Yes...

It looks like the problem is that even when the password is changed, it is going through the first part of the last if and displaying

Error:

I tried displaying the error variable prior to going into the if statement and it is null.

It should be going through the else statement and displaying "Password changed successfully.".
Avatar of afpcos

ASKER

Yes...

It looks like the problem is that even when the password is changed, it is going through the first part of the last if and displaying

Error:

I tried displaying the error variable prior to going into the if statement and it is null.

It should be going through the else statement and displaying "Password changed successfully.".
Avatar of afpcos

ASKER

jlevie...

Just received my book.

This is what I did to make it work for me.

wait -nowait

if [llength $error]>0 {
        errormsg "$error"
} else {
        successmsg "Password changed successfully."
}

expect eof

I changed the wait to wait -nowait because
where the user did not type the second new password correctly it took forever for the error message to come back.

I also read about if statements and changed it to [llength $error]>0.

I am new at this, so unless you see anything here with these changes that I could improve on or will have problems with, I will accept your answer.

Thanks...
I don't think the "wait -nowait" is the solution. That, "causes the wait to return immediately with the indication of a successful wait".

Let me look at something else, I just found what might be a slight variation in the behaviour of "passwd" (when presented with an invalid second password) depending on what patches are installed. I think I need a more general closure section.   Shortly...
Is this what you mean...:

#!/usr/local/bin/expect --

spawn /usr/bin/passwd $1
expect "New password:"
send "$2\r"
expect "Re-enter password:"
send "$2\r"
I've made some progess. Interestingly, I've found lots of ways to get it to fail amoung my 2.6, 2.7, & 2.8 systems. I think I've pretty well sorted out and accounted for the differences, but I'd like to do one last round of tests before I post the results. Say tomorrow as it's late and the regression test takes a while to work through all the ways I know it can fail.
try a
  sleep 1
as last command in your expect script
Avatar of afpcos

ASKER

jlevie....

I will await your post.

Again thank you for your help.

My experience is in Visual Basic, Access, and Informix database.

Maybe some dat I can return the favor.

Have a nice weekend.  

I will check back on Monday...
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of afpcos

ASKER

Adjusted points from 50 to 100
Avatar of afpcos

ASKER

jlevie...

Sorry it took me so long to accept your answer.

I was tied up in another project.

Thanks again!!!

afpcos....