简体   繁体   English

Java Ganymed-SSH-从缓冲区读取时挂起

[英]Java Ganymed-SSH - Hangs while reading from Buffer

currently I am trying to run some commands on a ssh-server using the Ganymed-SSH2-Library for Java. 目前,我正在尝试使用用于Java的Ganymed-SSH2-Library在ssh服务器上运行一些命令。 Because I also need to call a perl script, i can't use Session.execCommand(); 因为我还需要调用Perl脚本,所以我不能使用Session.execCommand(); , because it will just run "normal" unix-commands (or is this wrong?). ,因为它只会运行“正常”的Unix命令(或者这是错误的吗?)。 Instead I need to use a pseudoterminal. 相反,我需要使用伪终端。

If I run the following code it hangs: 如果我运行以下代码,它将挂起:

session = con.openSession();
session.requestPTY("vt220");
session.execCommand("/bin/bash");
session.getStdin().write("echo test".getBytes());
int b = 1;
InputStream is = new StreamGobbler(session.getStdout());
while ((b = is.read()) != -1) {
    System.out.print((char)b);
}
System.out.println("finished");

will result in the following output (censored USERNAME and HOST): 将导致以下输出(检查USERNAME和HOST):

echo testUSERNAME@HOST:~

# echo test

but the code will hang at this point and never reach System.out.println("finished"); 但是代码将在此时挂起,并且永远不会到达System.out.println("finished");

The connection-build up works perfectly, if i run the following code, it works: 建立连接的效果非常好,如果我运行以下代码,则可以正常工作:

session = con.openSession();
session.execCommand("ls");
BufferedReader reader = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
String line = reader.readLine();
while (line != null) {
    line = reader.readLine();
    System.out.println(line);
}
System.out.println("finished");

Anyone knows, where the problem is? 谁知道,问题出在哪里? Some time ago I tried this with Jsch (another SSH-Lib for Java) but it has the same problems. 不久前,我尝试使用Jsch(Java的另一个SSH-Lib)进行此操作,但是它存在相同的问题。

Thanks in advance :) 提前致谢 :)

I guess that the problem has not come from JSch and Ganymed. 我想问题不在于JSch和Ganymed。 How about the following? 接下来呢?

session.getStdin().write("echo test; exit".getBytes());

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

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