问题描述
使用,我尝试创建使用JSch连接到SSH服务器的代码,然后使用它连接到Web中的服务器并将HTTP请求传递给服务器,基本上使用SSH服务器作为代理。这就是我所拥有的:
Using the code at http://www.jcraft.com/jsch/examples/StreamForwarding.java.html, I have tried to create code that uses JSch to connect to an SSH server, then use it to connect to a server in the Web and pass HTTP(s) requests to the server, basically using the SSH server as a proxy. Here is what I have:
//SSH server and credentials
String host = "00.000.000.00";
String user = "login";
String password = "password";
int port = 22;
//The host I want to send HTTP requests to - the remote host
String remoteHost = "test.com";
int remotePort = 80;
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig(config);
session.connect();
Channel channel = session.getStreamForwarder(remoteHost, remotePort);
//The GET request
String cmd = "GET /foo/foo HTTP/1.0\r\n\r\n";
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.connect(10000);
byte[] bytes = cmd.getBytes();
InputStream is = new ByteArrayInputStream(cmd.getBytes("UTF-8"));
int numRead;
while ((numRead = is.read(bytes)) >= 0)
out.write(bytes, 0, numRead);
out.flush();
channel.disconnect();
session.disconnect();
System.out.println("Request supposed to have been sent");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
for (String line; (line = reader.readLine()) != null;){
System.out.println(line);
}
} catch (java.io.IOException exc) {
System.out.println(exc.toString());
}
} catch (Exception e){
e.printStackTrace();
}
这在理论上听起来不错,但实际上它不起作用。我认为我可以实现本地端口转发以使其工作,但我不明白如何。有人可以帮帮我吗?
This all sounds fine in theory, but in reality it doesn't work. I think that I could probably implement local port forwarding to make it work, but I don't understand how. Could somebody please help me out?
推荐答案
Channel channel = session.getStreamForwarder(remoteHost, remotePort);
[...]
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
[...]
while ((numRead = is.read(bytes)) >= 0)
out.write(bytes, 0, numRead);
[...]
channel.disconnect();
session.disconnect();
[...]
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
for (String line; (line = reader.readLine()) != null;){
您正在关闭频道并在发送HTTP命令后立即删除SSH会话。如果远程HTTP服务器未记录请求,则很可能是因为您在远程 sshd
实例有机会将请求转发到HTTP服务器之前删除了会话。
You are closing the channel and dropping the SSH session immediately after sending the HTTP command. If the remote HTTP server isn't logging the request, it's very possibly because you dropped the session before the remote sshd
instance had a chance to forward the request to the HTTP server.
阅读您已关闭的频道的回复也不会有效。
Reading the response from the channel which you've already closed isn't going to work either.
在你完成之前不要关闭频道或会话。
Don't close the channel or the session until you're done with them.
这篇关于Java JSch创建通过HTTP访问远程服务器的通道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!