Guest User

Untitled

a guest
Jul 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /**
  2. * Constante que representa un enter.
  3. */
  4. private static final String ENTER_KEY = " ";
  5. /**
  6. * Sesión SSH establecida.
  7. */
  8. private Session session;
  9.  
  10. private static final String USERNAME = "user";
  11. private static final String HOST = "10.00.00";
  12. private static final int PORT = 22;
  13. private static final String PASSWORD = "pass";
  14.  
  15.  
  16. public void connect(String username, String password, String host, int port)
  17. throws JSchException, IllegalAccessException {
  18. if (this.session == null || !this.session.isConnected()) {
  19. JSch jsch = new JSch();
  20.  
  21. this.session = jsch.getSession(username, host, port);
  22. this.session.setPassword(password);
  23.  
  24. // Parametro para no validar key de conexion.
  25. this.session.setConfig("StrictHostKeyChecking", "no");
  26.  
  27. this.session.connect();
  28. } else {
  29. throw new IllegalAccessException("Sesion SSH ya iniciada.");
  30. }
  31. }
  32.  
  33. public final String executeCommand(String command)
  34. throws IllegalAccessException, JSchException, IOException {
  35. if (this.session != null && this.session.isConnected()) {
  36.  
  37. // Abrimos un canal SSH. Es como abrir una consola.
  38. ChannelExec channelExec = (ChannelExec) this.session.
  39. openChannel("exec");
  40.  
  41. InputStream in = channelExec.getInputStream();
  42.  
  43. // Ejecutamos el comando.
  44. channelExec.setCommand(command);
  45. channelExec.connect();
  46.  
  47. // Obtenemos el texto impreso en la consola.
  48. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  49. StringBuilder builder = new StringBuilder();
  50. String linea;
  51.  
  52. while ((linea = reader.readLine()) != null) {
  53. builder.append(linea);
  54. builder.append(ENTER_KEY);
  55. }
  56.  
  57. // Cerramos el canal SSH.
  58. channelExec.disconnect();
  59.  
  60. // Retornamos el texto impreso en la consola.
  61. return builder.toString();
  62. } else {
  63. throw new IllegalAccessException("No existe sesion SSH iniciada.");
  64. }
  65. }
  66. public static void main(String[] args) {
  67.  
  68. try {
  69. SSHConnector sshConnector = new SSHConnector();
  70. sshConnector.connect(USERNAME, PASSWORD, HOST, PORT);
  71. **String result = sshConnector.executeCommand("cd home/recauser/procesos/orquestador/bin;./startOrquestador.sh start");**
  72.  
  73. sshConnector.disconnect();
  74.  
  75. System.out.println(result);
  76.  
  77. } catch (JSchException ex) {
  78. ex.printStackTrace();
  79.  
  80. System.out.println(ex.getMessage());
  81. } catch (IllegalAccessException ex) {
  82. ex.printStackTrace();
  83.  
  84. System.out.println(ex.getMessage());
  85. }
  86. catch (IOException ex) {
  87. ex.printStackTrace();
  88.  
  89. System.out.println(ex.getMessage());
  90. }
  91. }
  92. /**
  93. * Cierra la sesión SSH.
  94. */
  95. public final void disconnect() {
  96. this.session.disconnect();
  97. }
Add Comment
Please, Sign In to add comment