Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
newbie needs 'expect' help

Posted by gw1500se (gw1500se), 14 December 2004
I have written what I thought was a simple 'expect' script that su's to a userid and changes the password from arguments. The script works flawlessly from the command line. My problem is executing it from a php page. I suspect it has something to do with running without a terminal but I don't know how to fix it.

The error I am getting is that after the 'su' runs and I get the expected prompt string, I run the 'passwd' command. I think I am getting the next expected prompt string but then 'expect' fails telling me "spawn id exp12 is not open."

Here is the relevent script segment:

if [ $# -ne 3 ] ; then
     echo "Usage: emailchpw <userid> <oldpw> <newpw>"
     data=`/usr/bin/expect 2>/dev/null  <<EOF
           set timeout 2
           spawn /usr/bin/su -m $userid
           expect {
                 "Password:"      {send "$oldpw\r"}
                 timeout      {puts "^Error: Unexpected results, notify IT (su)" ; exit};
           expect {
                 $userid      {send "/usr/bin/passwd $userid\r"}
                 "su: Sorry"      {puts "^Error: Invalid password or email address" ; exit}
           expect {
                 "admin:"      {send "blahblah\r"}
                        timeout      {puts "^Error: Unexpected results, notify IT (pwd fail)" ; exit}

The error occurs while executing the last send statement. Can someone point me in the right direction on this?


Posted by admin (Graham Ellis), 14 December 2004
The passwd command is intentionally written to make it hard to automate password changes - that's why it's often done through expect rather than any other scripting system, as it reads from /dev/tty and not from stdin.

I suspect your problem is caused by PHP not "passing through" /dev/tty (and it would be illogical for it to do so!).

How to fix?  Don't know - it might be tricky. But PHP has encryption built in (see the crypt function), so you might be able to do the password encoding and change the password other than through the passwd program.  Be careful not to leave any security holes ... if you provide a way of allowing users to change their passwords from a web page, there's going to be some joker who tries to break the system.

Posted by gw1500se (gw1500se), 15 December 2004
Thanks for the reply.

There has to be a way to accomplish this, it is just a matter of figuring out how. Your suggestion of a possible problem at least gives me some place to look. Thanks.

As to security, this is a LAN only web page. It is not accessable to the outside world but I do make sure it is not vulnerable to accidental error. If an employee is even capable of hacking it, they would be summarily terminiated if they did so.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho