Obtaining host key fingerprint |
When connecting to an SSH server for the first time you may not know what the host key fingerprint is, and without knowing the ssh host key fingerprint you will be unable to validate the host key. The following code example demonstrates how to connect to an SSH server and obtain the host key fingerprint.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Ssh instance
Ssh ssh = new Ssh(sshParams);
// set your license key
ssh.LicenseKey = "license key data";
// establish connection
ssh.Connect();
// obtain host keys for connection
SshHostKeys hostKeys = ssh.HostKeys;
// get all hosts for SshHostKeys
IEnumerator hosts = hostKeys.GetHosts();
// iterate thru each host
while (hosts.MoveNext())
{
DictionaryEntry host = (DictionaryEntry)hosts.Current;
IPHostEntry ip = (IPHostEntry)host.Key;
// print host
Console.WriteLine("Found host {0}", ip.HostName);
// print fingerprints for host
IEnumerator fingerprints = hostKeys.GetKeys(ip);
while (fingerprints.MoveNext())
{
string fingerprint = (string)fingerprints.Current;
Console.WriteLine("Fingerprint found {0}", fingerprint);
}
}
See also