PDA

View Full Version : Expect Script Help


pug306d
06-22-2004, 05:01 PM
I have this Expect script which is working but I need a small alteration.

#!/usr/bin/expect --

puts "Content-type: text/html\n" ;# note extra newline

puts "
<head>
<title>Password Change Result</title>
</head>

<h2>Password Change Result</h2>
"

proc cgi2ascii {buf} {
regsub -all {\+} $buf { } buf
regsub -all {([\\["$])} $buf {\\\1} buf
regsub -all -nocase "%0d%0a" $buf "\n" buf
regsub -all -nocase {%([a-f0-9][a-f0-9])} $buf {[format %c 0x\1]} buf
eval return \"$buf\"
}

foreach pair [split [read stdin $env(CONTENT_LENGTH)] &] {
regexp (.*)=(.*) $pair dummy varname val
set val [cgi2ascii $val]
set var($varname) $val
}

log_user 0

proc errormsg {s} {puts "<h3>Password Not Changed: $s</h3>"}
proc successmsg {s} {puts "<h3>$s</h3>"}

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

expect {
"/bin/su:" {
errormsg "check your username"
exit
} default {
errormsg "$expect_out(buffer)"
exit
} "Password:"
}
send "$var(old)\r"
expect {
"/bin/su:" {
errormsg "old password incorrect"
exit
} default {
errormsg "$expect_out(buffer)"
exit
} "password:"
}
send "$var(old)\r"
expect "password:"
send "$var(new1)\r"
expect "password:"
send "$var(new2)\r"
close
wait

if {[info exists error]} {
errormsg "$error"
} else {
successmsg "Password changed successfully</p>
Please login with your new details"
}


If the security requirements are not met on the new password the script still reports the password was changed. If the password is not changed passwd reports:

passwd: "the error"

I need the script to report if "passwd:" was the last message then error else give the success message.

Thanks
Dean.

YUPAPA
06-22-2004, 11:33 PM
This isn't perl tho... :p
Are you asking for us to conver it for you?

pug306d
06-23-2004, 12:48 AM
I thought this section was the best fit for the script as its CGI?

dswimboy
06-30-2004, 10:09 PM
this is the best place to put it. i've never heard of expect, but hopefully someone around here has!