JSch jsch = new JSch();
Session session = jsch.getSession("root", "服务器ip","端口");
session.setPassword("密码");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("docker"+" "+"ps");
channelExec.connect();
BufferedReader input = new BufferedReader(new InputStreamReader(channelExec.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
channelExec.disconnect();
session.disconnect();
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>