Question

This question already has an answer here:

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 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$ 

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?

Was it helpful?

Solution

I think your problem has been solved here before:

Using expect to pass a password to ssh

The command you're looking for is interact. It hands the control over to you/your keyboard.

OTHER TIPS

I've used a similar script to autologin.

I used "interact" and I removed "expect eof". By doing this, I can get the screen back so that I can enter commands by hand.

expect "?assword: "
send -- "$password\r"
expect "$"
interact

putting it all together, log you in and leave you on the command line exactly as though you typed it manually

#!/usr/bin/expect -f
set ip "127.001.001.001"
set password "xxyykkx"
spawn ssh $ip -l root
expect "?assword:"
send "$password\r"
interact
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top