Need Help with expect when ssh fails

I'm trying to write a script using expect. I'd like the script to execute several commands when the ssh succeeds and i want it to exit if the ssh fails. Does this require to define a time out for the ssh command so that if the prompt is back before this defined time the next commands are executed??

Another question: Is there a way to direct the script to skip some lines when ssh fails and start execution from certain line?

Thanks in advance

In general you can have expect wait a certain amount of time before it goes on to try and run the next command.

I did something similar with an expect script that logs into our switches at work, and uploads the configs. For example, the script looks to see if you are asked to accept the host key, so the line for expect says:

# Look for unknown Host Keys question and reply with yes:
expect -timeout 10 "yes/no" {send "yes\r"}

After connecting the script will wait 10 seconds to see the yes/no...if it doesn't see it, it moves on.

Hope that helps

Thanks alot...but let me tell you more details about the peoblem i'm facing.
I'm using the below code

spawn ssh mxadmin@$ip
expect {
"yes/no" {send "yes\r"}
Connection timed out {break}
eof {exit }
}

--some more commands--
My problem is that when ssh fails: the commands continue execution without so the break command did not prevent the expect file from being completed.

Thanks in advance