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))
Processing output after Spawning in Expect

Posted by dibya.jyoti (dibya.jyoti), 6 April 2007
I am new to this forum.
I want to write Expect script to check the ping after doing ssh to a router.I am able to log the output of ping in local machine where I am running the script
After closing the ssh "using close -i $spawn_id" I want to comeback to the local machine same directory where I want to parse the output from the same script.

Here is my program

#!/usr/bin/expect
# Set the DISPLAY environment variable (as a test)
set env(DISPLAY) chilli:0
spawn ssh root@192.168.1.248

set telnet $spawn_id

expect -nocase "root@"
send  "123456\r"
expect -nocase "#"

send "ping -c 10 10.168.180.51 \r"
log_file outfile
expect eof
expect -nocase "#"
close -i $spawn_id
cat outfile  ===================>> Not running.

exit

I have tried exec cat outfile. Still didn't work. I want to parse outfile to do regexp that is my goal.


Please help me to do that.


Posted by admin (Graham Ellis), 6 April 2007
Your problem is one of
a) buffering
and
b) Interactive v program shell.

In the buffering, your outfile hasn't actually reached the disc - it's stuck in memory and hasn't been flushed when you try to cat.  There's usually little point in writing files directly to disc, byte by byte as they're generated and so for efficiency the writes are blocked up and done all at once.  Just occasionally, and your example may be a case in point, it can cause a problem.

And if you're running a Tcl shell (such as tclsh, wish, expect or expectk) from an interactive prompt and you give a command name it doesn't recognise, it falls back to
1. Shortened command names (e.g. if you type in regex not regexp, it WILL resolve)
2. Shell commands if nothing matches.
These two facets of the "unknown" command trap do not happen in stored programs which measn that you can develop interactively, have something work, then cut and paste to a file and have it fail.

That's the analysis.  The SOLUTION for you is totally different to analysing the above - have a look at expect_out(buffer) which will contain all the text generated while the most recent expect command was scanning for its completion. No need for you to worry about buffers and unknown - Don Libes did what you need and gave it you as a gift when he wrote Expect



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