Package com.sshtools.j2ssh

Examples of com.sshtools.j2ssh.SshClient


      BufferedReader reader =
          new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Connect to host? ");
      String hostname = reader.readLine();
      // Make a client connection
      SshClient ssh = new SshClient();
      // Connect to the hos
      ssh.connect(hostname);
      // Create a password authentication instance
      PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
      // Get the users name
      System.out.print("Username? ");
      String username = reader.readLine();
      pwd.setUsername(username);
      // Get the password
      System.out.print("Password? ");
      String password = reader.readLine();
      pwd.setPassword(password);
      // Try the authentication
      int result = ssh.authenticate(pwd);
      // Evaluate the result
      if (result == AuthenticationProtocolState.COMPLETE) {
        ForwardingClient forwarding = ssh.getForwardingClient();
        forwarding.addLocalForwarding("Test Local", "0.0.0.0", 8081,
                                      "127.0.0.1", 80);
        forwarding.startLocalForwarding("Test Local");
        forwarding.addRemoteForwarding("Test Remote", "0.0.0.0", 8081,
                                       "127.0.0.1", 8080);
        forwarding.startRemoteForwarding("Test Remote");
      }
      ssh.getConnectionState().waitForState(TransportProtocolState.DISCONNECTED);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here


      ConfigurationLoader.initialize(false);
      System.out.print("Connect to host? ");
      System.out.print("Connect to host? ");
      String hostname = reader.readLine();
      // Make a client connection
      SshClient ssh = new SshClient();
      SshConnectionProperties properties = new SshConnectionProperties();
      properties.setHost(hostname);
      // Connect to the host
      ssh.connect(properties);
      // Create a password authentication instance
      KBIAuthenticationClient kbi = new KBIAuthenticationClient();
      // Get the users name
      System.out.print("Username? ");
      // Read the password
      String username = reader.readLine();
      kbi.setUsername(username);
      kbi.setKBIRequestHandler(new KBIRequestHandler() {
        public void showPrompts(String name, String instructions,
                                KBIPrompt[] prompts) {
          System.out.println(name);
          System.out.println(instructions);
          String response;
          if (prompts != null) {
            for (int i = 0; i < prompts.length; i++) {
              System.out.print(prompts[i].getPrompt() + ": ");
              try {
                response = reader.readLine();
                prompts[i].setResponse(response);
              }
              catch (IOException ex) {
                prompts[i].setResponse("");
                ex.printStackTrace();
              }
            }
          }
        }
      });
      // Try the authentication
      int result = ssh.authenticate(kbi);
      // Evaluate the result
      if (result == AuthenticationProtocolState.COMPLETE) {
        // The connection is authenticated we can now do some real work!
        SessionChannelClient session = ssh.openSessionChannel();
        if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, ""))
          System.out.println("Failed to allocate a pseudo terminal");
        if(session.startShell()) {
          IOStreamConnector input =
              new IOStreamConnector(System.in, session.getOutputStream());
          IOStreamConnector output =
              new IOStreamConnector(session.getInputStream(), System.out);
          output.getState().waitForState(IOStreamConnectorState.CLOSED);
        }else
          System.out.println("Failed to start the users shell");
        ssh.disconnect();
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
View Full Code Here

