Java 中的简单 SSH 隧道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1677248/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:17:45  来源:igfitidea点击:

Simple SSH Tunnel in Java

javasshtunnel

提问by User1

I want to create an SSH Tunnel in Java. I noticed quite a few Java SSH libraries on another post. Before I dig into each option, maybe someone can give me some code snippets of how they did it or at least tell me which client library would work best.

我想用 Java 创建一个 SSH 隧道。我在另一篇文章中注意到不少 Java SSH 库。在我深入研究每个选项之前,也许有人可以给我一些他们是如何做到的代码片段,或者至少告诉我哪个客户端库最有效。

I only need tunneling. I won't need stuff like file transfers, terminal emulation, etc. Is there a simple few lines of code that can forward a port on the server to work on my client's localhost adapter? Ideally both client and server would be in Java, but I'll settle for just client for now.

我只需要隧道。我不需要文件传输、终端仿真等东西。是否有几行简单的代码可以转发服务器上的端口以在我客户端的本地主机适配器上工作?理想情况下,客户端和服务器都将使用 Java,但我现在只使用客户端。

采纳答案by Pascal Thivent

Well, as pointed out in the other question, JSchis indeed a great choice and has several examples here. The PortForwardingL.javaclass might be a good starting point.

好吧,正如在另一个问题中所指出的,JSch确实是一个不错的选择,这里有几个例子。该PortForwardingL.java类可能是一个很好的起点。

回答by ZZ Coder

You can do this with several libraries. My favorite is the ssh library inside MindTerm package,

您可以使用多个库执行此操作。我最喜欢的是 MindTerm 包中的 ssh 库,

http://linuxmafia.com/pub/java/ISNetworks-MindTerm-1.2.1-SCP3.tar.gz

http://linuxmafia.com/pub/java/ISNetworks-MindTerm-1.2.1-SCP3.tar.gz

You can open a tunneled connection like this,

您可以像这样打开隧道连接,

  SSHSocketFactory fact = new SSHSocketFactory(sshHost, sshPort, new SSHPasswordAuthenticator(sshUser, sshPassword));

  sock = fact.createSocket(host, port);