Java Code Examples for net.schmizz.sshj.SSHClient#loadKnownHosts()

The following examples show how to use net.schmizz.sshj.SSHClient#loadKnownHosts() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SshClientServiceImpl.java    From cymbal with Apache License 2.0 5 votes vote down vote up
private SSHClient getSshCLient(final SSHInfo sshInfo) throws IOException {
    SSHClient ssh = new SSHClient(defaultConfig);

    // 登陆验证器
    ssh.addHostKeyVerifier(new PromiscuousVerifier());

    // ?
    ssh.loadKnownHosts();

    // do connect
    ssh.connect(sshInfo.getHostName());

    // 加载免密登陆私钥
    KeyProvider keyProvider = ssh.loadKeys(keyPath);
    ssh.authPublickey(sshInfo.getUser(), keyProvider);

    return ssh;
}
 
Example 2
Source File: SSHClientRebuilder.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public SSHClient build() throws IOException {
  SSHClient sshClient = new SSHClient(config);
  if (knownHosts != null) {
    sshClient.loadKnownHosts(knownHosts);
  } else {
    sshClient.addHostKeyVerifier(new PromiscuousVerifier());
  }
  KeyProvider keyProvider = null;
  if (password == null) {
    if (keyLocation != null) {
      if (keyPassphrase != null) {
        keyProvider = sshClient.loadKeys(keyLocation, keyPassphrase);
      } else {
        keyProvider = sshClient.loadKeys(keyLocation);
      }
    } else {
      if (keyPassphrase != null) {
        keyProvider = sshClient.loadKeys(keyPlainText, null, PasswordUtils.createOneOff(keyPassphrase.toCharArray()));
      } else {
        keyProvider = sshClient.loadKeys(keyPlainText, null, null);
      }
    }
  }
  sshClient.connect(hostname, port);
  if (password == null) {
    sshClient.authPublickey(username, keyProvider);
  } else {
    sshClient.authPassword(username, password);
  }
  sshClient.setTimeout(socketTimeout * 1000);
  sshClient.setConnectTimeout(connectionTimeout * 1000);
  return sshClient;
}
 
Example 3
Source File: Main.java    From aedict with GNU General Public License v3.0 5 votes vote down vote up
private void upload() throws Exception {
    System.out.println("Uploading");
    final SSHClient ssh = new SSHClient();
    ssh.loadKnownHosts();
    String password = config.password;
    if (password == null) {
        System.out.println("Enter password");
        final Scanner s = new Scanner(System.in);
        password = s.nextLine();
        if (MiscUtils.isBlank(password)) {
            throw new RuntimeException("Invalid password: blank");
        }
    }
    System.out.println("Connecting");
    ssh.connect("rt.sk");
    try {
        System.out.println("Authenticating");
        ssh.authPassword("moto", password);
        System.out.println("Uploading version");
        final String targetFName = REMOTE_DIR + "/" + config.getTargetFileName();
        exec(ssh, "echo `date +%Y%m%d` >" + REMOTE_DIR + "/" + config.getTargetFileName() + ".version");
        exec(ssh, "rm -f " + targetFName);
        System.out.println("Uploading");
        final SCPFileTransfer ft = ssh.newSCPFileTransfer();
        ft.upload(config.getTargetFileName(), targetFName);
    } finally {
        ssh.disconnect();
    }
}