3 L]4@sddZddlZddlZddlZddlZddlZdZGdddeZGdddeZ Gdd d e Z dS) z2 Configuration file (aka ``ssh_config``) support. Nc@sReZdZdZejdZddZddZddZ d d Z d d Z d dZ ddZ dS) SSHConfiga 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. .. versionadded:: 1.6 z(\w+)(?:\s*=\s*|\s+)(.+)cCs g|_dS)z5 Create a new OpenSSH config object. N)_config)selfr5/tmp/pip-install-wfra5znf/paramiko/paramiko/config.py__init__.szSSHConfig.__init__cCs<dgid}x|D]}|j}| s|jdr4qtj|j|}|sTtdj||jdj}|jd}|dkr|j j ||j |id}q|dkr|jd krd |d |<q|jd r|j d r|dd}|dkr||d kr|d |j |n|g|d |<q||d kr||d |<qW|j j |d S)z Read an OpenSSH config from the given file object. :param file_obj: a file-like object to read the config file from *)hostconfig#zUnparsable line {}r proxycommandnoneNr " identityfile localforward remoteforward)rrr) strip startswithrematchSETTINGS_REGEX Exceptionformatgrouplowerrappend _get_hostsendswith)rZfile_objr linerkeyvaluerrrparse4s0      zSSHConfig.parsecsfddjD}t}xb|D]Z}xT|djD]D\}}||krb|dk rX|ddn|||<q4|dkr4||j|q4Wq"Wj|}d|kr|ddkr|d=|S)a; 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 :param str hostname: the hostname to lookup .. versionchanged:: 2.5 Returns `SSHConfigDict` objects instead of dict literals. cs g|]}j|dr|qS)r )_allowed).0r )hostnamerrr sz$SSHConfig.lookup..r Nrr)r SSHConfigDictitemsextend_expand_variables)rr(matchesretrr#r$r)r(rrlookup`s    zSSHConfig.lookupcCs(t}x|jD]}|j|dqW|S)z Return the set of literal hostnames defined in the SSH config (both explicit hostnames and wildcard entries). r )setrupdate)rhostsentryrrr get_hostnamess zSSHConfig.get_hostnamescCsHd}x>|D]6}|jdr0tj||ddr0dStj||r d}q W|S)NF!r T)rfnmatch)rr3r(rr rrrr&s  zSSHConfig._allowedcCsd|kr|djd||d<n||d<d|kr8|d}nt}tjd}d|krX|d}n|}tjjdd}t||}tjj d}d|dfd |fd |fd |fd |fd |fd|fgd|fd|fd|dfd |fd|fd |fgd|fd|dfd |fd |fgd} x|D]} || dkrq| | krx| | D]\} } t || t rxtt t || D]8} | || | kr\|| | j| t| || | <q\Wn&| || kr2|| j| t| || <q2WqW|S)aC Return a dict of config options with expanded substitutions for a given hostname. Please refer to man ``ssh_config`` for the parameters that are replaced. :param dict config: the config for the hostname :param str hostname: the hostname that the config belongs to r(z%hportUSERuser.r~z%lz%Lz%nz%pz%rz%uz%d)Z controlpathrrN)replaceSSH_PORTosgetenvsocket gethostnamesplitLazyFqdnpath expanduser isinstancelistrangelenstr)rr r(r8r:Z remoteuserr fqdnZhomedir replacementskfindr=itemrrrr-sV            $zSSHConfig._expand_variablesc Cs2y tj|Stk r,tdj|YnXdS)z> Return a list of host_names from host value. zUnparsable host {}N)shlexrC ValueErrorrr)rr rrrr s zSSHConfig._get_hostsN)__name__ __module__ __qualname____doc__rcompilerrr%r0r5r&r-r rrrrr!s  ,5  Irc@s"eZdZdZdddZddZdS)rDz7 Returns the host's fqdn on request as string. NcCsd|_||_||_dS)N)rLr r )rr r rrrrszLazyFqdn.__init__c Cs|jdkrd}|jjddj}|dkrybtj}|dkr>tjtj|jd|tj tj tj }x,|D]$}|\}}}} } | r`d| kr`| }Pq`WWntj k rYnX|dkrtj }||_|jS)NZ addressfamilyanyZinetr;)rLr getrrAAF_INET6AF_INET getaddrinfor SOCK_DGRAM IPPROTO_IP AI_CANONNAMEgaierrorgetfqdn) rrLaddress_familyfamilyresultsresafsocktypeproto canonnamesarrr__str__s4    zLazyFqdn.__str__)N)rSrTrUrVrrkrrrrrDs rDcs0eZdZdZfddZddZddZZS)r*a 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 .. versionadded:: 2.5 cstt|j||dS)N)superr*r)rargskwargs) __class__rrrTszSSHConfigDict.__init__cCs"||}t|tr|S|jdkS)a 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. .. versionadded:: 2.5 yes)rGboolr)rr#valrrras_boolXs zSSHConfigDict.as_boolcCs t||S)z 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. .. versionadded:: 2.5 )int)rr#rrras_intks zSSHConfigDict.as_int)rSrTrUrVrrsru __classcell__rr)rorr*2s  r*) rVr7r?rrQrAr>objectrrDdictr*rrrrs[7