Ниже приведен самый простой способ SSh в Java. Загрузите любой файл по приведенной ниже ссылке и распакуйте его, затем добавьте файл jar из извлеченного файла и добавьте его в путь сборки проекта.
http://www.ganymed.ethz.ch/ssh2/
и используйте метод ниже
public void SSHClient(String serverIp,String command, String usernameString,String password) throws IOException{
System.out.println("inside the ssh function");
try
{
Connection conn = new Connection(serverIp);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(usernameString, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
ch.ethz.ssh2.Session sess = conn.openSession();
sess.execCommand(command);
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
System.out.println("the output of the command is");
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
}
}