Ejemplo n.º 1
2
 public String sendCommand(Session session, String Command) {
   StringBuilder result = new StringBuilder();
   try {
     Channel channel = session.openChannel("exec");
     ((ChannelExec) channel).setCommand(Command);
     InputStream in = channel.getInputStream();
     channel.connect();
     byte[] tmp = new byte[1024];
     boolean allow = true;
     while (allow) {
       while (in.available() > 0) {
         int i = in.read(tmp, 0, 1024);
         if (i < 0) break;
         result.append(new String(tmp, 0, i));
       }
       if (channel.isClosed()) {
         if (in.available() > 0) continue;
         System.out.println("exit-status: " + channel.getExitStatus());
         break;
       }
     }
     channel.disconnect();
     return result.toString();
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   }
 }
Ejemplo n.º 2
0
  public static void main(String[] arg) throws JSchException, InterruptedException {
    JSch jsch = new JSch();

    String[] proxyInfo = queryUserAndHost("proxy server", arg.length > 0 ? arg[0] : null);

    Session gateway = jsch.getSession(proxyInfo[0], proxyInfo[1]);

    UserInfo ui = new SwingDialogUserInfo();
    gateway.setUserInfo(ui);
    gateway.connect();

    String[] targetInfo = queryUserAndHost("target server", arg.length > 1 ? arg[1] : null);

    Session session = jsch.getSession(targetInfo[0], targetInfo[1]);

    session.setProxy(new ProxySSH(gateway));

    session.setUserInfo(ui);

    System.err.println("connecting session ...");
    session.connect();

    System.err.println("session connected.");
    System.err.println("opening shell channel ...");

    Channel channel = session.openChannel("shell");

    channel.setOutputStream(System.out, true);
    channel.setExtOutputStream(System.err, true);

    channel.setInputStream(System.in, true);

    channel.connect();

    System.err.println("shell channel connected.");

    do {
      Thread.sleep(100);
    } while (!channel.isEOF());
    System.err.println("exitcode: " + channel.getExitStatus());
    session.disconnect();
    Thread.sleep(50);

    gateway.disconnect();
  }
Ejemplo n.º 3
0
  public String runCommand(String params) {
    try {
      StringBuilder sb = new StringBuilder();
      Channel channel = ConnectionManager.getSession().openChannel("exec");
      channel.setInputStream(null);
      channel.setOutputStream(System.out);
      ((ChannelExec) channel).setCommand(params);
      ((ChannelExec) channel).setPty(false);
      channel.connect();
      InputStream in = channel.getInputStream();
      //			byte[] tmp = new byte[1024];
      while (true) {
        InputStreamReader is = new InputStreamReader(in);

        BufferedReader br = new BufferedReader(is);
        String read = br.readLine();
        while (read != null) {
          System.out.println(read);
          sb.append(read);
          read = br.readLine();
        }
        if (channel.isClosed()) {
          System.out.println(sb.toString());
          System.out.println("exit-status:" + channel.getExitStatus());
          break;
        }
        try {
          Thread.sleep(1000);
        } catch (Exception ee) {
        }
      }
      channel.disconnect();
      return sb.toString();
    } catch (Exception e) {
      e.printStackTrace();
      return "empty";
    }
  }
Ejemplo n.º 4
0
  public boolean exec(String command) {
    execResponse = "";
    if (!prepareChannel()) {
      sessionCleanup();
      return false;
    }

    // channel.setXForwarding(true);

    // channel.setInputStream(System.in);
    // channel.setOutputStream(System.out);

    // FileOutputStream fos=new FileOutputStream("/tmp/stderr");
    // ((ChannelExec)channel).setErrStream(fos);
    // ((ChannelExec) channel).setErrStream(System.err);

    if (!getStreams()) {
      sessionCleanup();
      return false;
    }

    if (command.equalsIgnoreCase(ADJUST_DATE)) {
      command = getAdjustDateCommand();
    }
    setCommand(command);

    if (!connectChannel()) {
      channelCleanup();
      sessionCleanup();
      return false;
    }
    NeptusLog.pub().info("Date to set in vehicle '" + vehicleId + "': " + command);

    try {
      byte[] tmp = new byte[1024];
      while (true) {
        while (in.available() > 0) {
          int i = in.read(tmp, 0, 1024);
          if (i < 0) break;
          String tmpStr = new String(tmp, 0, i);
          execResponse += "\n" + tmpStr;
          System.out.print(tmpStr);
        }
        if (channel.isClosed()) {
          NeptusLog.pub().info("<###>exit-status: " + channel.getExitStatus());
          exitStatus = channel.getExitStatus();
          break;
        }
        try {
          Thread.sleep(1000);
        } catch (Exception ee) {
          NeptusLog.pub().error(ee.getStackTrace());
        }
      }
    } catch (IOException e) {
      NeptusLog.pub().error(this + " :: Error reading from InputStream.", e);
      execResponse += "\n :: Error reading from InputStream. " + e.getMessage();
      channel.disconnect();
      session.disconnect();
      return false;
    }
    exitStatus = channel.getExitStatus();
    channel.disconnect();
    session.disconnect();
    return (exitStatus == 0) ? true : false;
  }