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
How to continue a loop

Posted by drheams (drheams), 30 December 2005
Hello,
I am new to expect but am trying to write a script to run some tests on all of the routers in my network. I am attempting to run a series of tests in a specific order so if any single test fails, log the problem, close the connection, and continue on to the next router. As long as all of the tests pass I run through the entire list of routers with no problem but as soon as a test fails the problem is logged correctly but the script terminates immediately.
Can anyone point me in the right direction to make this work? The relevant portion of my script follows. Thanks for any help you can give me.



set f [open list r]
set data [read $f]
close $f

proc fail {host reason} {
  send_log "SEARCH Test Failed on $host - $reason\n"
  "send exit\r"        
}

foreach host [split $data \n] {
log_file $host
spawn /usr/bin/telnet $host
expect {
timeout {
       fail $host Unreachable
       }
refused {
       fail $host Refused
       }
"sername:" {
       send -s [string trimright "$user" "\r"]
       }
}
expect
.
.
.
.

Posted by admin (Graham Ellis), 31 December 2005
Hello ... I think

Code:
"send exit\r"


should read

Code:
send "exit\r"


As it was, you were trying to run a command called "send exit" which doesn't exist .... rather than sending an "exit" to telnet.  Expect will cease execution (just as you mentioned) if it comes to a non-existant command ((I would have expected an error message too, but never mind!))

Posted by drheams (drheams), 3 January 2006
Hello Graham,
Thank you for the reply.
I did find my mistake with the quotes rather quickly because of an error message but fixing that did not help. It seems that when I send the exit command the telnet connection is obviously closed and the corresponding spawn_id no longer exists but the script attempts to continue on with its remaining tests.
I am now receiving the following error when the script runs and sends the exit correctly...

can not find channel named "exp4"
   while executing
"send -s "isdn test call interface bri0/0 <number>\r""
   ("foreach" body line 42)

(<number> is actually the phone number being dialed)

The isdn test command is the test to run assuming the previous test was successful. In this case the previous test was not successful so I want to end the telnet session and loop to the next router in the list. It appears that the ending of the telnet session is the problem but I cannot figure out a way of cleanly ending it and continuing the loop.

Thanks again for your help.


Posted by admin (Graham Ellis), 3 January 2006
I think the clue is in the end of the sections of the script you posted; that final expect .... is running even if the telnet session has already failed.

If you  set aok 1 if you get a good login but set aok 0 in the event of a failure to login, then run that extra expect only if {$aok == 1} ... I think it'll sort you out

Posted by drheams (drheams), 3 January 2006
This is perfect, that is exactly what I was looking for. In fact it opened up a lot more avenues for me to expand this tool. Thank you very much for the help



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