2016-11-30 115 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


在您的测试环境中,连接或服务器可能足够快以在读取它之前填写退出状态。而在生产服务器上则不是。一个简单的竞赛条件。