2012-12-10 40 views
0

當前我正在嘗試使用Ganymed-SSH2-Library for Java在ssh服務器上運行一些命令。因爲我還需要調用perl腳本,所以我不能使用Session.execCommand();,因爲它只會運行「正常」的unix命令(或者這是錯誤的?)。相反,我需要使用僞終端。Java Ganymed-SSH - 從緩衝區讀取時掛起

如果我運行下面的代碼搞得暈頭轉向:

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"); 

將導致下面的輸出(審查USERNAME和HOST):

echo [email protected]:~ 

# echo test 

但代碼將在該點掛永不達到System.out.println("finished");

連接建立完美的作品,如果我運行下面的代碼,它的工作原理:

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"); 

任何人都知道,問題在哪裏?前段時間我用Jsch(另一個用於Java的SSH-Lib)嘗試了這個,但它也有同樣的問題。

感謝提前:)

回答

1

我想,這個問題不是來自JSch和Ganymed。 以下情況如何?

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

這將給我以下輸出,但也掛起:'回聲測試; exitUSER @ HOST:〜 #echo test;退出' – user1571117

+0

我不知道Ganymed,但是你對JSch有同樣的結果嗎? – ymnk

+0

這也行不通:它像以前一樣掛起 – user1571117