* @param newProfile
*/
    public void connect(final SshToolsConnectionProfile profile,
        final boolean newProfile) {
        // We need to connect
        ssh = new SshClient();

        // Set the current connection properties
        setCurrentConnectionProfile(profile);

        // We'll do the threading rather than j2ssh as we want to get errors
View Full Code Here

      BufferedReader reader =
          new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Connect to host? ");
      String hostname = reader.readLine();
      // Make a client connection
      SshClient ssh = new SshClient();
      ssh.setSocketTimeout(30000);
      SshConnectionProperties properties = new SshConnectionProperties();
      properties.setHost(hostname);
      properties.setPrefPublicKey("ssh-dss");
      // Connect to the host
      ssh.connect(properties);
      // Create a password authentication instance
      PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
      // Get the users name
      System.out.print("Username? ");
      // Read the password
      String username = reader.readLine();
      pwd.setUsername(username);
      // Get the password
      System.out.print("Password? ");
      String password = reader.readLine();
      pwd.setPassword(password);
      // Try the authentication
      int result = ssh.authenticate(pwd);
      // Evaluate the result
      if (result == AuthenticationProtocolState.COMPLETE) {
        // The connection is authenticated we can now do some real work!
        SessionChannelClient session = ssh.openSessionChannel();
        if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, ""))
          System.out.println("Failed to allocate a pseudo terminal");
        if (session.startShell()) {
          IOStreamConnector input =
              new IOStreamConnector();
          IOStreamConnector output =
              new IOStreamConnector();
          IOStreamConnector error =
              new IOStreamConnector();
          output.setCloseOutput(false);
          input.setCloseInput(false);
          error.setCloseOutput(false);
          input.connect(System.in, session.getOutputStream());
          output.connect(session.getInputStream(), System.out);
          error.connect(session.getStderrInputStream(), System.out);
          session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
        }else
          System.out.println("Failed to start the users shell");
        ssh.disconnect();
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
View Full Code Here

      BufferedReader reader =
          new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Connect to host? ");
      String hostname = reader.readLine();
      // Make a client connection
      SshClient ssh = new SshClient();
      // Connect to the host
      ssh.connect(hostname);
      // Create a password authentication instance
      PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
      // Get the users name
      System.out.print("Username? ");
      String username = reader.readLine();
      pwd.setUsername(username);
      // Get the password
      System.out.print("Password? ");
      String password = reader.readLine();
      pwd.setPassword(password);
      // Try the authentication
      int result = ssh.authenticate(pwd);
      // Evaluate the result
      if (result == AuthenticationProtocolState.COMPLETE) {
        // The connection is authenticated we can now do some real work!
        SftpClient sftp = ssh.openSftpClient();
        // Make a directory
        try {
          sftp.mkdir("j2ssh");
        }
        catch (IOException ex) {
        }
        // Change directory
        sftp.cd("j2ssh");
        System.out.println(sftp.pwd());
        // Change the mode
        sftp.chmod(0777, "j2ssh");
        sftp.lcd("c:/");
        // Upload a file
        sftp.put("system.gif");
        // Change the local directory
        sftp.lcd("localdir");
        // Download a file
        sftp.get("somefile.txt", "anotherfile.txt");
        // Remove a directory or file
        sftp.rm("j2ssh");
        // Quit
        sftp.quit();
        ssh.disconnect();
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
View Full Code Here

  }

  public boolean connect(){
    // Make a client connection
    //System.out.println("Con1"); 
      ssh = new SshClient();
    //System.out.println("Con2"); 
      try
          // Connect to the host
      if(portSSH==0)
        ssh.connect(server,new IgnoreHostKeyVerification());
View Full Code Here

  }

  public boolean restart(){
    // Make a client connection
    //System.out.println("Con1"); 
      ssh = new SshClient();
    //System.out.println("Con2"); 
      try
          // Connect to the host
      if(portSSH==0)
        ssh.connect(server_,new IgnoreHostKeyVerification());
View Full Code Here

  SessionChannelClient session = null;

  public MySSHClient(String hostName,int port, String userName, String passwd ){
    try{
      // Make a client connection
      ssh = new SshClient();
      properties = new SshConnectionProperties();
      properties.setHost(hostName);
     
      if(port != 0)
        properties.setPort(port);
View Full Code Here

        airavataAPI.getProvenanceManager().updateApplicationJobData(jobId, shellCmd);
      } catch (AiravataAPIInvocationException e) {
        log.error("Error in saving EC2 shell command!!!", e);
      }
        }
        SshClient sshClient = new SshClient();
        sshClient.setSocketTimeout(SOCKET_TIMEOUT);
        SshConnectionProperties properties = new SshConnectionProperties();
        properties.setHost(this.instance.getPublicDnsName());
        properties.setPort(SSH_PORT);

        // Connect to the host
        try
        {
            String outParamName;
            OutputParameterType[] outputParametersArray = jobExecutionContext.getApplicationContext().
                    getServiceDescription().getType().getOutputParametersArray();
            if(outputParametersArray != null) {
                outParamName = outputParametersArray[0].getParameterName();
            } else {
                throw new GFacProviderException("Output parameter name is not set. Therefore, not being able " +
                        "to filter the job result from standard out ");
            }

            sshClient.connect(properties, new HostKeyVerification() {
                public boolean verifyHost(String s, SshPublicKey sshPublicKey) throws TransportProtocolException {
                    log.debug("Verifying Host: " + s);
                    return true;
                }
            });
            // Initialize the authentication data.
            PublicKeyAuthenticationClient publicKeyAuth = new PublicKeyAuthenticationClient();
            publicKeyAuth.setUsername(amazonSecurityContext.getUserName());
            SshPrivateKeyFile file = SshPrivateKeyFile.
                    parse(new File(System.getProperty("user.home") + "/.ssh/" + KEY_PAIR_NAME));
            SshPrivateKey privateKey = file.toPrivateKey("");
            publicKeyAuth.setKey(privateKey);

            // Authenticate
            int result = sshClient.authenticate(publicKeyAuth);
            if(result== AuthenticationProtocolState.FAILED) {
              GFacUtils.saveJobStatus(jobExecutionContext, details, JobState.FAILED);
                throw new GFacProviderException("The authentication failed");
            } else if(result==AuthenticationProtocolState.PARTIAL) {
                throw new GFacProviderException("The authentication succeeded but another"
                        + "authentication is required");
            } else if(result==AuthenticationProtocolState.COMPLETE) {
                log.info("ssh client authentication is complete...");
            }
            GFacUtils.saveJobStatus(jobExecutionContext, details, JobState.SUBMITTED);
            SessionChannelClient session = sshClient.openSessionChannel();
            log.info("ssh session successfully opened...");
            session.requestPseudoTerminal("vt100", 80, 25, 0, 0, "");
            session.startShell();
            GFacUtils.saveJobStatus(jobExecutionContext, details, JobState.ACTIVE);
             
View Full Code Here

      try{
        ConfigurationLoader.initialize(false);
      }catch(IOException e){
        setTask(TASK_ERROR,new String[] {m_errorPrefix+"configuration: "+e.getMessage() });
      }
      m_ssh = new SshClient();

      try{
        m_ssh.setSocketTimeout(m_details.getTimeout()*1000);
        m_ssh.connect(
            m_details.getServer(),
View Full Code Here

TOP

Related Classes of com.sshtools.j2ssh.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.