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))
Expect - how to spawn a file read operation

Posted by neilvp (neilvp), 1 September 2006
Oh Great and Glorious Guru Graham your assistance please...

My challenge this week is to run a TCL script that waits for a router to boot, then tells it to multiple ping tests, collects the results, and then reboots router, repeat ad-infinitum.

This will be done over a serial port.

I have created a text file from the terminal interaction (i.e. having done it manually) and intend to use this as the source for testing the script before running live. i.e. read a line from this text file and use that as if it had come from the serial port.

The question is, how do I spawn this 'read a line from the file' so that Expect can use it?
..or should I try something different

I am running the current ActiveState releases under Windoze XP.

Your hints, tips, help and assistance, is, as ever, very much appreciated.

Neil

Posted by admin (Graham Ellis), 1 September 2006
On unix / linux,I would spawn a cat command.  Using the same logic on XP, I guess you would spawn a type command; not sure if that logic's going to work, but at least it's somewhere straightforward to start trying it out and, after all, you are only looking for a solution for testing purposes  

Posted by neilvp (neilvp), 1 September 2006
Thanks Graham.

This did not work but having caught the error I am puzzled as to the real reason...

Unfortunately I cannot figure out how to attach a screen shot to this mail ...

As you can see from the code, prior to my trying to spawn I check that the file exists, why do I do this (apart from good programming of course) is because the reason that is reported for the spawn failing is  'The system cannot find the file specified'

However, the file does exist in the same pwd and the 'file exists ' check supports this.

Any ideas as to why the 'spawn type' cannot find the file?

Thanks

Neil


package require Expect

set FileToRead test_file_1.txt

puts "File To Read is $FileToRead\r"


if { [file exists $FileToRead] } \
{
   puts "$FileToRead exists\r"
} else \
{
   puts "$FileToRead does not exist\r"
}    

if [catch {spawn type $FileToRead}  reason]\
{
   puts "Failed to spawn\r"
   puts "Reason $reason\r"
} else {
   puts "spawned"    
}    
   
  puts "Ending Program"



Posted by neilvp (neilvp), 1 September 2006
Doh!

It is of course the 'TYPE.EXE' it cannot find. To make matters worse type.exe (or type.com) no longer exists as a standalone program within XP, it is, I am told, built into the DOS shell which is why that test worked and I consequently thought the spawn could not find the target file.

Any other suggestions?
(Running cygwin is not an option - sorry)

Neil



Posted by neilvp (neilvp), 1 September 2006
Courtesy of those very nice people at sourceforge I found a cat utility for Windoze.

http://unxutils.sourceforge.net/

That certainly did what I thought I wanted but it has given me other problems to solve now... like how can I get it to do it one line at a time (or get the expect to loop)?

Can I do something like

while {stdout <> eof} {
expect {
              test1  { action1}
              test2  {action2}
            }
}

And I thought it such a straight forward task

Thanks

Neil

Posted by admin (Graham Ellis), 1 September 2006
You can do

while 1 {
     expect ...

      }

and have an eof option on your 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