title: Expect telnet automation fails without interact
body: <p>The following Expect script works, but the <code>interact</code> at the end forces the user into an interactive session.</p>
  
  <pre><code>#!/usr/bin/expect -f
  
  set timeout 10
  spawn telnet sometelnetserver.com 10000
  
  foreach line [split [read [open somefile.txt r]] "\n"] {
      send "$line\n"
  }
  
  interact
  </code></pre>
  
  <p>However, removing the <code>interact</code> stops telnet from connecting.  The script outputs</p>
  
  <pre><code>spawn telnet sometelnetserver.com 10000
  </code></pre>
  
  <p>and the remote never gets a connection.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Hackily solved by sending a proper quit command before interact so there's nothing to interact with.</p>
  
  <pre><code>#!/usr/bin/expect -f
  
  spawn -noecho telnet sometelnetserver 10000
  
  foreach line [split [read [open somefile.txt r]] "\n"] {
      send -- "ed77e768-0a1f-4fda-98c0-c3e5af441f78 $line\r"
  }
  
  send \035
  send quit\r
  interact
  </code></pre>
  
  <p>This seems more than a little sketchy though, because it will still hang if the server doesn't respond.</p>