Package com.trendmicro.mist.client

Examples of com.trendmicro.mist.client.MistClient$Session


        int exitCode = DEFAULT_EXIT_CODE;
        String outputString = "";
        try {
            mSessionLock.lock();
            final Session thisSession;
            try {
                thisSession = sess;
            } finally {
                mSessionLock.unlock();
            }
            if (thisSession == null) {
                return new SshOutput("", 130);
            }
            /* requestPTY mixes stdout and strerr together, but it works
            better at the moment.
            With pty, the sudo wouldn't work, because we don't want
            to enter sudo password by every command.
            (It would be exposed) */
            thisSession.requestPTY("dumb", 0, 0, 0, 0, null);
            LOG.debug2("execOneCommand: command: "
                       + host.getName()
                       + ": "
                       + host.getSudoCommand(host.getHoppedCommand(oneCommand), true));
            thisSession.execCommand("bash -c '"
                                    + Tools.escapeSingleQuotes("export LC_ALL=C;"
                                                               + host.getSudoCommand(host.getHoppedCommand(oneCommand),
                                                                                     false), 1) + '\'');
            outputString = execCommandAndCaptureOutput(oneCommand, thisSession);
            if (cancelIt) {
                return new SshOutput("", 130);
            }
            if (commandVisible) {
                host.getTerminalPanel().nextCommand();
            }
            thisSession.waitForCondition(ChannelCondition.EXIT_STATUS, 10000);
            final Integer ec = thisSession.getExitStatus();
            if (ec != null) {
                exitCode = ec;
            }
            thisSession.close();
            sess = null;
        } catch (final IOException e) {
            LOG.appWarning("execOneCommand: " + host.getName() + ':' + e.getMessage() + ':' + oneCommand);
            exitCode = ERROR_EXIT_CODE;
            cancelTheSession();
View Full Code Here


            boolean isAuthenticated = conn.authenticateWithPassword(this.user, this.password);
            if (isAuthenticated == false) {
                result.append("ERROR: Authentication Failed !");
            }

            Session session = conn.openSession();

            session.execCommand(cmd);
            BufferedReader read =
                    new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout()), "GBK"));
            String line = "";
            while ((line = read.readLine()) != null) {
                result.append(line).append("\r\n");
            }
            session.close();
            conn.close();
            return result.toString();
        }
        catch (Throwable e) {
            throw new RemoteExecuteException("ִ���������", e);
View Full Code Here

    String get_ls() throws IOException {
        if (!conn.isAuthenticationComplete())
            throw new IOException("Authentication failed");

        Session session = conn.openSession();
        session.execCommand("ls -a");
        StreamGobbler stdout = new StreamGobbler(session.getStdout());

        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        String out = br.readLine();

        session.close();

        return out;
    }
View Full Code Here

    String get_command(String command) throws IOException  {
        String response = null;
        if (!conn.isAuthenticationComplete())
            throw new IOException("Authentication failed");

        Session session = conn.openSession();
        session.execCommand(command);
        StreamGobbler stdout = new StreamGobbler(session.getStdout());

        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }

        session.close();

        return response;
    }
