Debian Codegraph @

sites/docs/api/config.rst

raw
1 =============
2 Configuration
3 =============
4
5 Paramiko **does not itself** leverage `OpenSSH-style config file directives
6 <ssh_config>`_, but it **does** implement a parser for the format, which users
7 can honor themselves (and is used by higher-level libraries, such as
8 `Fabric`_).
9
10 The API for this is `.SSHConfig`, which loads SSH config files from disk,
11 file-like object, or string and exposes a "look up a hostname, get a dict of
12 applicable keywords/values back" functionality.
13
14 As with OpenSSH's own support, this dict will contain values from across the
15 parsed file, depending on the order in which keywords were encountered and how
16 specific or generic the ``Host`` or ``Match`` directives were.
17
18 .. note:;
19 Result keys are lowercased for consistency and ease of deduping, as the
20 overall parsing/matching is itself case-insensitive. Thus, a source file
21 containing e.g. ``ProxyCommand`` will result in lookup results like
22 ``{"proxycommand": "shell command here"}``.
23
24
25 .. _ssh-config-support:
26
27 Keywords currently supported
28 ============================
29
30 The following is an alphabetical list of which `ssh_config`_ directives
31 Paramiko interprets during the parse/lookup process (as above, actual SSH
32 connections **do not** reference parsed configs). Departures from `OpenSSH's
33 implementation <ssh_config>`_ (e.g. to support backwards compat with older
34 Paramiko releases) are included. A keyword by itself means no known departures.
35
36 - ``AddressFamily``: used when looking up the local hostname for purposes of
37 expanding the ``%l``/``%L`` :ref:`tokens <TOKENS>` (this is actually a minor
38 value-add on top of OpenSSH, which doesn't actually honor this setting when
39 expanding ``%l``).
40 - ``CanonicalDomains``
41
42 .. versionadded:: 2.7
43
44 - ``CanonicalizeFallbackLocal``: when ``no``, triggers raising of
45 `.CouldNotCanonicalize` for target hostnames which do not successfully
46 canonicalize.
47
48 .. versionadded:: 2.7
49
50 - ``CanonicalizeHostname``: along with the other ``Canonicaliz*`` settings
51 (sans ``CanonicalizePermittedCNAMEs``, which is not yet implemented), enables
52 hostname canonicalization, insofar as calling `.SSHConfig.lookup` with a
53 given hostname will return a canonicalized copy of the config data, including
54 an updated ``HostName`` value.
55
56 .. versionadded:: 2.7
57
58 - ``CanonicalizeMaxDots``
59
60 .. versionadded:: 2.7
61
62 - ``Host``
63 - ``HostName``: used in ``%h`` :ref:`token expansion <TOKENS>`
64 - ``Match``: supports the keywords ``all``, ``canonical``, ``exec``, ``final``,
65 ``host``, ``localuser``, ``originalhost``, and ``user``, with the following
66 caveats:
67
68 - You must have the optional dependency Invoke installed; see :ref:`the
69 installation docs <paramiko-itself>` (in brief: install
70 ``paramiko[invoke]`` or ``paramiko[all]``).
71 - As usual, connection-time information is not present during config
72 lookup, and thus cannot be used to determine matching. This primarily
73 impacts ``Match user``, which can match against loaded ``User`` values
74 but has no knowledge about connection-time usernames.
75
76 .. versionadded:: 2.7
77 .. versionchanged:: 3.3
78 Added support for the ``final`` keyword.
79
80 - ``Port``: supplies potential values for ``%p`` :ref:`token expansion
81 <TOKENS>`.
82 - ``ProxyCommand``: see our `.ProxyCommand` class for an easy
83 way to honor this keyword from a config you've parsed.
84
85 - Honors :ref:`token expansion <TOKENS>`.
86 - When a lookup would result in an effective ``ProxyCommand none``,
87 Paramiko (as of 1.x-2.x) strips it from the resulting dict entirely. A
88 later major version may retain the ``"none"`` marker for clarity's sake.
89
90 - ``User``: supplies potential values for ``%u`` :ref:`token expansion
91 <TOKENS>`.
92
93 .. _TOKENS:
94
95 Expansion tokens
96 ----------------
97
98 We support most SSH config expansion tokens where possible, so when they are
99 present in a config file source, the result of a `.SSHConfig.lookup` will
100 contain the expansions/substitutions (based on the rest of the config or
101 properties of the local system).
102
103 Specifically, we are known to support the below, where applicable (e.g. as in
104 OpenSSH, ``%L`` works in ``ControlPath`` but not elsewhere):
105
106 - ``%C``
107 - ``%d``
108 - ``%h``
109 - ``%l``
110 - ``%L``
111 - ``%n``
112 - ``%p``
113 - ``%r``
114 - ``%u``: substitutes the configured ``User`` value, or the local user (as seen
115 by ``getpass.getuser``) if not specified.
116
117 In addition, we extend OpenSSH's tokens as follows:
118
119 - ``~`` is treated like ``%d`` (expands to the local user's home directory
120 path) when expanding ``ProxyCommand`` values, since ``ProxyCommand`` does not
121 natively support ``%d`` for some reason.
122
123
124 .. _ssh_config: https://man.openbsd.org/ssh_config
125 .. _Fabric: http://fabfile.org
126
127
128 ``config`` module API documentation
129 ===================================
130
131 Mostly of interest to contributors; see previous section for behavioral
132 details.
133
134 .. automodule:: paramiko.config
135 :member-order: bysource