我正在嘗試通過 JSch –diff命令比較檔案。我想通過 Java 代碼獲取狀態以了解檔案是否等于 ie echo $status。我怎么能在java中做到這一點?
這是我運行 SSH 命令的課程
public class SSH {
static Logger logger = LogManager.getLogger("sshLogger");
static Process p;
static String s;
int exitStatus = -1;
String user;
String pass;
String server;
String command;
public Channel channel;
public Session session;
public InputStream in;
public SSH(String user, String pass, String server) {
this.user = user;
this.pass = pass;
this.server = server;
logger.debug("instance: " user "@" server);
}
private Session getConnectedSession(String user, String pass, String server) throws JSchException {
session = new JSch().getSession(user, server, 22);
java.util.Properties config = new java.util.Properties();
session.setPassword(pass);
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications","publickey,keyboard-interactive,password");
session.setConfig(config);
logger.debug("get connected session at: " server);
session.connect();
logger.debug("connected to session");
return session;
}
public Channel getChannel() {
// ByteArrayOutputStream byteArrOutSt = new ByteArrayOutputStream();
try {
session = getConnectedSession(user, pass, server);
logger.debug("opening channel at: " server);
channel = session.openChannel("shell");
} catch (JSchException e) {
logger.error("failed to connect to: " user "@" server);
e.printStackTrace();
}
// channel.setOutputStream(baos);
channel.setOutputStream(System.out);
channel.setExtOutputStream(System.out, true);
// channel.setPty(Boolean.FALSE);
return channel;
}
public String commandBuilder(String... commands) {
StringBuilder commandString = new StringBuilder();
for (String command : commands) {
logger.debug("adding command: " command);
commandString.append(command "\\n");
}
logger.debug("adding command: exit");
commandString.append("exit\\n");
String myCommand = commandString.toString().replace("\\n", "\n");
return myCommand;
}
public String executeCommand(String myCommand) {
channel = getChannel();
//String finishedCommand = commandBuilder(myCommand);
logger.debug(myCommand);
StringBuilder outBuff = new StringBuilder();
String outComm = null;
channel.setInputStream(new ByteArrayInputStream(myCommand.getBytes(StandardCharsets.UTF_8)));
try {
in = channel.getInputStream();
channel.connect();
// read from the output
System.out.println("printing");
while (true) {
for (int c; ((c = in.read()) >= 0);) {
outBuff.append((char) c);
}
if (channel.isClosed()) {
if (in.available() > 0)
continue;
exitStatus = channel.getExitStatus();
break;
}
}
} catch (IOException e) {
logger.error("failed while running or reading output of command: " myCommand);
} catch (JSchException e) {
logger.error("failed to connect to server while running command: " myCommand);
}
logger.info(outBuff.toString());
return outBuff.toString();
}
public void endSession() {
logger.debug("ending session");
channel.disconnect();
session.disconnect();
logger.debug("disconnected channel and session");
// print exit status
logger.debug("Exit status of the execution: " exitStatus);
if (exitStatus == 0) {
logger.debug(" (OK)\n");
} else {
logger.debug(" (NOK)\n");
}
}
public String runCommand(String... commands) {
// many commands as you want
String output = null;
String myCommands;
myCommands = commandBuilder(commands);
try {
output = executeCommand(myCommands);
endSession();
} catch (Exception e) {
logger.error("failed to run ssh comands: " commands);
e.printStackTrace();
}
return output;
}
}
以 SSHdiff為例:
SSH session = new SSH(user, pass, server);
session.runCommand("diff file1.xml file2.xml");
uj5u.com熱心網友回復:
首先不要在“shell”通道中執行命令。“shell”是一個只有輸入和輸出的黑盒子。查看JSch 中的“shell”通道和“exec”通道有什么區別
使用“執行”頻道。在“exec”頻道中,Channel.getExitStatus作品。
有關代碼示例,請參閱: 如何讀取 JSch 命令輸出?
強制警告:不要StrictHostKeyChecking=no用于盲目接受所有主機密鑰。那是一個安全漏洞。您失去了對MITM 攻擊的保護。有關正確(且安全)的方法,請參閱:How to resolve Java UnknownHostKey, while using JSch SFTP library?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406097.html
標籤:
上一篇:使用popen將緩沖區從我的程式傳輸到另一個程式的標準輸入
下一篇:少-一次搜索多個檔案
