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))
Waiting for the result when running expect script

Posted by theaccelerator (theaccelerator), 24 December 2007
I'm running expect script from a C++ program using the popen function.
My expect script is using the command:
spawn telnet $ipaddress

Then it performs login and runs several commands.

My problem is that I don't get the results of the script. It seems like I'm getting the control right away and continue with my program before the script has performed the telnet, thus I get and empty result when running the script from my program.

How can I wait for the script to end, and receive the results to see if there was a success or some problem has occured.


Posted by admin (Graham Ellis), 24 December 2007
Without seeing your program it's hard to know, but one very common problem is that people issues a command through send then fail to wait for the next prompt via expect.

The easiest way to describe this is to quote an example - I have seen people all too often connect via FTP, send a get command, and then exit their script.   This results in only a partial file being received as the FTP connection is dropped as soon as the expect program exits, and the solution is to expect "ftp>".

Posted by theaccelerator (theaccelerator), 25 December 2007
My mistake, it appears like I got the whole interaction as the result, and I thought that the interaction was after I returned.
The problem now is how to get only the messages which where send by send_user and not the whole output.

Here is a short and simple script, where I would expect to recieve to my C program only the OK.

spawn telnet [lindex $argv 0]
expect login:
send "admin\r"
expect Password:
send "\r"
expect >
send "enable\r"
expect #
send_user "OK"
exit

This is important since, my script is much more complex and I only want to get as a result if there was some error, or if there was a success.

Posted by admin (Graham Ellis), 25 December 2007
Add a

log_user 0

at the top



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