Using GANYMED-SSH2 to invoke commands to execute remote Linux, look up on Maven and find that the most used package is Ganymed-ssh2-build210.jar, so download it
private static final string hostname = "115.182.62.225";//linux IP Address private static final string username = "root";//lin
UX username private static final String password = "J\\^pr$&zslxcqhif";//linux password private static Connection conn;
private static session SSH = NULL; /** * Determine if you can connect the host, account password is correct (default configured server) * @return/public static Boolean hasconnection () {return hasconnection (host
Name,username,password,false); /** * Execute command (default configured server) * @param cmd * @return/public static string Excutecmd (string cmd) {return Excutecmd (
Hostname,username,password,cmd); /** * Determine whether the host can be connected, the account password is correct * @param hostname * @param username * @param password * @param flag whether the query is only connected to the service Device * @return/public static Boolean hasconnection (String hostname,string username,string Password,boolean flag) {b
Oolean Isconn = false;
conn = new Connection (hostname);
try {//Connect to host Conn.connect (); Use user name and password checksum isconn = Conn.authentiCatewithpassword (username, password);
catch (IOException e) {e.printstacktrace ();
} if (flag) {closeconnection ();
return isconn; /** * Execute command * @param hostname * @param username * @param password * @param cmd * @return/Public stat IC string Excutecmd (string hostname,string username,string password,string cmd) {StringBuilder result = new Stringbuilde
R ();
Boolean isconn = Hasconnection (Hostname,username,password,false);
if (!isconn) {//Connection failed Result.append ("Connection Server failed");
}else{try {ssh = conn.opensession ();
Use multiple commands to separate Ssh.execcommand (CMD) with semicolons; Only one line of commands is allowed, that is, the SSH object can only be used once execcommand this method, multiple use will appear an exception//print all the text on the screen inputstream is = new STR
Eamgobbler (Ssh.getstdout ());
BufferedReader BRS = new BufferedReader (new InputStreamReader (IS));
while (true) {String line = Brs.readline ();
if (line==null) { Break
} result.append (line+ "\ n");
The catch (IOException e) {result.append ("Connection Server failed");
E.printstacktrace ();
} return result.tostring ();
/** * Close connected object/private static void CloseConnection () {//connected session and connection object all need to close ssh.close ();
Conn.close ();
public static void Main (string[] args) {System.out.println (Excutecmd ("Cd/home/;ls")); }