View Full Code Here

            public void go() {
                go = true;
            }

            public void run() {
                MistClient sinkClient = null;
                try {
                    sinkClient = new MistClient(Role.PRODUCER, 1);
                    sinkClient.mount(true, fwdExchange.toString());
                    sinkClient.attach();
                }
                catch(Exception e) {
                    e.printStackTrace();
                    return;
                }

                if(fwdExchange.getBroker() == null) {
                    ready = true;
                    try {
                        sinkClient.close();
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    return;
                }

                javax.jms.Connection fromConn = null;
                javax.jms.Session fromSess = null;
                javax.jms.MessageConsumer fromConsumer = null;
                try {
                    ConnectionFactory fromFact = new com.sun.messaging.ConnectionFactory();
                    ((com.sun.messaging.ConnectionFactory) fromFact).setProperty(com.sun.messaging.ConnectionConfiguration.imqBrokerHostName, fwdExchange.getBroker());
                    ((com.sun.messaging.ConnectionFactory) fromFact).setProperty(com.sun.messaging.ConnectionConfiguration.imqBrokerHostPort, "7676");
                    ((com.sun.messaging.ConnectionFactory) fromFact).setProperty(com.sun.messaging.ConnectionConfiguration.imqDefaultUsername, "admin");
                    ((com.sun.messaging.ConnectionFactory) fromFact).setProperty(com.sun.messaging.ConnectionConfiguration.imqDefaultPassword, "admin");
                    fromConn = fromFact.createConnection();
                    fromConn.start();

                    fromSess = fromConn.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
                    javax.jms.Destination fromDest;
                    if(fwdExchange.isQueue())
                        fromDest = fromSess.createQueue(fwdExchange.getName());
                    else
                        fromDest = fromSess.createTopic(fwdExchange.getName());
                    fromConsumer = fromSess.createConsumer(fromDest);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                ready = true;

                for(;;) {
                    if(!go) {
                        Utils.justSleep(100);
                    }
                    else
                        break;
                }

                while(!done) {
                    try {
                        javax.jms.Message msg = fromConsumer.receive(50);
                        if(msg != null) {
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            try {
                                if(msg instanceof BytesMessage) {
                                    byte[] buffer = new byte[256];
                                    int ret = -1;
                                    while((ret = ((BytesMessage) msg).readBytes(buffer)) > 0)
                                        bos.write(buffer, 0, ret);
                                }
                                else if(msg instanceof TextMessage) {
                                    byte[] buffer = ((TextMessage) msg).getText().getBytes("UTF-8");
                                    bos.write(buffer, 0, buffer.length);
                                }
                            }
                            catch(Exception e) {
                                e.printStackTrace();
                                continue;
                            }

                            byte[] buffer = bos.toByteArray();
                            MistMessage.MessageBlock.Builder msgBuilder = MistMessage.MessageBlock.newBuilder().setId(fwdExchange.toString()).setMessage(ByteString.copyFrom(buffer));
                            Enumeration<?> propNames = msg.getPropertyNames();
                            while(propNames.hasMoreElements()) {
                                String key = (String) propNames.nextElement();
                                String value = msg.getStringProperty(key);
                                if(key.equals("MIST_TTL"))
                                    msgBuilder.setTtl(Long.valueOf(value));
                                else
                                    msgBuilder.addProperties(KeyValuePair.newBuilder().setKey(key).setValue(value).build());
                            }
                            sinkClient.writeMessage(msgBuilder.build());
                            totalForwardedCount++;
                        }
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                }

                try {
                    fromConsumer.close();
                    fromSess.close();
                    fromConn.close();

                    sinkClient.close();
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }
View Full Code Here

   * \return boolean
   *
   * @return
   */
  public boolean remoteIsWindowsShell() {
    Session objSSHSession = null;
    flgIsRemoteOSWindows = false;

    try {
      // TODO the testcommand should be defined by an option
      String checkShellCommand = "echo %ComSpec%";
      logger.debug("Opening new session...");
      objSSHSession = this.getSshConnection().openSession();
      logger.debug("Executing command " + checkShellCommand);
      objSSHSession.execCommand(checkShellCommand);

      logger.debug("output to stdout for remote command: " + checkShellCommand);
      ipsStdOut = new StreamGobbler(objSSHSession.getStdout());
      ipsStdErr = new StreamGobbler(objSSHSession.getStderr());
      BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(ipsStdOut));
      String stdOut = "";
      while (true) {
        String line = stdoutReader.readLine();
        if (line == null)
          break;
        logger.debug(line);
        stdOut += line;
      }
      logger.debug("output to stderr for remote command: " + checkShellCommand);
      BufferedReader stderrReader = new BufferedReader(new InputStreamReader(ipsStdErr));
      while (true) {
        String line = stderrReader.readLine();
        if (line == null)
          break;
        logger.debug(line);
      }
      // TODO The expected result-string for testing the os should be defined by an option
      if (stdOut.indexOf("cmd.exe") > -1) {
        logger.debug("Remote shell is a Windows shell.");
        flgIsRemoteOSWindows = true;
        return true;
      }
    }
    catch (Exception e) {
      logger.debug("Failed to check if remote system is windows shell: " + e);
    }
    finally {
      if (objSSHSession != null)
        try {
          objSSHSession.close();
        }
        catch (Exception e) {
          logger.debug("Failed to close session: ", e);
        }
    }
View Full Code Here

      throw new Exception("Failed to kill children of pid "+pel.pid+": "+e,e);
    }
  }

  private void executeCommand(String command, int logLevel) throws Exception{
    Session session = getSshConnection().openSession();
    try{
      session.execCommand(command);
      spooler_log.log(logLevel,"output to stdout for remote command: " + command);
      stdout = new StreamGobbler(this.getSshSession().getStdout());
      stderr = new StreamGobbler(this.getSshSession().getStderr());
      BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));

      while (true) {
        String line = stdoutReader.readLine();
        if (line == null) break;
        spooler_log.log(logLevel, line);
      }


      spooler_log.log(logLevel, "output to stderr for remote command: " + command);

      BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
      stderrOutput = new StringBuffer();
      while (true) {
        String line = stderrReader.readLine();
        if (line == null) break;
        spooler_log.log(logLevel,line);
        stderrOutput.append( line + "\n");
      }
    }catch(Exception e){
      throw new Exception ("Error executing command \""+command+"\": "+e,e);
    }finally{
      if (session !=null) session.close();
    }   
  }
