Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public void run (String command) {
  2. String hostname = "130.237.177.213";
  3. String username = "pi";
  4. String password = "raspberry";
  5. try
  6. {
  7. StrictMode.ThreadPolicy policy = new
  8. StrictMode.ThreadPolicy.Builder()
  9. .permitAll().build();
  10. StrictMode.setThreadPolicy(policy);
  11.  
  12. Connection conn = new Connection(hostname); //init connection
  13. conn.connect(); //start connection to the hostname
  14. boolean isAuthenticated =
  15. conn.authenticateWithPassword(username, password);
  16. if (isAuthenticated == false)
  17. throw new IOException("Authentication failed.");
  18. Session sess = conn.openSession();
  19. sess.execCommand(command);
  20. InputStream stdout = new StreamGobbler(sess.getStdout());
  21. BufferedReader br = new BufferedReader(new
  22. InputStreamReader(stdout)); //reads text
  23. while (true)
  24. { String line = br.readLine(); // read line
  25. if (line == null)
  26. break;
  27. System.out.println(line); }
  28. /* Show exit status, if available (otherwise "null") */
  29. System.out.println("ExitCode: " + sess.getExitStatus());
  30. sess.close(); // Close this session
  31. conn.close();
  32. }
  33. catch (IOException e)
  34. { e.printStackTrace(System.err);
  35. System.exit(2); }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement