1.添加依赖
com.jcraft:jsch
ch.ethz.ganymed:ganymed-ssh2:262
2.获取连接
conn = new Connection(ip, port);
conn.connect();
conn.authenticateWithPassword(username, password);
3.发送命令
session = conn.openSession();
session.execCommand(cmd);
4.结果
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
StringBuilder result = new StringBuilder();
while(true) {
String line = reader.readLine();
if (line == null) {
break;
}
result.append(line + "\n");
}