Package net.schmizz.sshj.connection.channel.direct

Examples of net.schmizz.sshj.connection.channel.direct.Session


      if (agentProxy == null) {
        System.err.println("No agentProxy");
        System.exit(0);
      }
      ssh.auth(System.getProperty("user.name"), getAuthMethods(agentProxy));
      final Session session = ssh.startSession();
      try {
        final Command cmd = session.exec("ping -c 1 google.com");
        System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
        cmd.join(5, TimeUnit.SECONDS);
        System.out.println("\n** exit status: " + cmd.getExitStatus());
      } finally {
        session.close();
      }
    } finally {
      ssh.disconnect();
    }
  }
View Full Code Here


        final String obfuscatedCmd = origCmd.toCommandLine(os, true);
        logger.info("Starting command [{}] on [{}]", obfuscatedCmd, this);

        try {
            if (openShellBeforeExecute) {
                Session session = null;
                try {
                    logger.debug("Creating a temporary shell to allow for deferred home dir creation.");
                    session = getSshClient().startSession();
                    Session.Shell shell = session.startShell();
                    shell.close();
                } finally {
                    closeQuietly(session);
                }
            }

            Session session = getSshClient().startSession();
            if (allocatePty != null && !allocatePty.isEmpty()) {
                if (allocateDefaultPty) {
                    logger.warn("The " + ALLOCATE_PTY + " and " + ALLOCATE_DEFAULT_PTY
                            + " connection options have both been set for the connection {}. Ignoring "
                            + ALLOCATE_DEFAULT_PTY + " and using " + ALLOCATE_PTY + ".", this);
                }
                Matcher matcher = ptyPattern.matcher(allocatePty);
                checkArgument(matcher.matches(), "Value for allocatePty [%s] does not match pattern \"" + PTY_PATTERN + "\"", allocateDefaultPty);

                String term = matcher.group(1);
                int cols = Integer.valueOf(matcher.group(2));
                int rows = Integer.valueOf(matcher.group(3));
                int width = Integer.valueOf(matcher.group(4));
                int height = Integer.valueOf(matcher.group(5));
                logger.debug("Allocating PTY {}:{}:{}:{}:{}", new Object[]{term, cols, rows, width, height});
                session.allocatePTY(term, cols, rows, width, height, Collections.<PTYMode, Integer>emptyMap());
            } else if (allocateDefaultPty) {
                logger.debug("Allocating default PTY");
                session.allocateDefaultPTY();
            }
            return createProcess(session, cmd);
        } catch (SSHException e) {
            throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, this), e);
        }
