In practice, it is sometimes necessary to remotely start programs on other Linux hosts from the Web management interface, which can be easily met by SSH protocol. In fact, in the Hadoop architecture, this principle is used when the DN is started on the NN. GANYMED-SSH2 is an open source project that implements the SSH protocol, the project address is: http://ganymed-ssh-2.googlecode.com/(download source to turn strong, well-known reason), if only use, Pom.xml Add the following dependencies on the line:
1 <Dependency>2 <groupId>Ch.ethz.ganymed</groupId>3 <Artifactid>Ganymed-ssh2</Artifactid>4 <version>262</version>5 </Dependency>
For the sake of convenience, a tool class Sshutil.java is encapsulated:
Package Com.cnblogs.yjmyzz.utils;import Ch.ethz.ssh2.connection;import Ch.ethz.ssh2.session;import Ch.ethz.ssh2.streamgobbler;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.InputStream; Import java.io.inputstreamreader;/** * SSH tool class (can remotely execute shell commands on other Linux machines) * Created by Jimmy on 2015/7/6. * Http://code.taobao.org/p/y-lib/src/trunk/src/main/java/com/cnblogs/yjmyzz/utils/SSHUtil.java */public class Sshutil {/** * Connect to Host * * @param hostname * @param username * @param password * @return * @ Throws Exception */private static Connection getconnection (string hostname, string Username, string password) thro WS Exception {Connection conn = null; try {conn = new Connection (hostname); Conn.connect (); Boolean isauthenticated = Conn.authenticatewithpassword (username, password); if (isauthenticated = = False) {throw new IOException ("Authentication failed."); }} catch (Exception e) {throw new IOException ("Username or password error."); } return conn; /** * Execute REMOTE command * * @param hostname remote HOST IP * @param username user name * @param password password * @param c Ommand command to execute * @param timeout timeout (seconds) * @return * @throws Exception */public static String Execrem Otecommand (string hostname, string username, string password, string command, long timeout) throws Exception { Connection conn = getconnection (hostname, username, password); StringBuilder sb = new StringBuilder (); Session session = NULL; try {session = Conn.opensession (); Session.requestpty ("vt100", N, 640, 480, NULL); Session.execcommand (command); InputStream stdout = new Streamgobbler (Session.getstdout ()); BufferedReader br = new BufferedReader (new InputStreamReader (stdout)); Long start = System.currenttiMemillis (); char[] arr = new char[512]; int read; int i = 0; while (true) {read = Br.read (arr, 0, arr.length); if (Read < 0 | | | (System.currenttimemillis ()-start) > timeout *) {break; } sb.append (New String (arr, 0, read)); i++; }} finally {if (session! = NULL) {session.close (); } if (conn! = null) {conn.close (); }} return sb.tostring (); */** * Execute remote command (default 5 seconds Timeout) * * @param hostname remote HOST IP * @param username username * @param password Password * @param command to execute * @return * @throws Exception */public static string Execremotecommand (String host Name, string Username, string password, String command) throws Exception {return Execremotecommand (host Name, username, password, comMand, 5); /** * Bulk Execute remote command * * @param hostname remote HOST IP * @param username username * @param password Password * @param Command List of commands to execute * @param timeout timeout (seconds) * @return * @throws Exception */public static String exe Cremotecommand (string hostname, string username, string password, string[] command, long timeout) throws Except ion {Connection conn = getconnection (hostname, username, password); StringBuilder sb = new StringBuilder (); Session session = NULL; try {for (int t = 0; t < command.length; t++) {session = Conn.opensession (); Session.requestpty ("vt100", N, 640, 480, NULL); Session.execcommand (Command[t]); InputStream stdout = new Streamgobbler (Session.getstdout ()); BufferedReader br = new BufferedReader (new InputStreamReader (stdout)); Long start = System.currenttimemillis (); Char[] arr = new char[512]; int read; int i = 0; while (true) {read = Br.read (arr, 0, arr.length); if (Read < 0 | | | (System.currenttimemillis ()-start) > timeout *) {break; } sb.append (New String (arr, 0, read)); i++; } session.close (); }} finally {if (conn! = null) {conn.close (); }} return sb.tostring (); /** * Bulk Execute remote command (default 5-second timeout) * * @param hostname remote HOST IP * @param username username * @param password password * @param command List of commands to execute * @return * @throws Exception */public static string Execremotecommand (string Hostname, string Username, string password, string[] command) throws Exception {return Execremotecomman D (hostname, username, password, command, 5); }}
Key points of Use:
1. If you want to execute multiple commands consecutively, use the && connection, for example: CD/Switch to root directory, and then all files under the LS root directory, you can call:
public static void Main (string[] args) { String hostname = "172.21.129.**"; String username = "root"; String Password = "* * *"; try { System.out.println (Sshutil.execremotecommand (hostname, username, password, "PWD&&CD/& &pwd&&ls ")); } catch (Exception e) { e.printstacktrace (); } }
The above command is equivalent to the same session, continuous execution
Pwd
CD/
Pwd
Ls
2. If you want to invoke commands in a background process, add nohup directly to the command.
Remote execution of SHELL commands on other Linux machines using GANYMED-SSH2