所以,我有一些很好的代码(我很难理解),它允许我向我的服务器发送命令,并获得一行响应。代码有效,但我希望从服务器返回多行。
主要课程是:
JSch jSch = new JSch();
MyUserInfo ui = new MyUserInfo();
String Return = "Error";
try {
String host = "Username@HostName";
String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);
Session rebootsession = jSch.getSession(user, host, 22);
rebootsession.setPassword(Password);
rebootsession.setUserInfo(ui);
rebootsession.connect();
Channel channel = rebootsession.openChannel("exec");
((ChannelExec) channel).setCommand(Command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) break;
Return = new String(tmp, 0, i);
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
rebootsession.disconnect();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
return Return;
我希望能够从第一堂课返回多行。 任何帮助将不胜感激!
答案 0 :(得分:0)
您必须将输出连接到Return
变量,而不是在每次传递中覆盖它:
Return += new String(tmp, 0, i);