View Full Code Here

    }

    @Test
    public void shouldNotAllocateDefaultPty() throws IOException {
        Session session = mock(Session.class);
        when(client.startSession()).thenReturn(session);
        connectionOptions.set(ALLOCATE_DEFAULT_PTY, false);

        SshConnection connection = newConnectionWithClient(client);
        connection.connect();
View Full Code Here

        connection.close();
    }

    @Test
    public void shouldAllocateDefaultPty() throws IOException {
        Session session = mock(Session.class);
        when(client.startSession()).thenReturn(session);
        connectionOptions.set(ALLOCATE_DEFAULT_PTY, true);

        SshConnection connection = newConnectionWithClient(client);
        connection.connect();
View Full Code Here

    }

    @SuppressWarnings("unchecked")
    @Test
    public void shouldAllocatePty() throws IOException {
        Session session = mock(Session.class);
        when(client.startSession()).thenReturn(session);
        connectionOptions.set(ALLOCATE_PTY, "xterm:132:50:264:100");

        SshConnection connection = newConnectionWithClient(client);
        connection.connect();
View Full Code Here

    }

    @SuppressWarnings("unchecked")
    @Test
    public void allocatePtyShouldOverrideAllocateDefaultPty() throws IOException {
        Session session = mock(Session.class);
        when(client.startSession()).thenReturn(session);
        connectionOptions.set(ALLOCATE_PTY, "xterm:132:50:264:100");
        connectionOptions.set(ALLOCATE_DEFAULT_PTY, true);

        SshConnection connection = newConnectionWithClient(client);
View Full Code Here

  }

  @Override
  public void execute(JobExecutionContext jobExecutionContext) throws GFacProviderException {
    ApplicationDeploymentDescriptionType app = jobExecutionContext.getApplicationContext().getApplicationDeploymentDescription().getType();
    Session session = null;
    try {
      session = securityContext.getSession(jobExecutionContext.getApplicationContext().getHostDescription().getType().getHostAddress());
      /*
       * Execute
       */
      String execuable = app.getStaticWorkingDirectory() + File.separatorChar + Constants.EXECUTABLE_NAME;
      Command cmd = session.exec("/bin/chmod 755 " + execuable + "; " + execuable);
      log.info("stdout=" + GFacUtils.readFromStream(session.getInputStream()));
      cmd.join(Constants.COMMAND_EXECUTION_TIMEOUT, TimeUnit.SECONDS);

      /*
       * check return value. usually not very helpful to draw conclusions
       * based on return values so don't bother. just provide warning in
View Full Code Here

    try {
      ssh.loadKnownHosts();
      ssh.connect(this.instance.getPublicDnsName());

      ssh.authPublickey(privateKeyFilePath);
      final Session session = ssh.startSession();
      try {
        StringBuilder command = new StringBuilder();
        command.append("mkdir -p ");
        command.append(model.getTmpDir());
        command.append(" | ");
        command.append("mkdir -p ");
        command.append(model.getWorkingDir());
        command.append(" | ");
        command.append("mkdir -p ");
        command.append(model.getInputDataDir());
        command.append(" | ");
        command.append("mkdir -p ");
        command.append(model.getOutputDataDir());
        Command cmd = session.exec(command.toString());
        cmd.join(5, TimeUnit.SECONDS);
      } catch (Exception e) {
        throw e;
      } finally {
        try {
          session.close();
        } catch (Exception e) {
        }
      }
    } catch (Exception e) {
      throw new GfacException(e.getMessage(), e);
View Full Code Here

       */
      ssh.loadKnownHosts();
      ssh.connect(model.getHost());
      ssh.authPublickey(privateKeyFilePath);

      final Session session = ssh.startSession();
      try {
        /*
         * Build working Directory
         */
        log.info("WorkingDir = " + model.getWorkingDir());     
        session.exec("mkdir -p " + model.getWorkingDir());
        session.exec("cd " + model.getWorkingDir());
       
        /*
         * Set environment
         */
        for (String key : nv.keySet()) {
          session.setEnvVar(key, nv.get(key));
        }
       
        /*
         * Execute
         */
        Command cmd = session.exec(command);
        log.info("stdout=" + GfacUtils.readFromStream(session.getInputStream()));
        cmd.join(5, TimeUnit.SECONDS);
       
       
        // notify end
        notifier.computationFinished(compObj);
       
        /*
         * check return value. usually not very helpful to draw conclusions
         * based on return values so don't bother. just provide warning in
         * the log messages
         */       
        if (cmd.getExitStatus() != 0) {
          log.error("Process finished with non zero return value. Process may have failed");
        } else {
          log.info("Process finished with return value of zero.");
        }                       
       
        File logDir = new File("./service_logs");
        if (!logDir.exists()) {
          logDir.mkdir();
        }       
       
        // Get the Stdouts and StdErrs
        QName x = QName.valueOf(invocationContext.getServiceName());
        String timeStampedServiceName = GfacUtils.createServiceDirName(x);
        File localStdOutFile = new File(logDir, timeStampedServiceName + ".stdout");
        File localStdErrFile = new File(logDir, timeStampedServiceName + ".stderr");
       
        SCPFileTransfer fileTransfer = ssh.newSCPFileTransfer();
        fileTransfer.download(model.getStdOut(), localStdOutFile.getAbsolutePath());
        fileTransfer.download(model.getStderr(), localStdErrFile.getAbsolutePath());       
       
        context.getExecutionModel().setStdoutStr(GfacUtils.readFile(localStdOutFile.getAbsolutePath()));
        context.getExecutionModel().setStderrStr(GfacUtils.readFile(localStdErrFile.getAbsolutePath()));
       
        // set to context
        OutputUtils.fillOutputFromStdout(invocationContext.getMessageContext("output"), context.getExecutionModel().getStdoutStr(), context.getExecutionModel().getStderrStr());
       
      } catch (Exception e) {
        throw e;
      } finally {
        try {
          session.close();
        } catch (Exception e) {
        }
      }
    } catch (Exception e) {
      throw new GfacException(e.getMessage(), e);
View Full Code Here

      ssh.loadKnownHosts();
      ssh.connect(model.getHost());

      // TODO how to authenticate with system
      ssh.authPublickey(System.getProperty("user.name"));
      final Session session = ssh.startSession();
      try {
        StringBuilder command = new StringBuilder();
        command.append("mkdir -p ");
        command.append(model.getTmpDir());
        command.append(" | ");
        command.append("mkdir -p ");
        command.append(model.getWorkingDir());
        command.append(" | ");
        command.append("mkdir -p ");
        command.append(model.getInputDataDir());
        command.append(" | ");
        command.append("mkdir -p ");
        command.append(model.getOutputDataDir());
        Command cmd = session.exec(command.toString());
        cmd.join(5, TimeUnit.SECONDS);
      } catch (Exception e) {
        throw e;
      } finally {
        try {
          session.close();
        } catch (Exception e) {
        }
      }
    } catch (Exception e) {
      throw new GfacException(e.getMessage(), e);
View Full Code Here

TOP

Related Classes of net.schmizz.sshj.connection.channel.direct.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.