title: Using conditional statements inside 'expect'
body: <p>I need to automate logging into a TELNET session using <strong>expect</strong>, but I need to take care of multiple passwords for the same username.</p>
  
  <p>Here's the flow I need to create:</p>
  
  <ol>
  <li>Open TELNET session to an IP</li>
  <li>Send user-name</li>
  <li>Send password</li>
  <li>Wrong password? Send the same user-name again, then a different password</li>
  <li>Should have successfully logged-in at this point...</li>
  </ol>
  
  <p>For what it's worth, here's what I've got so far:</p>
  
  <pre><code>#!/usr/bin/expect
  spawn telnet 192.168.40.100
  expect "login:"
  send "spongebob\r"
  expect "password:"
  send "squarepants\r"
  expect "login incorrect" {
    expect "login:"
    send "spongebob\r"
    expect "password:"
    send "rhombuspants\r"
  }
  expect "prompt\&gt;" {
    send_user "success!\r"
  }
  send "blah...blah...blah\r"
  </code></pre>
  
  <p>Needless to say this doesn't work, and nor does it look very pretty. From my adventures with Google <strong>expect</strong> seems to be something of a dark-art. Thanks in advance to anyone for assistance in the matter!</p>