title: Error handling in expect scripts
body: <p>I am reproducing a minimal working example of my script. It works fine most of the times, up to many days, until it encounters a host that for some reason refuses the connection. In such a case, the script stops. I want the script to ignore any such event and continue to the next host in the list. Any ideas how to handle such errors in an expect script? </p>
  
  <pre><code>#!/usr/bin/expect -f 
  
  set timeout 5
  
  # The script runs forever. 
  while {1} {
  
  # There is a long list of IPs, keeping only two for simplicity. 
  
          set servers [list 172.22.29.254 172.22.2.125 ]
          foreach server $servers {
  
  # Creates a file depending on time and IP.
  
          set fh [exec date +%Y-%m-%d--%H:%M:%S_$server]
  
  # Telnets a remote host
  
          spawn telnet $server
  
          expect Password:
          exp_send "password\r"
  
  # Copies whatever is there. 
  
          expect ".*"
          expect -re ".*\r"
  
  # Opens the above created file and writes there. 
  
          set file [open $fh w]
          puts $file  $expect_out(buffer)\n
          close $file
  
  # Connection is now closed. 
  
          exp_close
          exp_wait
          }
  }
  </code></pre>