Java SSH Linux 取得fingerprint

  
//使用第三方類庫
//https://code.google.com/p/ganymed-ssh-2/

import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.ConnectionInfo;
import ch.ethz.ssh2.KnownHosts;
import ch.ethz.ssh2.ServerHostKeyVerifier;

public class SSHTest {
	public static void main(String[] args) {
		String sshServerAddr = "172.16.210.230";
		Connection sshConnection = new Connection(sshServerAddr, 22);
		try {
			sshConnection.connect(new ServerHostKeyVerifier() {
				@Override
				public boolean verifyServerHostKey(String hostname, int port,
						String serverHostKeyAlgorithm, byte[] serverHostKey)
						throws Exception {
					return true;
				}
			});
			ConnectionInfo info = sshConnection.getConnectionInfo();
			String keyAlgorithm = info.serverHostKeyAlgorithm;
			byte[] hostKeys = sshConnection.getConnectionInfo().serverHostKey;
			String finperprint = KnownHosts.createHexFingerprint(keyAlgorithm,hostKeys);
			System.out.println("The host fingerprint is  " + finperprint);
		} catch (IOException e) {
		} finally {
			sshConnection.close();
		}
	}
}

The host fingerprint is  ee:a7:4a:4a:85:a2:34:55:8e:d5:06:44:50:36:37:a2

你可能感兴趣的:(java,ssh,Fingerprint)