当前位置: 首页 > 工具软件 > jail-shell > 使用案例 >

java ssh 执行时间很长_java ssh执行shell脚本

华俊弼
2023-12-01

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");

}

 类似资料: