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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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., 2021: 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