Package net.schmizz.sshj

Examples of net.schmizz.sshj.SSHClient


  @Override
  public void initialize(InvocationContext invocationContext) throws GfacException {
    ExecutionContext appExecContext = invocationContext.getExecutionContext();
    ExecutionModel model = appExecContext.getExecutionModel();

    SSHClient ssh = new SSHClient();
    try {
      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);
    } finally {
      try {
        ssh.disconnect();
      } catch (Exception e) {
      }
    }
  }
View Full Code Here


    ExecutionContext context = invocationContext.getExecutionContext();
    ExecutionModel model = context.getExecutionModel();

    List<String> cmdList = new ArrayList<String>();

    SSHClient ssh = new SSHClient();
    try {

      /*
       * Notifier
       */
      NotificationService notifier = context.getNotificationService();

      /*
       * Builder Command
       */
      cmdList.add(context.getExecutionModel().getExecutable());
      cmdList.addAll(context.getExecutionModel().getInputParameters());

      // create process builder from command
      String command = buildCommand(cmdList);
     
      //redirect StdOut and StdErr
      command += SPACE + "1>" + SPACE + model.getStdOut();
      command += SPACE + "2>" + SPACE + model.getStderr();     

      // get the env of the host and the application
      Map<String, String> nv = context.getExecutionModel().getEnv();     

      // extra env's
      nv.put(GFacConstants.INPUT_DATA_DIR, context.getExecutionModel().getInputDataDir());
      nv.put(GFacConstants.OUTPUT_DATA_DIR, context.getExecutionModel().getOutputDataDir());
     
      // log info
      log.info("Command = " + buildCommand(cmdList));     
      for (String key : nv.keySet()) {
        log.info("Env[" + key + "] = " + nv.get(key));
      }

      // notify start
      DurationObj compObj = notifier.computationStarted();

      /*
       * Create ssh connection
       */
      ssh.loadKnownHosts();
      ssh.connect(model.getHost());

      // TODO how to authenticate with system
      ssh.authPublickey(System.getProperty("user.name"));

      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);
    } finally {
      try {
        ssh.disconnect();
      } catch (Exception e) {
      }
    }
  }
View Full Code Here

    private SSHSecurityContextImpl sshContext;
    private String command;
    private SSHClient ssh;

    public SSHProvider() {
        ssh = new SSHClient();
    }
View Full Code Here

  }

  public Session getSession(String hostAddress) throws IOException {
    try {
      if (sshClient == null) {
        sshClient = new SSHClient();
      }
      if (getSSHClient().isConnected())
        return getSSHClient().startSession();

      KeyProvider pkey = getSSHClient().loadKeys(getPrivateKeyLoc(), getKeyPass());
View Full Code Here

    }
  }

  public SSHClient getSSHClient() {
    if (sshClient == null) {
      sshClient = new SSHClient();
    }
    return sshClient;
  }
View Full Code Here

    private SSHSecurityContextImpl sshContext;
    private String command;
    private SSHClient ssh;

    public SSHProvider() {
        ssh = new SSHClient();
    }
View Full Code Here

  }

  public Session getSession(String hostAddress) throws IOException {
    try {
      if (sshClient == null) {
        sshClient = new SSHClient();
      }
      if (getSSHClient().isConnected())
        return getSSHClient().startSession();

      KeyProvider pkey = getSSHClient().loadKeys(getPrivateKeyLoc(), getKeyPass());
View Full Code Here

    }
  }

  public SSHClient getSSHClient() {
    if (sshClient == null) {
      sshClient = new SSHClient();
    }
    return sshClient;
  }
View Full Code Here

    private SSHSecurityContextImpl sshContext;
    private String command;
    private SSHClient ssh;

    public SSHProvider() {
        ssh = new SSHClient();
    }
View Full Code Here

    private SSHSecurityContextImpl sshContext;
    private String command;
    private SSHClient ssh;

    public SSHProvider() {
        ssh = new SSHClient();
    }
View Full Code Here

TOP

Related Classes of net.schmizz.sshj.SSHClient

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.