EXCEPT timeout problem

i am trying to write an except script to ssh into a list of devices and run some commands, and i came across this problem, not every device is alive, which breaks the script, my script looks like this

#!/usr/bin/expect                           
# set defaults                              
set device [lindex $argv 0]                 

set user "admin"
set password "test123"
set list [open $device r] 
set resultfile [open setlog.log w]
set timeout 10                    
trap exit SIGINT                  

log_file -a -noappend setlog_logfile.log

# ssh to each IP on the list

while {[gets $list line] != -1} {
set pid [spawn ssh -o "ConnectTimeout 3" admin@$line]  
expect "password:"               
send "$password\r"               
expect eof                       
expect "#"                       
send "shell\r"                   
****some commands here****
send "exit\r"                             
expect eof                                
puts $resultfile "$line, Worked!"         
log_user 1                                
wait                                      
}  

following is the error message i got, apparently expect runs across a dead ip and it times out and *continue* to run the next command in the script, how can i fix this? thanks

$ ./testrun /tmp/testip
spawn ssh -o ConnectTimeout 3 admin@192.168.1.2
ssh: connect to host 192.168.1.2 port 22: Connection timed out
send: spawn id exp9 not open
    while executing
"send "$password\r"               "
    ("while" body line 4)
    invoked from within

The mistype of the title will confuse!

I tend to write expect scripts using a loop, that way it can handle repeat questions and unexpected events rather than getting stuck when things don't occur in the correct order, the classic example is using expect to answer questions from fsck, can't remember where I found this example but it illustrates the point:

while 1 {
	expect {
		eof                    {break}
	        "UNREF FILE*CLEAR\\?"  {send "y\r"}
		"BAD INODE*FIX\\?"     {send "n\r"}
		"\\? "	               {interact +}
		}
	}

# The last question mark is a catch all.
#  \\ prevents the next character from being interpreted as a wild card.

In your case you could use the lines (plus others):

expect {
"password:"             {send "$password\r" }
Connection timed out    {break}
"#"                     {send "shell\r some more commands here"}
}

So it can then continue onto the next machine
I don't think this can handle the expect EOF followed by "#" that you are doing though...
Whether while statement can be nested in expect I'm not sure.

An idea that might be no good, but I thought I'd suggest it just in case?