我需要在远程计算机上运行 shell 脚本。我正在使用 JSch 连接到远程计算机并使用ChannelExec. 我需要知道如何才能知道执行命令时是否出现任何错误。
以下是我的代码
ChannelExec channel = (ChannelExec) session.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream()));
String command = scriptName;
if(parameter != null && !parameter.isEmpty()){
command += " " + parameter;
}
LOGGER.debug("Command To be Executed: " + command);
channel.setCommand(command);
channel.connect();
//capture output
String msg;
StringBuffer output = new StringBuffer();
while ((msg = in.readLine()) != null)
{
//output = output + msg;
output.append(msg);
}
LOGGER.debug("Output Message = " + output.toString());
LOGGER.debug("ERROR STATUS --" + channel.getExitStatus());
channel.disconnect(); …Run Code Online (Sandbox Code Playgroud)