简体   繁体   English

Java中的简单SSH隧道

[英]Simple SSH Tunnel in Java

I want to create an SSH Tunnel in Java. 我想用Java创建一个SSH隧道。 I noticed quite a few Java SSH libraries on another post . 我在另一篇文章中发现了不少Java SSH库。 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. 在深入研究每个选项之前,也许有人可以给我一些他们如何做到的代码片段,或者至少告诉我哪个客户端库最有效。

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? 我不需要文件传输,终端仿真等等。是否有简单的几行代码可以转发服务器上的端口以在我的客户端的localhost适配器上工作? Ideally both client and server would be in Java, but I'll settle for just client for now. 理想情况下,客户端和服务器都是Java,但我现在只会满足于客户端。

Well, as pointed out in the other question, JSch is indeed a great choice and has several examples here . 好吧,正如另一个问题所指出的, JSch确实是一个很好的选择,并且在这里有几个例子。 The PortForwardingL.java class might be a good starting point. PortForwardingL.java类可能是一个很好的起点。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM