ssh2.knownhost¶
- class ssh2.knownhost.KnownHost¶
Manage known host entries.
- add(self, bytes host, bytes salt, bytes key, int typemask)¶
Deprecated - use
self.addc
- addc(self, bytes host, bytes key, int typemask, bytes salt=None, bytes comment=None)¶
Adds a host and its key to known hosts collection.
Note -
libssh2expects correct use of hashed hosts whenLIBSSH2_KNOWNHOST_TYPE_SHA1is part of typemask. Incorrect use of hashed host typemask without appropriate hashed host and salt values will result in host entries being added to the collection without a host name.- Parameters
host (bytes) – Host to add key for.
key (bytes) – Key to add.
typemask – Bitmask of one of each from
ssh2.knownhost.LIBSSH2_KNOWNHOST_TYPE_*,ssh2.knownhost.LIBSSH2_KNOWNHOST_KEYENC_*andssh2.knownhost.LIBSSH2_KNOWNHOST_KEY_*for example for plain text host, raw key encoding and SSH RSA keytypewould beLIBSSH2_KNOWNHOST_TYPE_PLAIN | LIBSSH2_KNOWNHOST_KEYENC_RAW | LIBSSH2_KNOWNHOST_KEY_SSHRSA.salt (bytes) – Salt used for host hashing if host is hashed. Defaults to
None.comment (bytes) – Comment to add for host. Defaults to
None.
- Raises
ssh2.exceptions.KnownHostAddErroron errors adding known host entry.
- check(self, bytes host, bytes key, int typemask)¶
Deprecated - use
self.checkp
- checkp(self, bytes host, int port, bytes key, int typemask)¶
Check a host and its key against the known hosts collection and return known host entry, if any.
Note that server key provided to this function must be base64 encoded only if checking against a
self.addcadded known public key. When usingself.readfileand a known_hosts file, encoding is not needed.ssh2.exceptions.KnownHostCheckErroris base class for all host check error exceptions and can be used to catch all host check errors.- Parameters
host (bytes) – Host to check.
key (bytes) – Key of host to check.
typemask – Bitmask of one of each from
ssh2.knownhost.LIBSSH2_KNOWNHOST_TYPE_*,ssh2.knownhost.LIBSSH2_KNOWNHOST_KEYENC_*andssh2.knownhost.LIBSSH2_KNOWNHOST_KEY_*for example for plain text host, raw key encoding and SSH RSA keytypewould beLIBSSH2_KNOWNHOST_TYPE_PLAIN | LIBSSH2_KNOWNHOST_KEYENC_RAW | LIBSSH2_KNOWNHOST_KEY_SSHRSA.
- Raises
ssh2.exceptions.KnownHostCheckMisMatchErroron provided key mis-match error with found key from known hosts.- Raises
ssh2.exceptions.KnownHostCheckNotFoundErroron host not found in known hosts.- Raises
ssh2.exceptions.KnownHostCheckFailureon failure checking known host entry.- Raises
ssh2.exceptions.KnownHostCheckErroron unknown errors checking known host.- Return type
- delete(self, KnownHostEntry entry)¶
Delete given known host entry from collection of known hosts.
- Parameters
entry (
ssh2.knownhost.KnownHostEntry) – Known host entry to delete.- Raises
ssh2.exceptions.KnownHostDeleteErroron errors deleting host entry.
- get(self, KnownHostEntry prev=None)¶
Retrieve all host entries in known hosts collection.
- Parameters
prev – (Optional) Existing known host entry to start retrieval from. All hosts are retrieved when prev is
Nonewhich is the default.- Raises
ssh2.exceptions.KnownHostGetErroron errors retrieving known host collection.- Return type
- readfile(self, filename, int f_type=c_ssh2.LIBSSH2_KNOWNHOST_FILE_OPENSSH)¶
Read known hosts file and add hosts to known hosts collection. Only OpenSSH known hosts file format is currently supported.
Returns number of successfully read host entries.
- Parameters
filename (str) – File name to read.
- Raises
ssh2.exceptions.KnownHostReadFileErroron errors reading file.- Return type
int
- readline(self, bytes line, int f_type=c_ssh2.LIBSSH2_KNOWNHOST_FILE_OPENSSH)¶
Read line from known hosts file and add to known hosts collection. Only OpenSSH known hosts file format is currently supported.
Note - When using readline, the key values returned by
self.getwill need to be base64 encoded as libssh2’s readline does not encode them when adding, unlikeself.readfileandself.addc.- Parameters
line (bytes) – Byte string representing line to read.
- Raises
ssh2.exceptions.KnownHostReadLineErroron errors reading line.
- writefile(self, filename, int f_type=c_ssh2.LIBSSH2_KNOWNHOST_FILE_OPENSSH)¶
Write all known host entries to file. Only OpenSSH known hosts file format is currently supported.
- Parameters
filename (str) – File name to write known hosts to.
- Raises
ssh2.exceptions.KnownHostWriteFileErroron errors writing to file.
- writeline(self, KnownHostEntry entry, int f_type=c_ssh2.LIBSSH2_KNOWNHOST_FILE_OPENSSH, size_t buf_len=1024)¶
Convert a single known host entry to a single line of output for writing. Only OpenSSH known hosts file format is currently supported.
- Parameters
entry (
ssh2.knownhost.KnownHostEntry) – Known host entry to write line for.- Raises
ssh2.exceptions.KnownHostWriteLineErroron errors writing line.- Return type
bytes
- class ssh2.knownhost.KnownHostEntry¶
Class representing a single known host entry.
- key¶
Key byte string.
Key is stored base64 encoded according to
libssh2documentation and is returned by this property as a base64 decoded byte string.Note that in some cases, like keys added by
ssh2.knownhost.KnownHost.readline(), the stored key is not base64 encoded, contrary to documentation, andKnownHostEntry.keywill need to be re-encoded as base64 to get actual key.
- magic¶
Entry magic number.
- name¶
Name of host.
- typemask¶
Type mask of host entry.