title: Using expect to SSH to server and execute commands
body: <p>I want to write a generic expect script to login through SSH to a system and execute some commands.  An example I found had the following:</p>
  
  <pre><code>#!/usr/bin/expect
  
  set fid [open ./.secret]
  set password [read $fid]
  close $fid
  spawn /usr/bin/ssh root@[lindex $argv 0]
  expect {
    -re ".*Are.*.*yes.*no.*" {
      send "yes\n"
      exp_continue
      #look for the password prompt
    }
  
    "*?assword:*" {
      send $password
      send "\n"
    }
  }
  
  send -- "PS1='&gt;'\r"
  expect -re "&gt;$" { send "hostname\r" }
  expect -re "&gt;$" { send "pwd\r" }
  </code></pre>
  
  <p>...the script seems to login properly but it didn't execute the last 2 sends.  Ideas?</p>
  
  <p>Edit:
  After enabling exp_internal, I noticed the following:</p>
  
  <pre><code>expect: does "" (spawn_id exp4) match glob pattern "*"? yes
  expect: set expect_out(0,string) ""
  expect: set expect_out(spawn_id) "exp4"
  expect: set expect_out(buffer) ""
  send: sending "PS1='&gt;'\r" to { exp4 }
  Gate keeper glob pattern for '&gt;$' is '&gt;'. Activating booster.
  
  expect: does "" (spawn_id exp4) match regular expression "&gt;$"? Gate "&gt;"? gate=no
  
  
  expect: does "\r\n" (spawn_id exp4) match regular expression "&gt;$"? Gate "&gt;"? gate=no
  Last login: Tue Nov  6 14:13:31 2012 from 1.x.x.x
  
  expect: does "\r\nLast login: Tue Nov  6 14:13:31 2012 from 1.x.x.x\r\r\n" (spawn_id exp4) match regular expression "&gt;$"? Gate "&gt;"? gate=no
  </code></pre>
  
  <p>I'm trying to send <code>PS1='&gt;'\r</code> because I want to override the prompt.  I don't think there's any way for me to predict what the prompt will be and therefore, I wouldn't know what pattern to expect.  From the above, it looks like the prompt wasn't changed.  How do you tackle a problem like this?</p>