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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Problem with capturing output from mkpasswd

Posted by magna777 (magna777), 25 September 2004
I'm new to the expect language.  I have an expect script that that  takes in two command line arguments - host and account name.   The purpose of the script is to ssh to a certain host and create a new account on that host.   The password for this new account is automatically generated within this script.  The problem I'm having is capturing the output from the expect command "mkpasswd" which is called in my script and using the generated password.   Anyone's input would be greatly appreciated.  Here is my code:

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

if { $argc != 2 } {
  send_user "Usage: $argv0  host  emailAccountNameToCreate\n"
  exit 1
}

set timeout 5

set env(TERM) vt100;# actual value doesn't matter, just has to be set

set host [ lindex $argv 0 ]

set account [ lindex $argv 1 ]

spawn mkpasswd -l 9 -C 3 -c 3

expect eof

set password $expect_out( buffer )

spawn ssh root@$host

set value 1

while { $value } {
  global value

  expect "password:" { send "jus4Us\n"; set value 0 } \
             "(yes/no)?" { send "yes\n" } \
             timeout     { send_user "Timed out trying to connect to $host\n"; exit 1 }
}

expect "root]#" { send "adduser $account\n" } \
           timeout  { send_user "Timed out waiting for the system prompt during the step: adduser $account\n"; exit 1 }

expect "$account exists" { send_user "That account name already exists on $host\n"; exit 1 } \
           "root]#"          { send "passwd $account\n" } \
           timeout           { send_user "Timed out waiting for the system prompt during the step: passwd $account\n"; exit 1 }

expect "password:" { send "$password\n" } \
           timeout     { send_user "Timed out waiting for the system to prompt for a new password\n"; exit 1 }

expect "password:" { send "$password\n" } \
           timeout     { send_user "Timed out waiting for the system to ask to retype new password\n"; exit 1 }

send_user "mkpasswd generated: $password\n"
exit 0





When I try running this script I get the following error:

can't read "expect_out( buffer )": no such element in array
   while executing


Posted by admin (Graham Ellis), 25 September 2004
I think your problem is the spaces around the word "buffer". I cut and pasted your script, reduced it to this and reproduced the failure:

Code:
#!/usr/bin/expect -f

spawn mkpasswd -l 9 -C 3 -c 3
expect eof
set password $expect_out( buffer )
puts $password


I then removed the spaces so it read like this

Code:
#!/usr/bin/expect -f

spawn mkpasswd -l 9 -C 3 -c 3
expect eof
set password $expect_out(buffer)
puts $password


and the code worked

Posted by magna777 (magna777), 25 September 2004
Thanks so much Graham!  I cannot beleve it was that simple.  But I am beginning to notice that expect is very particular!  Thanks again bud!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho