bash 使用 expect(1) 进行 SSH 登录。如何退出期望并保持在 SSH 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2823007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SSH login with expect(1). How to exit expect and remain in SSH?
提问by Koroviev
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.
所以我想自动化我的 SSH 登录。我所在的主机不允许在此服务器上进行密钥身份验证,因此我必须更具创造性。
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.
我不太了解 shell 脚本,但一些研究向我展示了命令“expect”和一些脚本正是为了这个目的使用它的。我设置了一个脚本并运行它,它完美地登录。
#!/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]
命令不起作用,但按 Enter 只会换行。箭头键和其他非字母数字键会产生像 '^[[C', '^[[A', '^[OQ' 等符号 [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:
除了最初由expect 脚本创建的两个提示外,没有其他提示出现。一旦预期超时,任何被忽略的命令都将由我的本地 shell 执行。一个例子:
-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?
正如我所说,我没有 shell 脚本经验,但我认为这是因为我仍然在“内部”期望,但不是“内部”SSH。一旦我登录,有什么方法可以终止expect,并将SSH会话交给我吗?
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.
我试过像“关闭”和“退出”这样的命令,在“send --”\r“”之后。是的,他们做我想做的事并期望死亡,但它报复性地取消了 SSH 会话,让我回到了我开始的地方。我真正需要的是期待完成它的工作并终止,将 SSH 会话留在我的手中,就像我手动完成一样。
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?
感谢所有帮助,谢谢。[1] 我知道它有一个名字,但我不知道它是什么。这是无法用谷歌搜索的可怕事情之一,因为标点符号被忽略了。作为一个附带问题,这里的故事是什么?
回答by jkramer
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.
您正在寻找的命令是interact
. 它将控制权交给您/您的键盘。
回答by user2852921
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 eof”。通过这样做,我可以恢复屏幕,以便我可以手动输入命令。
expect "?assword: "
send -- "$password\r"
expect "$"
interact
回答by zzapper
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