Package ch.ethz.ssh2

Examples of ch.ethz.ssh2.Session


  public void msgChannelOpenFailure(byte[] msg, int msglen) throws IOException
  {
    if (msglen < 5)
      throw new IOException("SSH_MSG_CHANNEL_OPEN_FAILURE message has wrong size (" + msglen + ")");

    TypesReader tr = new TypesReader(msg, 0, msglen);

    tr.readByte(); // skip packet type
    int id = tr.readUINT32(); /* sender channel */

    Channel c = getChannel(id);

    if (c == null)
      throw new IOException("Unexpected SSH_MSG_CHANNEL_OPEN_FAILURE message for non-existent channel " + id);

    int reasonCode = tr.readUINT32();
    String description = tr.readString("UTF-8");

    String reasonCodeSymbolicName = null;

    switch (reasonCode)
    {
View Full Code Here


  public void msgGlobalRequest(byte[] msg, int msglen) throws IOException
  {
    /* Currently we do not support any kind of global request */

    TypesReader tr = new TypesReader(msg, 0, msglen);

    tr.readByte(); // skip packet type
    String requestName = tr.readString();
    boolean wantReply = tr.readBoolean();

    if (wantReply)
    {
      byte[] reply_failure = new byte[1];
      reply_failure[0] = Packets.SSH_MSG_REQUEST_FAILURE;
View Full Code Here

    {
      debug.println("Got REPLY.");
      debug.flush();
    }

    TypesReader tr = new TypesReader(resp);

    int t = tr.readByte();

    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
      throw new IOException("The server sent an invalid id field.");

    if (t == Packet.SSH_FXP_ATTRS)
    {
      return readAttrs(tr);
    }

    if (t != Packet.SSH_FXP_STATUS)
      throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");

    int errorCode = tr.readUINT32();

    throw new SFTPException(tr.readString(), errorCode);
  }
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

  }

  private Session startProcess(List<String> commands) throws IOException {
    String command = ToolBox.createCommandString(commands);
    SSHDataHandler dataHandler = getHandler();
    Session sess = dataHandler.createSession();
    if (sess == null)
      throw new IOException("Session corrupt");
    sess.execCommand(command);
    return sess;
  }
View Full Code Here

  public void startGromacsProgram(List<String> commands,
      OutputStream outStream, boolean interactive, OutputStream helperStream) throws IOException {
    SSHDataHandler dataHandler = getHandler();
    if (commands.get(0).equals(GromacsConstants.MDRUN)
        && remoteProject.isUseMPILib()) {
      Session mpiSess = dataHandler.createSession();
      mpiSess.execCommand(remoteProject.getMpiLib());
      outputSession(mpiSess);
      // outputStdout(mpiSess.getStdout());
      // outputStdout(mpiSess.getStderr());
      mpiSess.close();
      commands.add(0, Integer.toString(remoteProject.getNp()));
      commands.add(0, GromacsConstants.MPIRUNOPTION);
      commands.add(0, GromacsConstants.MPIRUN);
    }
    String command = ToolBox.createCommandString(commands);

    Session sess = dataHandler.createSession();
    SSHGromacsThread gromacsThread = new SSHGromacsThread(command, sess,
        outStream, interactive, helperStream);
    if (interactive)
      gromacsThread
          .setInputString(inputStringForInteractiveThread(commands
View Full Code Here

TOP

Related Classes of ch.ethz.ssh2.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.