一、依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kafkademo</groupId>
<artifactId>kafkademo</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、Host类
public class DestHost {
private String host;
private String username;
private String password;
private int port = 22;
private int timeout = 60 * 60 * 1000;
public DestHost(String host, String username, String password) {
this(host, username, password, 22, 60 * 60 * 1000);
}
public DestHost(String host, String username, String password, int timeout) {
this(host, username, password, 22, timeout);
}
public DestHost(String host, String username, String password, int port, int timeout) {
super();
this.host = host;
this.username = username;
this.password = password;
this.port = port;
this.timeout = timeout;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
}
三、GanymedUtils
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
public class GanymedUtils {
private static final int TIME_OUT = 0;
public static Session getSession(DestHost host) {
Connection connection = new Connection(host.getHost());
try {
connection.connect();
boolean isLogin = connection.authenticateWithPassword(host.getUsername(), host.getPassword());
if (!isLogin) {
throw new Exception("远程登录服务器失败");
}
Session session = connection.openSession();
session.requestPTY("bash");
session.startShell();
return session;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String executeShell(DestHost host, String[] commands) {
Session session = getSession(host);
PrintWriter out = new PrintWriter(session.getStdin());
for (String command : commands) {
out.println(command);
}
out.flush();
out.close();
try {
session.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF |
ChannelCondition.EXIT_STATUS , TIME_OUT);
} catch (IOException e) {
e.printStackTrace();
}
String result = getResult(new StreamGobbler(session.getStdout()));
session.close();
return result;
}
private static String getResult(InputStream in) {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
try {
while (in.read(buf) != -1) {
String t = new String(buf, StandardCharsets.UTF_8);
System.out.print(t);
sb.append(t);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
DestHost host = new DestHost("192.168.163.130", "oracle", "xxxxxx");
String[] commands = new String[]{
"cd /home/oracle/",
"pwd",
"exit"
};
String result = GanymedUtils.executeShell(host, commands);
System.out.println(result);
}
}