2016-11-30 112 views
0

我檢查在Java中遠程使用JSch服務器的文件修改的狀態。除了輸出外,我還在檢查退出代碼。 在測試服務器上運行良好,並給予退出代碼爲0,生產服務器是扔退出代碼-1。我如何獲得關於這個-1及其含義的更多細節。SSH命令-1

這裏是我的代碼,敏感區域移除。
如何獲得最後一行getExitStatus更多細節。

JSch js=new JSch(); 
Session s = js.getSession(username, host, 22); 
s.setPassword(password); 

Properties ssconfig = new Properties(); 
ssconfig.put("StrictHostKeyChecking", "no"); 
s.setConfig(ssconfig); 
s.connect(); 

Channel c = s.openChannel("exec"); 
ChannelExec ce = (ChannelExec) c; 

ce.setCommand("cd <some file dir> && date && ls -ltr"); 
ce.setErrStream(System.err); 

ce.connect(); 

/* 
* temporary test print 
*/ 
BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream())); 
String line; 
while ((line = reader.readLine()) != null) { 
    System.out.println(line); 
} 

/* 
* end of temporary print 
*/ 
ce.disconnect(); 
s.disconnect(); 

System.out.println("Exit code: " + ce.getExitStatus()); 

return ce.getExitStatus(); 

回答

1

getExitStatus返回-1,當退出代碼還不知道。

您可能需要調用Channel.disconnect之前等待Channel.isClosed


在您的測試環境中,連接或服務器可能足夠快以在讀取它之前填寫退出狀態。而在生產服務器上則不是。一個簡單的競賽條件。