How to implement a remote connection server to execute a script in a Java program? Sometimes there is such a need. If you have to go to another machine to execute the script, you can use the tool class below, which has been encapsulated and can be used directly.

The author uses the Maven project to demonstrate how to use:
1) First, introduce dependency:

 <!--  https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2  --> <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>build209</version> </dependency>

2) Copy the following tool class: RemoteShellExecutorUtils.java

 /** * RemoteShellExecutorUtils * * @author lcry * @date 2019/08/24 18:00 */ import ch.ethz.ssh2.ChannelCondition; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import org.slf4j. Logger; import org.slf4j. LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.nio.charset. Charset; public class RemoteShellExecutorUtils { private static final Logger LOG = LoggerFactory.getLogger(RemoteShellExecutorUtils.class); private Connection conn; private String ip; private String usr; private String psword; private int port; private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; /** *Remote information * * @param ip * @param usr * @param psword */ public RemoteShellExecutorUtils(String ip, int port, String usr, String psword) { this.ip = ip; this.usr = usr; this.port = port; this.psword = psword; } /** *Remote login * * @return * @throws IOException */ private boolean login() throws IOException { conn = new Connection(ip, port); conn.connect(); return conn.authenticateWithPassword(usr, psword); } /** *Execute command * * @param cmds * @return * @throws IOException */ public String exec(String cmds) throws IOException { InputStream stdOut = null; InputStream stdErr = null; String outStr = ""; String outErr = ""; int ret = -1; try { if (login()) { Session session = conn.openSession(); session.execCommand(cmds); stdOut = new StreamGobbler(session.getStdout()); outStr = processStream(stdOut, charset); LOG.info ("Execution log: [INFO] outStr="+outStr); stdErr = new StreamGobbler(session.getStderr()); outErr = processStream(stdErr, charset); LOG.info ("Execution log: [INFO] outErr="+outErr); session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); ret = session.getExitStatus(); } else { LOG. error ("Execution log: [INFO] ssh2 login failure:"+ip); throw new IOException("SSH2_ERR"); } } finally { if (conn != null) { conn.close(); } if (stdOut != null) { stdOut.close(); } if (stdErr != null) { stdErr.close(); } } return outStr; } private String processStream(InputStream in, String charset) throws IOException { byte[] buf = new byte[1024]; StringBuilder sb = new StringBuilder(); while (in.read(buf) != - 1) { sb.append(new String(buf, charset)); } return sb.toString(); } /** *Tool class * *@ param usr remote user name *@ param password *@ param serverIP server IP *@ param port 22 by default *@ param shPath Execute script *@ return Return execution information */ public static String ShellExecutor(String usr, String password, String serverIP, int port, String shPath) { RemoteShellExecutorUtils exe = new RemoteShellExecutorUtils(serverIP, port, usr, password); String outInf = ""; try { String cmd = shPath; outInf = exe.exec(cmd); } catch (IOException e) { e.printStackTrace(); } return outInf; }

3) Project Usage
RemoteShellExecutorUtils. ShellExecutor("root", "root", "192.168.254.140", 22, "/test.sh");
Parameter description:
1. User name of remote machine
2. Remote machine password
3. Remote machine IP
4. SSH port number defaults to 22
5. Script to be executed