Table of Contents

Class KnownHostsFile

Namespace
SshSharp.Config
Assembly
SshSharp.dll

An OpenSSH known_hosts file. Comments, blank lines and unrecognized lines are preserved when the file is saved.

public sealed class KnownHostsFile
Inheritance
KnownHostsFile
Inherited Members

Constructors

KnownHostsFile()

Creates an empty file.

Properties

Entries

The valid entries, in file order.

InvalidLineCount

The number of lines that could not be parsed (kept as-is).

Methods

Add(string, int, string, ReadOnlySpan<byte>, bool, string?, KnownHostsMarker)

Appends an entry for a host.

AppendAsync(string, string, int, string, ReadOnlyMemory<byte>, bool, string?, KnownHostsMarker, CancellationToken)

Appends one entry to a file without rewriting it, as AppendAsync(string, string, int, string, ReadOnlyMemory<byte>, bool, CancellationToken) does, with a comment and a marker, as Add(string, int, string, ReadOnlySpan<byte>, bool, string?, KnownHostsMarker) takes them.

AppendAsync(string, string, int, string, ReadOnlyMemory<byte>, bool, CancellationToken)

Appends one entry to a file without rewriting it (creating the file and directory if needed), the way ssh records a new host key. Other processes appending at the same time do not lose their lines, unlike a load-modify-save.

Check(string, int, string, ReadOnlySpan<byte>)

Checks a presented host key. @revoked wins over everything; a matching key wins over a changed one.

Check(string, string, ReadOnlySpan<byte>)

Checks a presented host key on port 22, as Check(string, int, string, ReadOnlySpan<byte>) does.

Find(string, int)

Finds the entries that apply to a host and port.

FindCertificateAuthorities(string, int)

Finds @cert-authority keys that apply to a host and port.

Load(string)

Loads a file. A missing file gives an empty KnownHostsFile.

LoadAsync(string, CancellationToken)

Loads a file. A missing file gives an empty KnownHostsFile.

Parse(string)

Parses known_hosts text.

Remove(string, int)

Removes a host, like ssh-keygen -R: a line that names only this host (or its hash) is dropped, and on a line with several host patterns only this host's pattern is removed. Lines that match only through a wildcard are kept, because they also cover other hosts, and @cert-authority/@revoked lines are never touched. Returns the number of entries changed or removed.

Save(string)

Saves the file, replacing it atomically and creating the directory if needed.

SaveAsync(string, CancellationToken)

Saves the file, replacing it atomically and creating the directory if needed.

ToString()

Formats the file as text, with \n line endings.