Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. public class Main {
  2.  
  3. static Scanner reader = new Scanner(System.in);
  4.  
  5.  
  6. public static void main(String[] args) throws IOException, InterruptedException{
  7.  
  8.  
  9. String hostname; // = "192.168.1.241";
  10. int port; // = 22
  11. String username; // = "root";
  12. String password;
  13. String cmd; // = "echo 'Hello World'";
  14.  
  15. //ask user for hostname/IP
  16. System.out.print("Enter hostname or IP: ");
  17. hostname = reader.nextLine();
  18.  
  19.  
  20. //ask user for port #
  21. System.out.print("Enter port number: ");
  22. port = reader.nextInt();
  23.  
  24. //create connection
  25. Connection connect = new Connection(hostname, port);
  26.  
  27. //ask user for username
  28. System.out.println("Enter Username (best to use 'root'): ");
  29. username = reader.nextLine();
  30.  
  31. //ask user for password
  32. System.out.println("Enter Password: ");
  33. password = reader.nextLine();
  34.  
  35.  
  36. System.out.println("Attempting to log in...");
  37.  
  38. reader.close();
  39.  
  40. //establish connection
  41. connect.connect();
  42.  
  43. //login using password
  44. connect.authenticateWithPassword(username, password);
  45.  
  46. Thread.sleep(1000);
  47. if (Thread.interrupted()) // Clears interrupted status!
  48. throw new InterruptedException();
  49.  
  50. Boolean didConnect = connect.isAuthenticationComplete();
  51. if (didConnect==false){
  52. throw new IOException("Authentication Unsucessful nCheck hostname and port number, then try again.");
  53. }
  54.  
  55. //open session
  56. Session session = connect.openSession();
  57. System.out.println("Opened session!");
  58.  
  59.  
  60.  
  61.  
  62. System.out.println("Enter a command to execute: ");
  63. cmd = reader.nextLine();
  64.  
  65. //execute command
  66. session.execCommand(cmd);
  67.  
  68.  
  69. //get output
  70. InputStream stdout = new StreamGobbler(session.getStdout());
  71.  
  72. BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
  73.  
  74. String line = br.readLine();
  75. if (line!=null){
  76. System.out.println("Command sent succesfully!" + "nOutput: " + "n" + "n" + line);
  77. }else{
  78. System.out.println("Command sent succesfully!" + "nNo Output");
  79. }
  80.  
  81. //close buffered reader
  82. br.close();
  83.  
  84. //close session
  85. session.close();
  86.  
  87. //close connection
  88. connect.close();
  89.  
  90.  
  91. }
  92.  
  93.  
  94.  
  95. }
  96.  
  97. //ask user for username
  98. System.out.println("Enter Username (best to use 'root'): ");
  99. username = reader.nextLine();
  100.  
  101. //ask user for password
  102. System.out.println("Enter Password: ");
  103. password = reader.nextLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement