paramiko.hostkeys.HostKeys

class paramiko.hostkeys.HostKeys(filename=None)

Representation of an OpenSSH-style “known hosts” file. Host keys can be read from one or more files, and then individual hosts can be looked up to verify server keys during SSH negotiation.

A .HostKeys object can be treated like a dict; any dict lookup is equivalent to calling lookup.

New in version 1.5.3.

Create a new HostKeys object, optionally loading keys from an OpenSSH style host-key file.

Parameters:filename (str) – filename to load host keys from, or None
__init__(filename=None)

Create a new HostKeys object, optionally loading keys from an OpenSSH style host-key file.

Parameters:filename (str) – filename to load host keys from, or None

Methods

__init__([filename]) Create a new HostKeys object, optionally loading keys from an OpenSSH style host-key file.
add(hostname, keytype, key) Add a host key entry to the table.
check(hostname, key) Return True if the given key is associated with the given hostname in this dictionary.
clear() Remove all host keys from the dictionary.
get((k[,d]) -> D[k] if k in D, ...)
HostKeys.has_key
hash_host(hostname[, salt]) Return a “hashed” form of the hostname, as used by OpenSSH when storing hashed hostnames in the known_hosts file.
items(() -> list of D’s (key, value) pairs, ...)
iteritems(() -> an iterator over the (key, ...)
iterkeys(() -> an iterator over the keys of D)
itervalues(...)
keys()
load(filename) Read a file of known SSH host keys, in the format used by OpenSSH.
lookup(hostname) Find a hostkey entry for a given hostname or IP.
pop((k[,d]) -> v, ...) If key is not found, d is returned if given, otherwise KeyError is raised.
popitem(() -> (k, v), ...) as a 2-tuple; but raise KeyError if D is empty.
save(filename) Save host keys into a file, in the format used by OpenSSH.
setdefault((k[,d]) -> D.get(k,d), ...)
update(([E, ...) If E present and has a .keys() method, does: for k in E: D[k] = E[k]
values()
add(hostname, keytype, key)

Add a host key entry to the table. Any existing entry for a (hostname, keytype) pair will be replaced.

Parameters:
  • hostname (str) – the hostname (or IP) to add
  • keytype (str) – key type ("ssh-rsa" or "ssh-dss")
  • key (.PKey) – the key to add
check(hostname, key)

Return True if the given key is associated with the given hostname in this dictionary.

Parameters:
  • hostname (str) – hostname (or IP) of the SSH server
  • key (.PKey) – the key to check
Returns:

True if the key is associated with the hostname; else False

clear()

Remove all host keys from the dictionary.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
static hash_host(hostname, salt=None)

Return a “hashed” form of the hostname, as used by OpenSSH when storing hashed hostnames in the known_hosts file.

Parameters:
  • hostname (str) – the hostname to hash
  • salt (str) – optional salt to use when hashing (must be 20 bytes long)
Returns:

the hashed hostname as a str

items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
load(filename)

Read a file of known SSH host keys, in the format used by OpenSSH. This type of file unfortunately doesn’t exist on Windows, but on posix, it will usually be stored in os.path.expanduser("~/.ssh/known_hosts").

If this method is called multiple times, the host keys are merged, not cleared. So multiple calls to load will just call add, replacing any existing entries and adding new ones.

Parameters:filename (str) – name of the file to read host keys from
Raises IOError:if there was an error reading the file
lookup(hostname)

Find a hostkey entry for a given hostname or IP. If no entry is found, None is returned. Otherwise a dictionary of keytype to key is returned. The keytype will be either "ssh-rsa" or "ssh-dss".

Parameters:hostname (str) – the hostname (or IP) to lookup
Returns:dict of str -> .PKey keys associated with this host (or None)
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

save(filename)

Save host keys into a file, in the format used by OpenSSH. The order of keys in the file will be preserved when possible (if these keys were loaded from a file originally). The single exception is that combined lines will be split into individual key lines, which is arguably a bug.

Parameters:filename (str) – name of the file to write
Raises IOError:if there was an error writing the file

New in version 1.6.1.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

Navigation