【问题标题】:Execution hangs after Running telnet command on remote SSH session using JSch使用 JSch 在远程 SSH 会话上运行 telnet 命令后执行挂起
【发布时间】:2017-08-13 16:07:00
【问题描述】:

我正在寻找一种解决方案,先通过 Java 进行 SSH 连接,然后通过 Telnet 连接到远程系统。由于仅使用 telnet 连接,我可以在远程机器上执行该特定命令。

在浏览了很多之后,我找到了这个答案“https://stackoverflow.com/questions/27146991/running-telnet-command-on-remote-ssh-session-using-jsch”但是在执行“telnet localhost 4444”之后,程序执行挂起并且永远不会从 while 循环中出来。因此,在进行 telnet 连接后,我无法执行其他命令。

我的代码是:-

    public static void main(String[] arg) {
    try {
        System.out.println(telnetConnection(command, puttyUserName,
    puttyPassword, puttyHostName));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String telnetConnection(String command, String user, String password, String host)
        throws JSchException, Exception {
    JSch jsch = new JSch();
    jsch.addIdentity(puttyPublicKey, puttyPassword);
    Session session = jsch.getSession(user, host, 22);

    session.setConfig("StrictHostKeyChecking", "no");

    session.connect(500);//This timeout is not working as mentioned in the example. Program execution never stops.

    Channel channel = session.openChannel("shell");

    channel.connect(500);

    DataInputStream dataIn = new DataInputStream(channel.getInputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(dataIn));
    DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
    System.out.println("Starting telnet connection...");
    dataOut.writeBytes("telnet localhost 4444\r\n"); after this no commands executes
    dataOut.writeBytes(command + "\r\n"); 

    dataOut.writeBytes("quit\r\n");

// 使用 quit 我可以退出 telnet 会话,同时通过 putty 手动操作,因为 exit 不起作用

    dataOut.writeBytes("exit\r\n"); // exit from shell
    dataOut.flush();
    String line = reader.readLine(); 
    String result = line + "\n";
    while (!(line = reader.readLine()).equals("Connection closed by foreign host")) 
    {
        result += line + "\n";

        System.out.println("heart beat" + result);
    }

    System.out.println("after while done");
    dataIn.close();
    dataOut.close();
    channel.disconnect();
    session.disconnect();
    System.out.println("done");
    return result;
}

}

输出//

心跳 telnet localhost 4444

START TRANSLATOR ABCLOC HIGH

退出

退出

[XYZ]$ 远程登录 localhost 4444 正在尝试 x.x.x.1...

连接到 localhost.localdomain (x.x.x.1)。

转义字符是'^]'。

到接口层的连接 心跳 telnet localhost 4444

START TRANSLATOR ABCLOC HIGH

退出

退出

[XYZ]$ 远程登录 localhost 4444 正在尝试 x.x.x.1...

连接到 localhost.localdomain (x.x.x.1)。

转义字符是'^]'。

到接口层的连接 为命令列表键入“帮助”

////在这个程序挂起并且没有执行任何操作之后

【问题讨论】:

    标签: java ssh telnet jsch


    【解决方案1】:

    我没有和你一样在端口 4444 上运行相同的服务器,但是在 JSch 中发出“telnet localhost 80”和“GET /”的本地测试中,我得到了类似的行为。在我的情况下,解决方法是要小心“连接关闭”消息的准确性。在我的情况下,服务器发送:

    连接被外部主机关闭。

    而您的代码正在检查

    连接被外部主机关闭

    (没有句号),因此循环永远不会终止。你可以在https://github.com/pgleghorn/JSchTest找到我的测试代码

    【讨论】:

    • 谢谢@Phil - 但我也尝试使用您提到的相同的连接关闭消息。结果是一样的。
    • 主要问题是在执行 Telnet localhost 4444 之后它不允许我发送/执行任何其他命令...我发现这种问题是开放的@many网站...前- [链接] (android.stackexchange.com/questions/161461/…)
    猜你喜欢
    • 2015-01-24
    • 1970-01-01
    • 2022-06-06
    • 2021-01-29
    • 2020-12-04
    • 2011-05-27
    • 2017-03-19
    • 1970-01-01
    • 2011-01-25
    相关资源
    最近更新 更多