예제 #1
0
파일: SSHTest2.java 프로젝트: lzupb/mybae
  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;
    }
  }
예제 #5
0
  public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "******";

    File keyfile = new File("~/.ssh/id_rsa"); // or "~/.ssh/id_dsa"
    String keyfilePass = "******"; // will be ignored if not needed

    try {
      /* Create a connection instance */

      Connection conn = new Connection(hostname);

      /* Now connect */

      conn.connect();

      /* Authenticate */

      boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);

      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);
    }
  }
예제 #6
0
  static void increaseCPU(OnDemandAWS pc, String keyName) throws InterruptedException {

    File keyfile = new File(keyName + ".pem"); // or "~/.ssh/id_dsa"
    String keyfilePass = "******"; // will be ignored if not needed

    try {
      Connection conn = new Connection(pc.ipAddress);
      conn.connect();

      boolean isAuthenticated = conn.authenticateWithPublicKey("ec2-user", keyfile, keyfilePass);
      if (isAuthenticated == false) throw new IOException("Authentication failed.");

      Session sess = conn.openSession();
      System.out.println("Increasing CPU usage for " + pc.machineName);
      sess.execCommand("while true; do true; done");
      sess.close();
      conn.close();

    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.out.println("Please use the attached script to start and stop cpu remotely");
    }
  }