SSH login with expect(1). How to exit expect and remain in SSH?

Posted by Koroviev on Stack Overflow See other posts from Stack Overflow or by Koroviev
Published on 2010-05-12T21:50:41Z Indexed on 2010/05/12 21:54 UTC
Read the original article Hit count: 469

Filed under:
|
|
|
|

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.

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.

#!/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

Initially, it runs as it should.

Last login: Wed May 12 21:07:52 on ttys002
esther:~ user$ expect expect-test.exp
spawn ssh -p 2222 [email protected]
[email protected]'s password: 
Last login: Wed May 12 15:44:43 2010 from 20.10.20.10


-jailshell-3.2$ 

But that's where the success ends.

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]

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:

-jailshell-3.2$ whoami
ls
pwd
hostname

(...time passes, expect times out...)

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

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?

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.

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?

© Stack Overflow or respective owner

Related posts about ssh

Related posts about bash