currently I am trying to run some commands on a ssh-server using the Ganymed-SSH2-Library for Java. Because I also need to call a perl script, i can’t use Session.execCommand();, because it will just run “normal” unix-commands (or is this wrong?). 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):
echo testUSERNAME@HOST:~
# echo test
but the code will hang at this point and never reach 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.
Thanks in advance 🙂
I guess that the problem has not come from JSch and Ganymed.
How about the following?