Package paramiko :: Class HostKeys |
[frames] | no frames]

# Class HostKeys

source code

    UserDict.DictMixin --+
                         |
                        HostKeys

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.

Since: 1.5.3

Instance Methods
__getitem__(self, key) | source code
__init__(self, filename=None) Create a new HostKeys object, optionally loading keys from an openssh style host-key file. | source code
__setitem__(self, hostname, entry) | source code
add(self, hostname, keytype, key) Add a host key entry to the table. | source code
bool |
check(self, hostname, key) Return True if the given key is associated with the given hostname in this dictionary. | source code
clear(self) Remove all host keys from the dictionary. | source code
keys(self) | source code
load(self, filename) Read a file of known SSH host keys, in the format used by openssh. | source code
dict(str, PKey) |
lookup(self, hostname) Find a hostkey entry for a given hostname or IP. | source code
save(self, filename) Save host keys into a file, in the format used by openssh. | source code
values(self) | source code
Inherited from `UserDict.DictMixin`: `__cmp__`, `__contains__`, `__iter__`, `__len__`, `__repr__`, `get`, `has_key`, `items`, `iteritems`, `iterkeys`, `itervalues`, `pop`, `popitem`, `setdefault`, `update`

Static Methods
str |
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. | source code

Method Details

### __init__(self, filename=None) (Constructor) | source code
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`

### add(self, hostname, keytype, key) | source code
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(self, hostname, key) | source code
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: bool `True` if the key is associated with the hostname; `False` if not

### clear(self) | source code
Remove all host keys from the dictionary. Overrides: UserDict.DictMixin.clear

### hash_host(hostname, salt=None) Static Method | source code
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: str the hashed hostname

### load(self, filename) | source code
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(self, hostname) | source code
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(str, PKey) keys associated with this host (or `None`)

### save(self, filename) | source code
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 Since: 1.6.1

### values(self) | source code
Overrides: UserDict.DictMixin.values