title: How do I use expect for repeat cases, without a long script?
body: <p>I am using expect to run a bash script which ssh's to 5 boxes, runs an aptitude dist-upgrade -d, zips up the packages, then scps them back to the original box.</p>
  
  <p>All the boxes have the same password and all the boxes require an input of "y" to say yes to the dist-upgrade.</p>
  
  <p>My current expect script is long and messy with loads of repeats of the following</p>
  
  <pre><code>## Original box
  expect "password"
  send "Password\n"
  ## Box sshing to
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  </code></pre>
  
  <p>My question is, how do I "dedupe" this so that where a password is asked for it sends the password, and where a continue prompt is asked for, it sends y.  So that I don't have to get a perfect order of what will come when (sometimes a key check is thrown into the mix where I have to send "yes")</p>
  
  <p>Full script below:</p>
  
  <pre><code>#!/usr/bin/expect -f
  set timeout -1
  ## This uses manualpatching.sh, used to zip up a list of the latest Debian packages
  spawn ./manualpatching.sh
  ## BOX1
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  ## BOX2
  expect "password"
  send "Password\n"
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  expect "password"
  send "Password\n"
  expect "connecting"
  send "yes\n"
  ## BOX3
  expect "password"
  send "Password\n"
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  expect "password"
  send "Password\n"
  ## BOX4
  expect "password"
  send "Password\n"
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  expect "password"
  send "Password\n"
  ## BOX5
  expect "password"
  send "Password\n"
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  expect "password"
  send "Password\n"
  ## BOX6
  expect "password"
  send "Password\n"
  expect "password"
  send "Password\n"
  expect "want to continue?"
  send "y\n"
  expect "password"
  send "Password\n"
  ## END
  expect eof
  </code></pre>