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))
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., 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