Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 339
Koroviev Avatar asked May 12 '10 21:05

Koroviev


People also ask

What is Exp_continue in Expect?

The command exp_continue allows expect itself to continue executing rather than returning as it normally would. By default exp_continue resets the timeout timer. The -continue_timer flag prevents timer from being restarted. (See expect for more information.)

How do I run a command in Expect script?

The first line defines the expect command path which is #!/usr/bin/expect . On the second line of code, we disable the timeout. Then start our script using spawn command. We can use spawn to run any program we want or any other interactive script.


2 Answers

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.

like image 124
jkramer Avatar answered Oct 09 '22 16:10

jkramer


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
like image 24
user2852921 Avatar answered Oct 09 '22 18:10

user2852921