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.
eof kills my script

Posted by selfism (selfism), 14 February 2005
Howdy & Thank you in advance!!

My script logs into remote systems and executes commands. I am trying to make my script fault tolerant by explicitly "expecting eof".  Even though I set my eof action to "continue"  rather than exit, it still kills my script. I test this by launching my script, it begins executing commands on the remote system, then I kill the spawned ssh connection. My script never recovers.  Here is the suspect code snippet:

Login to remote system.....
Begin executing commands via a foreach loop.....
expect  {
  puts "timed out"
   continue    <---- this works !

   puts "Lost connection to remote system"
   continue <---- NO WORKY !! the eof just kills my script

-re "$prompt"
  send -- "Next cmd\r"

p.s. Once I get this to work it seems I could wrap both "timeout" & "eof" into a expect_after statement. Then I could wrap the expect_after and the overall "expect" statement into a sendexpect {}... does this sound resonable?

Posted by admin (Graham Ellis), 14 February 2005
What do you want the continue to do?

EOF means "END of file" and so nothing is going to come after it. Once you've got to the end - once you've finished reading all the information back and the child process has completed, you can't ask for more.

Posted by selfism (00oo), 14 February 2005
I guess I assumed it would continue my server foreach loop which I didn't show in my code previously.

foreach server....
spawn connection
foreach cmd....
eof ---> kills my script, I am trying to get my script to move to the next server. Normaly my script will execute the commands and will move to the next server but if I get an eof while on a remote machine it dies. My script successfully moves past machines that are unavailable too, it is only the end of frame that causes the issue. Is there away I can catch eof to keep it from causing my script to die?

Posted by admin (Graham Ellis), 14 February 2005
I think you can just take the continue out and let it drop into the following code can't you?  It's the saying that you're still expecting more from a dead spawn that's causing the problem.  If you want to take a different action once you've exited the expect depending on whether or not the expect finished for EOF or some other reason, set a variable in the eof condition clause and check it later with an if.

Hope this is making sense - I'm doing it from memory as I'm in Germany and my books are in England this week!

Posted by selfism (00oo), 14 February 2005
Ok, that makes perfect sense. I will try it out and post the results!

Thank you !!!

Posted by selfism (00oo), 18 February 2005
Ok, I finally figured it out. EOF ends the current expect statement not the entire script. However, if the script attempts to send to a non-existant spawn id it will terminate. That is exactly what my script was doing. I was killing the ssh session on the remote system which would return "eof" and cause the expect statement to return.... but I returned to my command loop, hence another command was sent to a now non-existant spawn id which would kill my script.

The soluiton: use break instead of continue, thus when my expect statement saw the eof it would break out of the current loop.

   puts "Lost connection to remote system"

Thank you for your help, sorry it took me so long to reply but that is how long it took me to figure it out.

This page is a thread posted to the opentalk forum at 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., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho