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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
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  {
timeout
{
  puts "timed out"
   continue    <---- this works !
}

eof
{
   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
login...
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.

eof
{
   puts "Lost connection to remote system"
   break
}


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 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho