Java connects to the server remotely and executes the script command tool class
<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 --> <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>build209</version> </dependency>
/** * 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; }