我正在寻找一种在java应用程序中使用ssh绑定连接到远程服务器的方法。我在终端中输入如下命令来连接我的服务器:
ssh -D 1234 username@w.x.y.z然后我可以将我的浏览器socks ip和端口配置为:
socks ip: 127.0.0.1
socks port: 1234使用我的服务器在internet内浏览(&U)
现在,请帮助我在我的java应用程序中做到这一点。
目前,我在我的程序中使用一个名为JSCH的库,但是我无法让我的应用程序工作。对于这个问题,你有什么想法或者示例代码或者别的什么吗?
(请注意,在java库中应该同时支持SOCKS v4和v5 )
发布于 2014-05-13 03:32:52
您将获得多个选项
详细说明:http://mina.apache.org/sshd-project/documentation.html值得信赖的Apache项目,在用户社区、文档和示例方面都有很好的支持,我想这就是您所要求的。https://svn.apache.org/repos/asf/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
详细说明:http://www.jcraft.com/jsch/文档不好,但已经存在了很长的time.Sample代码,相当不错。
http://code.google.com/p/ganymed-ssh-2/
另一个只有足够文档的库。
发布于 2013-04-06 19:28:12
我使用jsch作为示例程序在scala中破解了这样一个程序,
https://github.com/ymnk/dpfwds发布于 2020-09-20 13:12:41
对于每个https://www.bytefold.com/java-ssh-tunnel-with-dynamic-port-forwarding/,您可以使用Apache Mina来完成此操作:
依赖项
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>3.0.0-M2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-core -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-putty</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>2.1.0</version>
</dependency>代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.auth.hostbased.HostKeyIdentityProvider;
import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.config.keys.loader.pem.PEMResourceParserUtils;
import org.apache.sshd.common.config.keys.loader.putty.PuttyKeyUtils;
import org.apache.sshd.common.forward.PortForwardingEventListener;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.apache.sshd.server.channel.PuttyRequestHandler;
import org.apache.sshd.server.forward.AcceptAllForwardingFilter;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
/**
* This class
*
* @author Ankit Katiyar
*
*/
public class AmazonTest {
private static String BASTION_SERVER_PASSWORD = "P@ssword1";
private static final String BASTION_SERVER_USER = "ec2-user";
private static final String BASTION_SEREVR_HOST = "ec2-18-191-207-91.us-east-2.compute.amazonaws.com";
private static final String URL_TO_ACCESS = "http://www.google.com";
public static void main(String[] args) {
try {
Collection<KeyPair> keys = null;
// OPtional loading keys from a PEM file
//keys=PEMResourceParserUtils.getPEMResourceParserByAlgorithm("RSA").loadKeyPairs(ClassLoader.getSystemResource("local-ps-test.pem").toURI().toURL(), null);
// Optional: Using Putty key for login
keys=PuttyKeyUtils.DEFAULT_INSTANCE.loadKeyPairs(ClassLoader.getSystemResource("local-ps-private-key.ppk").toURI().toURL(), null);
SshClient client = SshClient.setUpDefaultClient();
client.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
client.start();
// using the client for multiple sessions...
try (ClientSession session = client.connect(BASTION_SERVER_USER, BASTION_SEREVR_HOST, 22).verify()
.getSession()) {
// IF you use password to login provide here
// session.addPasswordIdentity(BASTION_SERVER_PASSWORD); // for password-based
// authentication
session.addPublicKeyIdentity(keys.iterator().next());
// authentication
// Note: can add BOTH password AND public key identities - depends on the
// client/server security setup
session.auth().verify(10000);
// start using the session to run commands, do SCP/SFTP, create local/remote
// port forwarding, etc...
session.addPortForwardingEventListener(new PortForwardingEventListener() {
@Override
public void establishedDynamicTunnel(Session session, SshdSocketAddress local,
SshdSocketAddress boundAddress, Throwable reason) throws IOException {
// TODO Auto-generated method stub
PortForwardingEventListener.super.establishedDynamicTunnel(session, local, boundAddress, reason);
System.out.println("Dynamic Forword Tunnel is Ready");
}
});
SshdSocketAddress sshdSocketAddress = session
.startDynamicPortForwarding(new SshdSocketAddress("localhost", 8000));
System.out.println("Host: " + sshdSocketAddress.getHostName());
System.out.println("Port: " + sshdSocketAddress.getPort());
// Create a Proxy object to work with
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(sshdSocketAddress.getHostName(), sshdSocketAddress.getPort()));
/**
* Now you can use this proxy instance into any URL until this SSH session is active.
*/
// TEST one URL
HttpURLConnection connection = (HttpURLConnection) new URL(URL_TO_ACCESS).openConnection(proxy);
System.out.println("Proxy work:" + connection.getURL());
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println("================== Data From URL ==================\n");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
System.out.println("================== Data From URL ==================\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}他的项目可以在https://github.com/ankitkatiyar91/java-framework-examples/tree/master/java-tunneling上找到
依赖项可能已过期,API可能已更改,但这应该可以帮助您入门。我用最新版本的Apache Mina (2.1.4)在30分钟内就完成了这项工作。不要被他使用的mina-core 3.0.0-M2版本所折服。此版本实际上早于版本2.1.4。
https://stackoverflow.com/questions/15847961
复制相似问题