我正在编写一个应用程序来管理一些远程设备。我无法使用JSch重启dhcpdaemon。我这样做:
@Override
public void createSession(String hostname) throws JSchException {
this.session = jsch.getSession(username, hostname, 22);
this.session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
this.hostname = hostname;
}
private final String RESTART_COMMAND = "/etc/init.d/isc-dhcp-server restart";
public int reloadDHCPDaemon() throws IOException, JSchException, InterruptedException {
int exitStatus = -1;
String command = RESTART_COMMAND;
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setPty(true);
channel.setCommand(command);
channel.connect();
Thread.sleep(1000);
if (channel.isClosed() == true) {
exitStatus = channel.getExitStatus();
channel.disconnect();
}
return exitStatus;
}
无论命令是否成功结束,我都会获得4作为退出状态。相同的代码,但与其他命令,例如cp
工作正常。可以somone帮帮我吗?