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

Class HostKeys

source code

       object --+            
                |            
    _abcoll.Sized --+        
                    |        
       object --+   |        
                |   |        
 _abcoll.Iterable --+        
                    |        
       object --+   |        
                |   |        
_abcoll.Container --+        
                    |        
      _abcoll.Mapping --+    
                        |    
   _abcoll.MutableMapping --+
                            |
                           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`.

.. versionadded:: 1.5.3

Nested Classes

Inherited from _abcoll.Sized: __metaclass__

Instance Methods
 
__init__(self, filename=None)
Create a new HostKeys object, optionally loading keys from an OpenSSH style host-key file.
source code
 
add(self, hostname, keytype, key)
Add a host key entry to the table.
source code
 
load(self, filename)
Read a file of known SSH host keys, in the format used by OpenSSH.
source code
 
save(self, filename)
Save host keys into a file, in the format used by OpenSSH.
source code
 
lookup(self, hostname)
Find a hostkey entry for a given hostname or IP.
source code
 
check(self, hostname, key)
Return True if the given key is associated with the given hostname in this dictionary.
source code
None
clear(self)
Remove all host keys from the dictionary.
source code
 
__iter__(self) source code
 
__len__(self) source code
 
__delitem__(self, key) source code
 
__getitem__(self, key) source code
 
__setitem__(self, hostname, entry) source code
list of D's keys
keys(self) source code
list of D's values
values(self) source code

Inherited from _abcoll.MutableMapping: pop, popitem, setdefault, update

Inherited from _abcoll.Mapping: __contains__, __eq__, __ne__, get, items, iteritems, iterkeys, itervalues

Inherited from object: __delattr__, __format__, __getattribute__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__

Class Methods

Inherited from _abcoll.Sized: __subclasshook__

Static Methods
 
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
Class Variables
  __abstractmethods__ = frozenset([])

Inherited from _abcoll.Mapping: __hash__

Properties

Inherited from object: __class__

Method Details

__init__(self, filename=None)
(Constructor)

source code 

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

:param str filename: filename to load host keys from, or ``None``

Overrides: object.__init__

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.

:param str hostname: the hostname (or IP) to add :param str keytype: key type (``"ssh-rsa"`` or ``"ssh-dss"``) :param .PKey key: the key to add

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.

:param str filename: name of the file to read host keys from

:raises IOError: if there was an error reading the file

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.

:param str filename: name of the file to write

:raises IOError: if there was an error writing the file

.. versionadded:: 1.6.1

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"``.

:param str hostname: the hostname (or IP) to lookup :return: dict of `str` -> `.PKey` keys associated with this host (or ``None``)

check(self, hostname, key)

source code 

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

:param str hostname: hostname (or IP) of the SSH server
:param .PKey key: the key to check
:return:
    ``True`` if the key is associated with the hostname; else ``False``

clear(self)

source code 

Remove all host keys from the dictionary.

Returns: None
Overrides: _abcoll.MutableMapping.clear

__iter__(self)

source code 
Overrides: _abcoll.Iterable.__iter__

__len__(self)
(Length operator)

source code 
Overrides: _abcoll.Sized.__len__

__delitem__(self, key)
(Index deletion operator)

source code 
Overrides: _abcoll.MutableMapping.__delitem__

__getitem__(self, key)
(Indexing operator)

source code 
Overrides: _abcoll.Mapping.__getitem__

__setitem__(self, hostname, entry)
(Index assignment operator)

source code 
Overrides: _abcoll.MutableMapping.__setitem__

keys(self)

source code 
Returns: list of D's keys
Overrides: _abcoll.Mapping.keys
(inherited documentation)

values(self)

source code 
Returns: list of D's values
Overrides: _abcoll.Mapping.values
(inherited documentation)

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.

:param str hostname: the hostname to hash :param str salt: optional salt to use when hashing (must be 20 bytes long) :return: the hashed hostname as a `str`