title: SSH login with expect(1). How to exit expect and remain in SSH?
body: <p>So I wanted to automate my SSH logins. The host I'm with doesn't allow key authentication on this server, so I had to be more inventive.</p>
  
  <p>I don't know much about shell scripting, but some research showed me the command 'expect' and some scripts using it for exactly this purpose. I set up a script and ran it, it worked perfectly to login.</p>
  
  <pre><code>#!/usr/bin/env expect -f
  
  set password "my_password"
  
  match_max 1000
  
  spawn ssh -p 2222 "my_username"@11.22.11.22
  
  expect "*?assword:*"
  send -- "$password\r"
  send -- "\r"
  
  expect eof
  </code></pre>
  
  <p>Initially, it runs as it should.</p>
  
  <pre><code>Last login: Wed May 12 21:07:52 on ttys002
  esther:~ user$ expect expect-test.exp
  spawn ssh -p 2222 my_username@11.22.11.22
  my_username@11.22.11.22's password: 
  Last login: Wed May 12 15:44:43 2010 from 20.10.20.10
  
  
  -jailshell-3.2$ 
  </code></pre>
  
  <p>But that's where the success ends.</p>
  
  <p>Commands do not work, but hitting enter just makes a new line.
  Arrow keys and other non-alphanumeric keys produce symbols like '^[[C', '^[[A', '^[OQ' etc.[1]</p>
  
  <p>No other prompt appears except the two initially created by the expect script.
  Any ignored commands will be executed by my local shell once expect times out.
  An example:</p>
  
  <pre><code>-jailshell-3.2$ whoami
  ls
  pwd
  hostname
  </code></pre>
  
  <p>(...time passes, expect times out...)</p>
  
  <pre><code>esther:~ user$ whoami
  user
  
  esther:~ ciaran$ ls
  Books       Documents   Movies      Public      
  Code        Downloads   Music       Sites       
  Desktop     Library     Pictures    expect-test.exp
  
  esther:~ ciaran$ pwd
  /Users/ciaran
  
  esther:~ ciaran$ hostname
  esther.local
  </code></pre>
  
  <p>As I said, I have no shell scripting experience, but I think it's being caused because I'm still "inside of" expect, but not "inside of" SSH. Is there any way to terminate expect once I've logged in, and have it hand over the SSH session to me?</p>
  
  <p>I've tried commands like 'close' and 'exit', after " send -- "\r" ". Yeah, they do what I want and expect dies, but it vindictively takes the SSH session down with it, leaving me back where I started. What I really need is for expect to do its job and terminate, leaving the SSH session back in my hands as if I did it manually.</p>
  
  <p>All help is appreciated, thanks.
  [1] I know there's a name for this, but I don't know what it is. And this is one of those frightening things which can't be googled, because the punctuation characters are ignored. As a side question, what's the story here?</p>