[英]How do I write a script to ssh to a computer from a remote computer?
I frequently need to ssh into a server, but I can't ssh into it directly while I'm connected to our VPN. 我经常需要ssh到服务器,但是当我连接到VPN时,我无法直接进入服务器。 Thus, I have to ssh into another server and ssh into it from there.
因此,我必须ssh到另一台服务器并从那里ssh到它。 Is there any way that I can write a script and/or shell function for this?
有什么方法可以为此编写脚本和/或shell函数吗? I've tried this, buit it gave me an error:
我试过这个,因为它给了我一个错误:
% ssh jason@server2 'ssh jason@server1'
jason@server2's password:
Pseudo-terminal will not be allocated because stdin is not a terminal.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
(and of course the username and server name have been changed) (当然用户名和服务器名称已更改)
It can't show you password prompt. 它无法显示密码提示。 I think all will work with public keys (and, for example, forwarded ssh-agent to eliminate need of entering key passphrases).
我认为所有都可以使用公钥(例如,转发ssh-agent以消除输入密钥密码的需要)。
SSH connection stacking may help you. SSH连接堆叠可能对您有所帮助。 Assuming the following layout: Client -> Middleman -> Destination
假设以下布局:客户端 - >中间人 - >目的地
On Client: 在客户端:
ssh user@Middleman -L 1337:Destination:22
This will allow you to directly SSH into Destination from Client in another session: 这将允许您在另一个会话中直接从客户端SSH到目标:
On Client: 在客户端:
ssh user@localhost -p 1337
The command runs as if you had typed ssh user@Destination . 该命令的运行就像您输入了ssh user @ Destination一样 。 You can pipe stdin to it as if you were directly connected to it.
您可以将stdin管道传输到它,就像您直接连接到它一样。
From what I see, the problem is ssh on the middle machine cannot get the standard input. 从我看到的,问题是中间机器上的ssh无法获得标准输入。 I guess it just want to ask for your password.
我想它只是想要你的密码。 If that is true, perhaps you should try to set up ssh key so that you can ssh without password.
如果这是真的,也许您应该尝试设置ssh密钥,以便您可以在没有密码的情况下进行ssh。 NOTE: If that success, you better create a new user on the middle machine to hold that key as a security measure.
注意:如果成功,您最好在中间计算机上创建一个新用户,以将该密钥作为安全措施。
Sounds like you want to set up public key authentication between the middle machine and end machines. 听起来好像你想在中间机器和终端机器之间设置公钥认证。
Here are a couple of decent guides to get you started. 这里有一些很好的指南可以帮助您入门。 Good luck.
祝好运。
http://hkn.eecs.berkeley.edu/~dhsu/ssh_public_key_howto.html
http://hkn.eecs.berkeley.edu/~dhsu/ssh_public_key_howto.html
http://pkeck.myweb.uga.edu/ssh/
http://pkeck.myweb.uga.edu/ssh/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.