Guest User

Untitled

a guest
Jun 28th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import net.schmizz.sshj.SSHClient;
  2. import net.schmizz.sshj.common.IOUtils;
  3. import net.schmizz.sshj.connection.channel.direct.Session;
  4.  
  5. import java.io.IOException;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. /** This examples demonstrates how a remote command can be executed. */
  9. public class Exec {
  10.  
  11. public static void main(String... args)
  12. throws IOException {
  13. String user = "pi";
  14. String password = "raspberry";
  15. String host = "192.168.1.10";
  16. int port = 22;
  17.  
  18. final SSHClient ssh = new SSHClient();
  19. ssh.loadKnownHosts();
  20. //disable verification altogether:
  21. // sshClient.addHostKeyVerifier(new PromiscuousVerifier());
  22.  
  23. ssh.connect(host);
  24. try {
  25. ssh.authPassword(user,password);
  26.  
  27. final Session session = ssh.startSession();
  28. try {
  29. String execPing = "ping -c 1 google.com";
  30. final Session.Command cmd = session.exec(execPing);
  31.  
  32. System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
  33.  
  34. cmd.join(5, TimeUnit.SECONDS);
  35.  
  36. System.out.println("\n** exit status: " + cmd.getExitStatus());
  37.  
  38. } finally {
  39. session.close();
  40. }
  41. } finally {
  42. ssh.disconnect();
  43. }
  44. }
  45.  
  46. }
Add Comment
Please, Sign In to add comment