Method ScanHostKeysAsync
- Namespace
- SshSharp
- Assembly
- SshSharp.dll
ScanHostKeysAsync(string, int, SshHostKeyScanOptions?, CancellationToken)
Fetches the server's host keys without authenticating, as ssh-keyscan does: one key exchange per key type the server offers,
each disconnected before user authentication. Each key is one the server proved it holds (it signed the exchange), but nothing
says the server is the one you meant to reach: compare the fingerprints out of band before trusting them, or use host certificates.
public static Task<IReadOnlyList<SshHostKey>> ScanHostKeysAsync(string host, int port = 22, SshHostKeyScanOptions? options = null, CancellationToken cancellationToken = default)
Parameters
hoststringThe server's host name or address.
portintThe server's port.
optionsSshHostKeyScanOptionsWhich key types to ask for; by default every plain key type SshSharp supports.
cancellationTokenCancellationTokenCancels the scan.
Returns
- Task<IReadOnlyList<SshHostKey>>
The keys found, one per key type, in the order of HostKeyAlgorithms's preference; empty when the server offers none of the requested types.
Remarks
To add the keys to known_hosts, pass each one's KeyType and PublicKey
to Add(string, int, string, ReadOnlySpan<byte>, bool, string?, KnownHostsMarker).
Exceptions
- SshException
A connection or key exchange failed for another reason than an unsupported key type.
ScanHostKeysAsync(Func<CancellationToken, ValueTask<Stream>>, SshHostKeyScanOptions?, CancellationToken)
Fetches the server's host keys without authenticating, over streams that connect opens: one per key type, so
it is called several times. For a host behind a jump host, open each stream with OpenTcpTunnelAsync(string, int, CancellationToken).
public static Task<IReadOnlyList<SshHostKey>> ScanHostKeysAsync(Func<CancellationToken, ValueTask<Stream>> connect, SshHostKeyScanOptions? options = null, CancellationToken cancellationToken = default)
Parameters
connectFunc<CancellationToken, ValueTask<Stream>>Opens a new connected stream to the server; the scan disposes it.
optionsSshHostKeyScanOptionsWhich key types to ask for; by default every plain key type SshSharp supports.
cancellationTokenCancellationTokenCancels the scan.
Returns
- Task<IReadOnlyList<SshHostKey>>
The keys found, one per key type; empty when the server offers none of the requested types.
Exceptions
- SshException
A connection or key exchange failed for another reason than an unsupported key type.