Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. public void requestX11Forwarding(String hostname, int port, byte[] cookie, boolean singleConnection)
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import ch.ethz.ssh2.Connection;
  9. import ch.ethz.ssh2.Session;
  10. import ch.ethz.ssh2.StreamGobbler;
  11.  
  12. public class Basic extends Thread
  13. {
  14. static Connection conn;
  15. static Session sess;
  16. public static void main(String[] args)
  17. {
  18. String hostname = "192.168.XXX.XX";
  19. String username = "XXX";
  20. String password = "XXX";
  21.  
  22. try
  23. {
  24.  
  25. conn= new Connection(hostname);
  26. conn.connect();
  27. boolean isAuthenticated = conn.authenticateWithPassword(username, password);
  28.  
  29. if (isAuthenticated == false)
  30. throw new IOException("Authentication failed.");
  31.  
  32. /* Create a session */
  33.  
  34. sess= conn.openSession();
  35. sess.requestX11Forwarding("192.168.XXX.XX",6012,null,true);
  36. System.out.println("Here is some information about the remote host:");
  37.  
  38. sess.execCommand("firefox");
  39. /*
  40. * This basic example does not handle stderr, which is sometimes dangerous
  41. * (please read the FAQ).
  42. */
  43.  
  44. InputStream stdout = new StreamGobbler(sess.getStdout());
  45.  
  46. BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
  47.  
  48. while (true)
  49. {
  50. String line = br.readLine();
  51. if (line == null)
  52. break;
  53. System.out.println(line);
  54. }
  55.  
  56. /* Show exit status, if available (otherwise "null") */
  57.  
  58. System.out.println("ExitCode: " + sess.getExitStatus());
  59.  
  60. /* Close this session */
  61.  
  62. sess.close();
  63.  
  64. /* Close the connection */
  65.  
  66. conn.close();
  67.  
  68. }
  69. catch (IOException e)
  70. {
  71. e.printStackTrace(System.err);
  72. System.exit(2);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement