求助:关于使用ganymed-ssh2-build210.jar执行多条命令的问题
最近碰到一个问题,ganymed-ssh2-build210.jar中可以在一个session里执行多条命令?
代码如下:
try {
ConnectionInfo info = conn.connect(null,time_out,0);
boolean result = conn.authenticateWithPassword(username, password);
if (result!=false) {
Session sshsession = conn.openSession();
for (int i = 0; i < commands.size(); i++){
if (sshsession!=null) {
sshsession.execCommand(commands.get(i));
in = new StreamGobbler(sshsession.getStdout());
reader = new BufferedReader(new InputStreamReader(in));
String response = null;
while ((response = reader.readLine()) != null)
builder.append(response + "\n");
sshsession.waitForCondition(ChannelCondition.EXIT_STATUS | ChannelCondition.CLOSED | ChannelCondition.EOF,time_out);
if(sshsession.getExitStatus()==null){
break;
}
}
}
if (builder.toString().isEmpty()) {
for (String command : commands) {
builder.append(command + ": command not found\n");
}
}
}
resu = builder.toString();
resu = new String(resu.getBytes());
return resu;
}catch (IOException e) {
logger.fatal("error", e);
return e.getMessage();
} finally {
if (reader != null) {
try {
reader.close();
in.close();
} catch (final IOException e) {
logger.fatal("error", e);
}
}
conn.close();
}
这里执行完一条命令后 sshsession.getExitStatus()始终为null,导致不能继续执行下一条命令