Пример #1
0
  public static void main(String[] args) {
    String hostname = "123.57.48.109";
    String username = "******";
    String password = "******";

    try {
      /* Create a connection instance */

      Connection conn = new Connection(hostname);

      /* Now connect */

      conn.connect();

      /* Authenticate */

      boolean isAuthenticated = conn.authenticateWithPassword(username, password);

      if (isAuthenticated == false) throw new IOException("Authentication failed.");

      /* Create a session */

      SCPClient scpclient = conn.createSCPClient();

      scpclient.get("/alidata/account.log", "D:\\temp");

      /* Close the connection */

      conn.close();

    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(2);
    }
  }
  @Override
  public Session connect(UserDetails userDetails, boolean verifyHostKey) throws Throwable {
    ConnectionInfo connInfo =
        ganymedConn.connect(
            verifyHostKey
                ? new ServerHostKeyVerifier() {

                  @Override
                  public boolean verifyServerHostKey(
                      String hostname,
                      int port,
                      String serverHostKeyAlgorithm,
                      byte[] serverHostKey)
                      throws Exception {
                    if (_serverDetails.hostKey() == null) {
                      // no host key is stored locally.
                      return false;
                    }
                    if (!_serverDetails.host().equals(hostname)) {
                      // host name not equal
                      return false;
                    }
                    String algorithm = _serverDetails.hostKeyAlgorithm();
                    if (algorithm == null || !algorithm.equals(serverHostKeyAlgorithm)) {
                      // host key algorithm does not match
                      return false;
                    }
                    byte[] hostKey = _serverDetails.hostKeyBytes();
                    return Arrays.equals(hostKey, serverHostKey);
                  }
                }
                : null);
    if (connInfo.serverHostKey != null && _serverDetails.hostKey() == null) {
      _serverDetails.setHostKey(connInfo.serverHostKey);
    }
    boolean authenticated = false;
    if (userDetails.password() == null && userDetails.privateKey() == null) {
      throw new IllegalArgumentException(
          "Expecting user password or private key. None is specified.");
    }
    if (userDetails.password() != null) {
      authenticated =
          ganymedConn.authenticateWithPassword(userDetails.username(), userDetails.password());
    }
    if (!authenticated && userDetails.privateKey() != null) {
      authenticated =
          ganymedConn.authenticateWithPublicKey(
              userDetails.username(),
              userDetails.privateKey().toCharArray(),
              userDetails.passphrase());
    }
    if (!authenticated) {
      throw new RuntimeException("Failed to authenticate user " + userDetails.username());
    }
    GanymedSession session = new GanymedSession(userDetails, this);
    session.open();
    return session;
  }
  public static void main(String[] args) throws IOException {
    String hostname = "somehost";
    String username = "******";
    String password = "******";

    File knownHosts = new File("~/.ssh/known_hosts");

    try {
      /* Load known_hosts file into in-memory database */

      if (knownHosts.exists()) database.addHostkeys(knownHosts);

      /* Create a connection instance */

      Connection conn = new Connection(hostname);

      /* Now connect and use the SimpleVerifier */

      conn.connect(new SimpleVerifier(database));

      /* Authenticate */

      boolean isAuthenticated = conn.authenticateWithPassword(username, password);

      if (isAuthenticated == false) throw new IOException("Authentication failed.");

      /* Create a session */

      Session sess = conn.openSession();

      sess.execCommand("uname -a && date && uptime && who");

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

      System.out.println("Here is some information about the remote host:");

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

      /* Close this session */

      sess.close();

      /* Close the connection */

      conn.close();

    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(2);
    }
  }
Пример #4
0
  public void connect() throws IOException {
    try {
      connection.connect();
      isAuthenticated = connection.authenticateWithPassword(account, password); // Authenticate
      if (!isAuthenticated) {
        throw new IOException("isAuthenticated=" + isAuthenticated);
      }

    } catch (IOException e) {
      throw e;
    }
  }