View Full Code Here

      throw new Exception("error occurred processing parameters: " + e.getMessage());
    }
  }

  protected boolean remoteIsWindowsShell() {
    Session session = null;
    try {
      String checkShellCommand = "echo %ComSpec%";
      getLogger().debug9("Opening ssh session...");
      session = this.getSshConnection().openSession();
      getLogger().debug9("Executing command " + checkShellCommand);
      session.execCommand(checkShellCommand);

      getLogger().debug9("output to stdout for remote command: " + checkShellCommand);
      stdout = new StreamGobbler(session.getStdout());
      stderr = new StreamGobbler(session.getStderr());
      BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
      String stdOut = "";
      while (true) {
        String line = stdoutReader.readLine();
        if (line == null)
          break;
        getLogger().debug9(line);
        stdOut += line;
      }
      getLogger().debug9("output to stderr for remote command: " + checkShellCommand);
      // Beide StreamGobbler m�ssen hintereinander instanziiert werden
      // InputStream stderr = new StreamGobbler(this.getSshSession().getStderr());
      BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
      while (true) {
        String line = stderrReader.readLine();
        if (line == null)
          break;
        getLogger().debug1(line);
      }
      if (stdOut.indexOf("cmd.exe") > -1) {
        getLogger().debug3("Remote shell is Windows shell.");
        return true;
      }
    }
    catch (Exception e) {
      try {
        getLogger().warn("Failed to check if remote system is windows shell: " + e);
      }
      catch (Exception es) {
        System.out.println(" Failed to check if remote system is windows shell: " + e);
      }
    }
    finally {
      if (session != null)
        try {
          session.close();
        }
        catch (Exception e) {
          try {
            getLogger().warn("Failed to close session: " + e);
          }
View Full Code Here

   * @see jSimMacs.gromacsrun.IGromacsRun#make_ndx(java.util.List,
   *      java.util.List, java.lang.String)
   */
  public List<String> make_ndx(List<String> commands,
      List<String> ndxCommands, String groupName) throws IOException {
    Session sess = null;
    BufferedReader commandResult = null;
    try {
      sess = startProcess(commands);

      OutputStuff ndxOutput = new OutputStuff();
      ndxOutput.output(sess.getStdin(), ndxCommands);

      InputStream stdout = new StreamGobbler(sess.getStdout());
      commandResult = new BufferedReader(new InputStreamReader(stdout));

      ndxCommands = GromacsCommandBuilder.getInstance()
          .createRenameGroupNdxCommand(commandResult, groupName);

    } catch (IOException e1) {
      throw e1;
    } finally {
      if (commandResult != null)
        commandResult.close();
      if (sess != null)
        sess.close();
    }
    return ndxCommands;
  }
View Full Code Here

   * @see jSimMacs.gromacsrun.IGromacsRun#renameNdxGroup(java.util.List,
   *      java.util.List)
   */
  public void renameNdxGroup(List<String> commands, List<String> ndxCommands)
      throws IOException {
    Session sess = null;
    try {

      sess = startProcess(commands);

      OutputStuff ndxOutput = new OutputStuff();
      ndxOutput.output(sess.getStdin(), ndxCommands);

      outputStdout(sess.getStdout());
    } catch (IOException e) {
      throw e;
    } finally {
      if (sess != null)
        sess.close();
    }

    DataHandler handler = getHandler();
    handler.delete(commands.get(4));
  }
View Full Code Here

TOP

Related Classes of com.trendmicro.mist.client.MistClient$Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.