简体   繁体   English

如何编写脚本以从远程计算机ssh到计算机?

[英]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.

相关问题 如何通过SSH运行Shell脚本,以使远程计算机的环境与本地计算机的环境相似? - How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer? 通过SSH在远程计算机上运行bash脚本时,如何在本地计算机上触发声音? - How do I get a sound to fire on local machine when I run a bash script on a remote computer via SSH? 如何列出远程计算机上的SSH连接顺序? - How can I list the SSH connection sequence on a remote computer? 如何使用 Git Bash 从我的 Windows 10 计算机中删除我的 SSH 密钥? - How do I delete my SSH Key from my windows 10 computer using Git Bash? 如何在bash脚本中使用ssh在计算机上sudo - How to sudo on a computer using ssh inside a bash script SSH进入远程计算机并编译/运行代码 - SSH into remote computer and compile/run code 如何在远程计算机上执行家用计算机上的git命令(pull,push)而无需在客户端上安装软件? - How can I execute git commands (pull, push) on my home computer from a remote computer without installing software on the client? 如何使用shell从计算机注销? - How do I logout from a computer using shell? 如何从另一台计算机运行bash脚本? - How to run a bash script from another computer? 使用bash和ssh如何从远程主机本地写入日志 - Using bash and ssh how do I write a log locally from a remote host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM