Configuration

Configuration file (aka ssh_config) support.

class paramiko.config.SSHConfig

Representation of config information as stored in the format used by OpenSSH. Queries can be made via lookup. The format is described in OpenSSH’s ssh_config man page. This class is provided primarily as a convenience to posix users (since the OpenSSH format is a de-facto standard on posix) but should work fine on Windows too.

New in version 1.6.

__init__()

Create a new OpenSSH config object.

parse(file_obj)

Read an OpenSSH config from the given file object.

Parameters:file_obj – a file-like object to read the config file from
lookup(hostname)

Return a dict (SSHConfigDict) of config options for a given hostname.

The host-matching rules of OpenSSH’s ssh_config man page are used: For each parameter, the first obtained value will be used. The configuration files contain sections separated by Host specifications, and that section is only applied for hosts that match one of the patterns given in the specification.

Since the first obtained value for each parameter is used, more host- specific declarations should be given near the beginning of the file, and general defaults at the end.

The keys in the returned dict are all normalized to lowercase (look for "port", not "Port". The values are processed according to the rules for substitution variable expansion in ssh_config.

Finally, please see the docs for SSHConfigDict for deeper info on features such as optional type conversion methods, e.g.:

conf = my_config.lookup('myhost')
assert conf['passwordauthentication'] == 'yes'
assert conf.as_bool('passwordauthentication') is True
Parameters:hostname (str) – the hostname to lookup

Changed in version 2.5: Returns SSHConfigDict objects instead of dict literals.

get_hostnames()

Return the set of literal hostnames defined in the SSH config (both explicit hostnames and wildcard entries).

__weakref__

list of weak references to the object (if defined)

class paramiko.config.LazyFqdn(config, host=None)

Returns the host’s fqdn on request as string.

__weakref__

list of weak references to the object (if defined)

class paramiko.config.SSHConfigDict(*args, **kwargs)

A dictionary wrapper/subclass for per-host configuration structures.

This class introduces some usage niceties for consumers of SSHConfig, specifically around the issue of variable type conversions: normal value access yields strings, but there are now methods such as as_bool and as_int that yield casted values instead.

For example, given the following ssh_config file snippet:

Host foo.example.com
    PasswordAuthentication no
    Compression yes
    ServerAliveInterval 60

the following code highlights how you can access the raw strings as well as usefully Python type-casted versions (recalling that keys are all normalized to lowercase first):

my_config = SSHConfig()
my_config.parse(open('~/.ssh/config'))
conf = my_config.lookup('foo.example.com')

assert conf['passwordauthentication'] == 'no'
assert conf.as_bool('passwordauthentication') is False
assert conf['compression'] == 'yes'
assert conf.as_bool('compression') is True
assert conf['serveraliveinterval'] == '60'
assert conf.as_int('serveraliveinterval') == 60

New in version 2.5.

as_bool(key)

Express given key’s value as a boolean type.

Typically, this is used for ssh_config’s pseudo-boolean values which are either "yes" or "no". In such cases, "yes" yields True and any other value becomes False.

Note

If (for whatever reason) the stored value is already boolean in nature, it’s simply returned.

New in version 2.5.

as_int(key)

Express given key’s value as an integer, if possible.

This method will raise ValueError or similar if the value is not int-appropriate, same as the builtin int type.

New in version 2.5.

__weakref__

list of weak references to the object (if defined)