【问题标题】:TCP Connection over a secure ssh connection通过安全 ssh 连接的 TCP 连接
【发布时间】:2013-02-19 02:05:44
【问题描述】:

我正在尝试使用 JSCH 连接到远程服务器,然后从该服务器通过 tcp/ip 端口打开类似 telnet 的会话。说连接到服务器 A,一旦连接,就会通过另一个端口向服务器 B 发出 tcp 连接。在我的网络服务器日志中,我看到了一个 GET / 已记录但不是 GET /foo 正如我所期望的那样。我在这里想念什么? (我不需要使用端口转发,因为我连接的系统可以访问远程端口)

package com.tekmor;

import com.jcraft.jsch.*;

import java.io.BufferedReader;
.
.

public class Siranga {

     public static void main(String[] args){
        Siranga t=new Siranga();
           try{
               t.go();
           } catch(Exception ex){
               ex.printStackTrace();
        }
    }
    public void go() throws Exception{
        String host="hostXXX.com";
        String user="USER";
        String password="PASS";
        int port=22;

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");      

        String remoteHost="hostYYY.com";
        int remotePort=80;


       try { 
        JSch jsch=new JSch();
        Session session=jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        Channel channel=session.openChannel("direct-tcpip");  


        ((ChannelDirectTCPIP)channel).setHost(remoteHost);
        ((ChannelDirectTCPIP)channel).setPort(remotePort);

        String cmd = "GET /foo";

        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);
              System.out.println(numRead);
        }

        out.flush();



        channel.disconnect();
        session.disconnect();

        System.out.println("foo");

       }
       catch (Exception e){
           e.printStackTrace();

       }

    }
}

【问题讨论】:

    标签: ssh jsch channel


    【解决方案1】:

    再次阅读您的HTTP specification。请求标头应以空行结束。所以假设你没有更多的标题行,你至少应该在最后换行。 (此处的换行表示 CRLF 组合。)

    此外,请求行应在 URL 后包含 HTTP 版本标识符。

    所以试试这个改变你的程序:

    String command = "GET /foo HTTP/1.0\r\n\r\n";
    

    作为提示:您可以使用setInputStream method,而不是手动将数据从 ByteArrayInputStream 传送到通道的输出流。另外,不要忘记从通道的输入流中读取结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-06
      • 2019-11-25
      • 1970-01-01
      • 2021-11-01
      • 2010-12-22
      • 2014-09-01
      • 1970-01-01
      • 2018-02-05
      相关资源
      最近更新 更多