Table of Contents

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

host string

The server's host name or address.

port int

The server's port.

options SshHostKeyScanOptions

Which key types to ask for; by default every plain key type SshSharp supports.

cancellationToken CancellationToken

Cancels 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

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

connect Func<CancellationToken, ValueTask<Stream>>

Opens a new connected stream to the server; the scan disposes it.

options SshHostKeyScanOptions

Which key types to ask for; by default every plain key type SshSharp supports.

cancellationToken CancellationToken

Cancels 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.