How do I ssh into a machine, wait for a prompt, and then send a bunch of commands to it?
<p>How do I do the following?</p>

<ol>
<li>SSH to a <code>machine</code>, probably using expect since this is a script and I can't type the password for it</li>
<li>Wait for a prompt from the machine.  The prompt is <code>-&gt;</code></li>
<li>Then send a bunch of commands from the original host to the machine I ssh'd to.  <em>Note that the output from these commands can contain the characters <code>-&gt;</code></em></li>
<li>Exit</li>
</ol>

<p>Here are the contents of <code>some-commands.txt</code>:</p>

<pre><code>first command
second command
third command
</code></pre>

<p>Here are the contents of the expect script:</p>

<pre><code>#!/usr/bin/expect

set f [open "some-commands.txt"]
set cmds [split [read $f] "\n"]
close $f

eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root@machine

interact -o -nobuffer -re "Password:" return
send "password\r"
# look for the prompt
set prompt "\n-&gt; "

foreach cmd $cmds {
    send "$cmd\r";
    # The following works, except for the commands
    # whose output include -&gt;
    interact -o -nobuffer -re "-&gt; " return
}
</code></pre>

<p>The problem is that the interact command captures the <code>-&gt;</code> from the command output instead of the prompt, which hasn't yet arrived at that point.</p>