Ejemplo n.º 1
0
  protected void connectSession() throws JSchException {
    if (session != null) {
      if (session.isConnected()) {
        session.disconnect();
      }
      session = null;
    }

    AutomationLogger.getInstance().info("Connectting...");

    if (StringUtil.notEmpty(user)) {
      session = jsch.getSession(user, host, 22);
      session.setPassword(password);
    } else if (auth != null) {
      session = auth.getSession(host);
    } else {
      throw new ItemNotFoundException("Authentication is missing!");
    }

    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    AutomationLogger.getInstance().info("Connected");
  }
Ejemplo n.º 2
0
 public void connect(Auth auth, String host) {
   disconnect();
   this.user = null;
   this.password = null;
   this.host = host;
   this.auth = auth;
   AutomationLogger.getInstance().info("SSH to " + host);
   connect();
 }
Ejemplo n.º 3
0
 /**
  * Use the given user,pwd and host to connect server
  *
  * @param user
  * @param password
  * @param host
  * @throws JSchException
  */
 public void connect(String user, String password, String host) {
   disconnect();
   this.user = user;
   this.password = password;
   this.host = host;
   this.auth = null;
   AutomationLogger.getInstance().info("SSH to " + host);
   connect();
 }
Ejemplo n.º 4
0
 protected void connect() {
   connected = false;
   int count = 0;
   while (!connected) { // while try 5 times
     if (count != 0) {
       AutomationLogger.getInstance().info("Try to re-connect, time#" + count);
     }
     try {
       connectSession();
       connected = true;
     } catch (ItemNotFoundException infe) {
       throw infe;
     } catch (Exception e) {
       if (count < 5) {
         AutomationLogger.getInstance().warn("Failed to connect due to " + e);
       } else {
         throw new ActionFailedException(e);
       }
       count++;
       Timer.sleep(2000); // sleep 2 seconds before re-connect
     }
   }
 }
Ejemplo n.º 5
0
  /**
   * execute the given command in the remote host
   *
   * @param command
   * @return - command output in the remote host
   */
  public String exec(String command) {
    if (!connected) {
      throw new ActionFailedException("There is no session!");
    }
    StringBuffer data = new StringBuffer();
    OutputStream out = null;
    InputStream in = null;
    try {
      Channel channel;

      boolean channel_connected = false;
      int count = 0;
      while (!channel_connected) {

        try {
          channel = session.openChannel("exec");
          ((ChannelExec) channel).setCommand(command);

          out = channel.getOutputStream();
          in = channel.getInputStream();
          channel.connect();
          channel_connected = true;
        } catch (Exception e) {
          count++;
          String msg = e.getMessage();
          if (count < 5) {
            AutomationLogger.getInstance()
                .warn(
                    "Failed to connect to SSH server due to "
                        + msg
                        + ". Will try again in 1 second");
            if (msg.startsWith("session is down")) {
              AutomationLogger.getInstance().info("Try to re-connect session");
              connect();
            }
            Timer.sleep(1000);
          } else {
            throw new ActionFailedException("Failed to connect to SSH server due to " + e);
          }
        }
      }

      byte[] buf = new byte[1024];

      // read
      count = 0;
      while ((count = in.read(buf)) > 0) {
        data.append(new String(buf, 0, count));
      }

    } catch (Exception e) {
      AutomationLogger.getInstance().warn(e);
    } finally {
      try {
        in.close();
      } catch (Exception e) {
      }
      try {
        out.close();
      } catch (Exception e) {
      }

      if (channel != null) {
        channel.disconnect();
      }
    }
    return data.toString();
  }