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
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

accessing return value in expect

Posted by quiknot (quiknot), 28 July 2003
Hi, I have an expect script that rlogin's to another system, and executes a few remote commands on that server.  I need to capture the return value of one of the commands given, and then return that value to another script (the one that called the expect script).  Here is a rough outline of my expect script:

#!/usr/bin/expect -f

set prompt "(%|#|\\$|%]) $"
catch {set prompt $env(EXPECT_PROMPT)}
set timeout 10
set username [lindex $argv 0]
set pword [lindex $argv 1]
set arg1 [lindex $argv 2]
set arg2 [lindex $argv 3]
set host "theserver@thehost.com"
spawn rlogin -l $username $host    
expect "assword:*"
send "$pword\r"         //Connected to remote host
expect -re $prompt
send "/path/to/the/script $arg1 $arg2 ..  need return value of this script
expect -re $prompt
send "exit\r"
//need to return the return value found
exit

basically it is like this...I call the expect script from php (via exec), it runs on the same server as the webserver, then rlogin's to the other server and runs the scripts...I need to return the return value back to php, can I do this?
Thanks!


Posted by admin (Graham Ellis), 28 July 2003
Where you say "the return value", do you mean the result that it sends to the screen when you run it manually, or the operating system status value on the remote machine?

If you mean the value that it prints out to the screen, you'll find that in the expect array expect_out(buffer) ... after each expect call, this variable contains the text that your application has skipped over while waiting for the correct response.

If you're looking for the operating system status variable from the remote computer, I think you best bet would be to execute an extra send - expect cyscle to send an echo command to report on the variable's value, and capture that from expect_out(buffer).

Posted by quiknot (quiknot), 29 July 2003
Thanks for the reply Graham.  I don't think what you mentioned will work though, since the return value is not output to the screen.
However, if an error occurs (what I am testing for), the output of the script would be "Incorrect format or data: NOT checking-in ".
Is there a way to test if that output occurs, and if it does, set a variable to 1, and then return that value back to the parent process? (exit $return_variable)


Posted by admin (Graham Ellis), 29 July 2003
Quote:
 I don't think what you mentioned will work though, since the return value is not output to the screen.


Have a look at the third paragraph of my answer - I think it's answered there - force it to the screen  

Posted by quiknot (quiknot), 29 July 2003
Ok Graham, I guess I missed that.  Do you think you could show me in my code how I could work that in there?  I really appreciate all your help!

Posted by admin (Graham Ellis), 29 July 2003
err ... I'm away from my normal Tcl / Expect resource this week so I'm answering from memory ...   I think you need to add

Code:
send "echo \$status\r"
expect -re $prompt
set igot $expect_out(buffer)
# Then do whatever you need on the value in igot


The "send" may vary depending on which shell you're running on the remote system - check it out manually first.

Posted by quiknot (quiknot), 30 July 2003
Excellent...worked great Graham.  Thanks for your help



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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho