
    3~h                         d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ed        Zd Z G d de	      Z y)    )contextmanager)StringIO)EventN)	decorator)Context)ThreadException)AgentRequestHandler)	SSHClientAutoAddPolicy)	SSHConfig)ProxyCommand   )Config)InvalidV1Env)Transfer)TunnelManagerTunnelc                 :    |j                           | |g|i |S N)open)methodselfargskwargss       Z/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/fabric/connection.pyopensr      s     IIK$((((    c                 $   | j                  dd      }|j                         }|r
|d   r|d   nd }|j                  d      dkD  r|}d }n5|j                  dd      }|j                  d      xs d }|r
|d   r|d   nd }|t        |      }|||dS )N@r   r   :)userhostport)rsplitpopcountint)host_stringuser_hostporthostportr!   r"   r#   	host_ports          r   derive_shorthandr,      s    &&sA.M  "H,q1A=tD
 ~~cQ OOC+	}}Q'4(Yq\y|t4y$55r   c                       e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZed        Z	 	 	 	 	 	 	 	 d fd	Zd Zd Zd Zd Zd	 Zd
 Zd Zd Zed        Zd Zd Zd Z d Z!d Z"e#d        Z$d Z%e#d        Z&e#d        Z'e#d        Z( fdZ)e#d        Z*d Z+d Z,e-e#	 	 	 dd              Z.e-e#	 	 	 d d              Z/ xZ0S )!
Connectionaq  
    A connection to an SSH daemon, with methods for commands and file transfer.

    **Basics**

    This class inherits from Invoke's `~invoke.context.Context`, as it is a
    context within which commands, tasks etc can operate. It also encapsulates
    a Paramiko `~paramiko.client.SSHClient` instance, performing useful high
    level operations with that `~paramiko.client.SSHClient` and
    `~paramiko.channel.Channel` instances generated from it.

    .. _connect_kwargs:

    .. note::
        Many SSH specific options -- such as specifying private keys and
        passphrases, timeouts, disabling SSH agents, etc -- are handled
        directly by Paramiko and should be specified via the
        :ref:`connect_kwargs argument <connect_kwargs-arg>` of the constructor.

    **Lifecycle**

    `.Connection` has a basic "`create <__init__>`, `connect/open <open>`, `do
    work <run>`, `disconnect/close <close>`" lifecycle:

    - `Instantiation <__init__>` imprints the object with its connection
      parameters (but does **not** actually initiate the network connection).

        - An alternate constructor exists for users :ref:`upgrading piecemeal
          from Fabric 1 <from-v1>`: `from_v1`

    - Methods like `run`, `get` etc automatically trigger a call to
      `open` if the connection is not active; users may of course call `open`
      manually if desired.
    - It's best to explicitly close your connections when done using them. This
      can be accomplished by manually calling `close`, or by using the object
      as a contextmanager::

          with Connection('host') as c:
             c.run('command')
             c.put('file')

      .. warning::
          While Fabric (and Paramiko) attempt to register connections for
          automatic garbage collection, it's not currently safe to rely on that
          feature, as it can lead to end-of-process hangs and similar behavior.

    .. note::
        This class rebinds `invoke.context.Context.run` to `.local` so both
        remote and local command execution can coexist.

    **Configuration**

    Most `.Connection` parameters honor :doc:`Invoke-style configuration
    </concepts/configuration>` as well as any applicable :ref:`SSH config file
    directives <connection-ssh-config>`. For example, to end up with a
    connection to ``admin@myhost``, one could:

    - Use any built-in config mechanism, such as ``/etc/fabric.yml``,
      ``~/.fabric.json``, collection-driven configuration, env vars, etc,
      stating ``user: admin`` (or ``{"user": "admin"}``, depending on config
      format.) Then ``Connection('myhost')`` would implicitly have a ``user``
      of ``admin``.
    - Use an SSH config file containing ``User admin`` within any applicable
      ``Host`` header (``Host myhost``, ``Host *``, etc.) Again,
      ``Connection('myhost')`` will default to an ``admin`` user.
    - Leverage host-parameter shorthand (described in `.Config.__init__`), i.e.
      ``Connection('admin@myhost')``.
    - Give the parameter directly: ``Connection('myhost', user='admin')``.

    The same applies to agent forwarding, gateways, and so forth.

    .. versionadded:: 2.0
    Nc                    |j                   st        d      |j                  di       }|j                  d|j                          t        |j                         }|j                  d|j                         |d   s%|j                  dt        |j                               |j                  |j                  d|j                         d|vrt        j                  |      |d<    | di |S )	a   
        Alternate constructor which uses Fabric 1's ``env`` dict for settings.

        All keyword arguments besides ``env`` are passed unmolested into the
        primary constructor.

        .. warning::
            Because your own config overrides will win over data from ``env``,
            make sure you only set values you *intend* to change from your v1
            environment!

        For details on exactly which ``env`` vars are imported and what they
        become in the new API, please see :ref:`v1-env-var-imports`.

        :param env:
            An explicit Fabric 1 ``env`` dict (technically, any
            ``fabric.utils._AttributeDict`` instance should work) to pull
            configuration from.

        .. versionadded:: 2.4
        zSupplied v1 env has an empty `host_string` value! Please make sure you're calling Connection.from_v1 within a connected Fabric 1 session.connect_kwargsr"   r!   r#   key_filenameconfig )
r(   r   
setdefaultr,   r!   r'   r#   r1   r   from_v1)clsenvr   r0   	shorthands        r   r5   zConnection.from_v1   s    8  \   **+;R@&#//2$S__5	&#((+  fc#((m4 '%%nc6F6FG 6!%~~c2F8}V}r   c
                 H   t         |   |       |t               }n&t        |t              s|j	                  t              }| j                  |       | j                  |      }
|
d   }d}|
d   !|t        |j                  d            |
d   }|
d   !|t        |j                  d            |
d   }| j                  j                  j                  |      | _        || _        || _        d	| j                  v r| j                  d	   | _        |xs0 | j                  j                  d| j                  j                         | _        |xs9 t#        | j                  j                  d| j                  j$                              | _        ||n| j'                         | _        |;| j                  j*                  }d
| j                  v rddd}|| j                  d
      }|| _        |:| j                  j                  d| j                  j,                  j.                        }|t#        |      }|| _        | j3                  |      | _        t7               }|j9                  t;                      || _        d| _        |	| j                  j@                  }	|	| _         y)a  
        Set up a new object representing a server connection.

        :param str host:
            the hostname (or IP address) of this connection.

            May include shorthand for the ``user`` and/or ``port`` parameters,
            of the form ``user@host``, ``host:port``, or ``user@host:port``.

            .. note::
                Due to ambiguity, IPv6 host addresses are incompatible with the
                ``host:port`` shorthand (though ``user@host`` will still work
                OK). In other words, the presence of >1 ``:`` character will
                prevent any attempt to derive a shorthand port number; use the
                explicit ``port`` parameter instead.

            .. note::
                If ``host`` matches a ``Host`` clause in loaded SSH config
                data, and that ``Host`` clause contains a ``Hostname``
                directive, the resulting `.Connection` object will behave as if
                ``host`` is equal to that ``Hostname`` value.

                In all cases, the original value of ``host`` is preserved as
                the ``original_host`` attribute.

                Thus, given SSH config like so::

                    Host myalias
                        Hostname realhostname

                a call like ``Connection(host='myalias')`` will result in an
                object whose ``host`` attribute is ``realhostname``, and whose
                ``original_host`` attribute is ``myalias``.

        :param str user:
            the login user for the remote connection. Defaults to
            ``config.user``.

        :param int port:
            the remote port. Defaults to ``config.port``.

        :param config:
            configuration settings to use when executing methods on this
            `.Connection` (e.g. default SSH port and so forth).

            Should be a `.Config` or an `invoke.config.Config`
            (which will be turned into a `.Config`).

            Default is an anonymous `.Config` object.

        :param gateway:
            An object to use as a proxy or gateway for this connection.

            This parameter accepts one of the following:

            - another `.Connection` (for a ``ProxyJump`` style gateway);
            - a shell command string (for a ``ProxyCommand`` style style
              gateway).

            Default: ``None``, meaning no gatewaying will occur (unless
            otherwise configured; if one wants to override a configured gateway
            at runtime, specify ``gateway=False``.)

            .. seealso:: :ref:`ssh-gateways`

        :param bool forward_agent:
            Whether to enable SSH agent forwarding.

            Default: ``config.forward_agent``.

        :param int connect_timeout:
            Connection timeout, in seconds.

            Default: ``config.timeouts.connect``.


        :param dict connect_kwargs:

            .. _connect_kwargs-arg:

            Keyword arguments handed verbatim to
            `SSHClient.connect <paramiko.client.SSHClient.connect>` (when
            `.open` is called).

            `.Connection` tries not to grow additional settings/kwargs of its
            own unless it is adding value of some kind; thus,
            ``connect_kwargs`` is currently the right place to hand in paramiko
            connection parameters such as ``pkey`` or ``key_filename``. For
            example::

                c = Connection(
                    host="hostname",
                    user="admin",
                    connect_kwargs={
                        "key_filename": "/home/myuser/.ssh/private.key",
                    },
                )

            Default: ``config.connect_kwargs``.

        :param bool inline_ssh_env:
            Whether to send environment variables "inline" as prefixes in front
            of command strings (``export VARNAME=value && mycommand here``;
            this is the default behavior), or submit them through the SSH
            protocol itself.

            In Fabric 2.x this defaulted to ``False`` (try using the protocol
            behavior), but in 3.x it changed to ``True`` due to the simple fact
            that most remote servers are deployed with a restricted
            ``AcceptEnv`` setting, making use of the protocol approach
            non-viable.

            The actual default value is the value of the ``inline_ssh_env``
            :ref:`configuration value <default-values>` (which, as above,
            currently defaults to ``True``).

            .. warning::
                This functionality does **not** currently perform any shell
                escaping on your behalf! Be careful when using nontrivial
                values, and note that you can put in your own quoting,
                backslashing etc if desired.

                Consider using a different approach (such as actual
                remote shell scripts) if you run into too many issues here.

            .. note::
                When serializing into prefixed ``FOO=bar`` format, we apply the
                builtin `sorted` function to the env dictionary's keys, to
                remove what would otherwise be ambiguous/arbitrary ordering.

            .. note::
                This setting has no bearing on *local* shell commands; it only
                affects remote commands, and thus, methods like `.run` and
                `.sudo`.

        :raises ValueError:
            if user or port values are given via both ``host`` shorthand *and*
            their own arguments. (We `refuse the temptation to guess`_).

        .. _refuse the temptation to guess:
            http://zen-of-python.info/
            in-the-face-of-ambiguity-refuse-the-temptation-to-guess.html#12

        .. versionchanged:: 2.3
            Added the ``inline_ssh_env`` parameter.

        .. versionchanged:: 3.0
            ``inline_ssh_env`` still defaults to the config value, but said
            config value has now changed and defaults to ``True``, not
            ``False``.
        r2   N)into)_configr"   zBYou supplied the {} via both shorthand and kwarg! Please pick one.r!   r#   hostnameforwardagentTF)yesnoconnecttimeout)!super__init__r   
isinstanceclone_setr,   
ValueErrorformatr2   base_ssh_configlookup
ssh_configoriginal_hostr"   getr!   r'   r#   get_gatewaygatewayforward_agenttimeoutsconnectconnect_timeoutresolve_connect_kwargsr0   r
   set_missing_host_key_policyr   client	transportinline_ssh_env)r   r"   r!   r#   r2   rO   rP   rS   r0   rX   r8   errmap_rV   	__class__s                 r   rC   zConnection.__init__   st   L 	' >XF FF+\\v\.F		&	! ))$/	 RV( F!344V$DV( F!344V$D
 ++55<<TB!	(
3DI IDOO//8H8HI	
 NC 3 3FDKK<L<L MN	 #*"5w4;K;K;M
   KK55M0#51 $T__^%D E*""oo11 $++"6"6">">O &!/2O. #99.I **=?; !![[77N -r   c                 8   |xs i }| j                   j                  }|j                  dg       }|j                  dg       }| j                  j                  dg       }|xs |}g }|||fD ]&  }	t	        |	t
              r|	g}	|j                  |	       ( |r||d<   |S )Nr1   identityfile)r2   r0   rM   rK   rD   strextend)
r   r0   constructor_kwargsconfig_kwargsconstructor_keysconfig_keysssh_config_keysfinal_kwargs
final_keysvalues
             r   rT   z!Connection.resolve_connect_kwargs  s     ,1r22-11."E#'';//--nbA *:]
 
!#3_E 	%E%%e$	%
 +5L(r   c                    d| j                   v rt        | j                   d   j                  d            }d }|D ]^  }| j                  |      d   | j                  k(  r y t        | j                  j                               }|||d<   t        |fi |}|}` |S d| j                   v r| j                   d   S | j                  j                  S )N	proxyjump,r"   r:   rO   proxycommand)
rK   reversedsplitr,   r"   dictr2   rE   r.   rO   )r   hopsprev_gwhopr   cxns         r   rN   zConnection.get_gateway  s    $//) DOOK8>>sCDDG  ((-f5B T[[%6%6%89&(/F9% // Nt.??>22{{"""r   c                    d| j                   fg}| j                  | j                  j                  k7  r|j                  d| j                  f       | j                  | j                  j                  k7  r|j                  d| j                  f       | j
                  r1d}t        | j
                  t              rd}|j                  d|f       dj                  dj                  d	 |D                    S )
Nr"   r!   r#   ri   rk   gwz<Connection {}> c              3   :   K   | ]  } d j                   |   yw)z{}={}N)rH   ).0xs     r   	<genexpr>z&Connection.__repr__.<locals>.<genexpr>(  s     6A^W^^Q'6s   )
r"   r!   r2   appendr#   rO   rD   r^   rH   join)r   bitsvals      r   __repr__zConnection.__repr__  s    #$99(((KK+, 99(((KK+, <<C$,,,$KKs$ ''HH666
 	
r   c                 H    | j                   | j                  | j                  fS r   )r"   r!   r#   r   s    r   	_identityzConnection._identity+  s     		499dii00r   c                 f    t        |t              sy| j                         |j                         k(  S )NF)rD   r.   r   r   others     r   __eq__zConnection.__eq__1  s(    %,~~5??#444r   c                 D    | j                         |j                         k  S r   )r   r   s     r   __lt__zConnection.__lt__6  s    ~~%//"333r   c                 4    t        | j                               S r   )hashr   r   s    r   __hash__zConnection.__hash__9  s     DNN$%%r   c                     t        |      S r   )r,   )r   r(   s     r   r,   zConnection.derive_shorthand>  s      ,,r   c                 J    | j                   r| j                   j                  S dS )za
        Whether or not this connection is actually open.

        .. versionadded:: 2.0
        F)rW   activer   s    r   is_connectedzConnection.is_connectedD  s     )-t~~$$AEAr   c                 0   | j                   ryd}dj                         D ]*  }|| j                  v st        |j	                  |             d| j                  v r&| j
                  t        |j	                  d            t        | j                  | j                  | j                  | j                        }| j                  r| j                         |d<   | j
                  r| j
                  |d<   d|v r|d   s|d= | j                  j                  }|EdD ]  }|j                  |d         || j                  | j                   | j                  	      |d
<    | j"                  j$                  di |}| j"                  j'                         | _        |S )a  
        Initiate an SSH connection to the host/port this object is bound to.

        This may include activating the configured gateway connection, if one
        is set.

        Also saves a handle to the now-set Transport object for easier access.

        Various connect-time settings (and/or their corresponding :ref:`SSH
        config options <ssh-config>`) are utilized here in the call to
        `SSHClient.connect <paramiko.client.SSHClient.connect>`. (For details,
        see :doc:`the configuration docs </concepts/configuration>`.)

        :returns:
            The result of the internal call to `.SSHClient.connect`, if
            performing an initial connection; ``None`` otherwise.

        .. versionadded:: 2.0
        .. versionchanged:: 3.1
            Now returns the inner Paramiko connect call's return value instead
            of always returning the implicit ``None``.
        NzeRefusing to be ambiguous: connect() kwarg '{}' was given both via regular arg and via connect_kwargs!zD
            hostname
            port
            username
        timeout)usernamer=   r#   sockr1   )allow_agentr1   look_for_keys
passphrasepasswordpkeyr   )rK   fabric_configr   auth_strategyr3   )r   rm   r0   rG   rH   rS   rn   r!   r"   r#   rO   open_gatewayauthenticationstrategy_classr%   rK   r2   rV   rR   get_transportrW   )r   rY   keyr   auth_strategy_classresults         r   r   zConnection.openM  s   0 u EG		2C
 d))) C11	2 ,,,$$0SZZ	233YYYY	
 <<!..0F6N $ 4 4F9V#F>,B~&"11@@* 	& 

3%	& ':??"kk'F?# %$$.v.224r   c                    t        | j                  t              rqt               }d}|j	                  t        |j                  | j                  | j                                     t        |j                  | j                        d         S | j                  j                          | j                  j                  j                  d| j                  t        | j                        fd      S )a$  
        Obtain a socket-like object from `gateway`.

        :returns:
            A ``direct-tcpip`` `paramiko.channel.Channel`, if `gateway` was a
            `.Connection`; or a `~paramiko.proxy.ProxyCommand`, if `gateway`
            was a string.

        .. versionadded:: 2.0
        zHost {}
    ProxyCommand {}rk   zdirect-tcpip) r   )kind	dest_addrsrc_addr)rD   rO   r^   r   parser   rH   r"   r   rJ   r   rW   open_channelr'   r#   )r   ssh_confdummys      r   r   zConnection.open_gateway  s     dllC( !{H2ENN8ELLDLL$IJK		 :> JKK 	 ||%%22yy#dii.1  3 
 	
r   c                    | j                   !| j                   j                          d| _         | j                  rO| j                  j                          | j                  r(| j
                  | j
                  j                          yyyy)ag  
        Terminate the network connection to the remote end, if open.

        If any SFTP sessions are open, they will also be closed.

        If no connection or SFTP session is open, this method does nothing.

        .. versionadded:: 2.0
        .. versionchanged:: 3.0
            Now closes SFTP sessions too (2.x required manually doing so).
        N)_sftpcloser   rV   rP   _agent_handlerr   s    r   r   zConnection.close  sr     ::!JJDJKK!!d&9&9&E##))+ 'F! r   c                     | S r   r3   r   s    r   	__enter__zConnection.__enter__  s    r   c                 $    | j                          y r   )r   )r   excs     r   __exit__zConnection.__exit__  s    

r   c                 r    | j                   j                         }| j                  rt        |      | _        |S r   )rW   open_sessionrP   r	   r   )r   channels     r   create_sessionzConnection.create_session  s/    ..--/"5g">Dr   c                 d    | j                   j                  j                  | | j                        S )N)context
inline_env)r2   runnersremoterX   r   s    r   _remote_runnerzConnection._remote_runner  s/    {{""))T%8%8 * 
 	
r   c                 F     | j                   | j                         |fi |S )a  
        Execute a shell command on the remote end of this connection.

        This method wraps an SSH-capable implementation of
        `invoke.runners.Runner.run`; see its documentation for details.

        .. warning::
            There are a few spots where Fabric departs from Invoke's default
            settings/behaviors; they are documented under
            `.Config.global_defaults`.

        .. versionadded:: 2.0
        )_runr   r   commandr   s      r   runzConnection.run  s%     tyy,,.B6BBr   c                 F     | j                   | j                         |fi |S )a  
        Execute a shell command, via ``sudo``, on the remote end.

        This method is identical to `invoke.context.Context.sudo` in every way,
        except in that -- like `run` -- it honors per-host/per-connection
        configuration overrides in addition to the generic/global ones. Thus,
        for example, per-host sudo passwords may be configured.

        .. versionadded:: 2.0
        )_sudor   r   s      r   sudozConnection.sudo  s%     tzz$--/CFCCr   c                    | j                   j                  j                  |       }d}i }| j                   j                         d   j	                         D ];  \  }}||v r-|j                  || j                   j                  |         ||<   7|||<   = |j                  d       |r3d}t        |j                  t        |j                                            |j                  d	ddi|S )
a
  
        Run an interactive login shell on the remote end, as with ``ssh``.

        This method is intended strictly for use cases where you can't know
        what remote shell to invoke, or are connecting to a non-POSIX-server
        environment such as a network appliance or other custom SSH server.
        Nearly every other use case, including interactively-focused ones, will
        be better served by using `run` plus an explicit remote shell command
        (eg ``bash``).

        `shell` has the following differences in behavior from `run`:

        - It still returns a `~invoke.runners.Result` instance, but the object
          will have a less useful set of attributes than with `run` or `local`:

            - ``command`` will be ``None``, as there is no such input argument.
            - ``stdout`` will contain a full record of the session, including
              all interactive input, as that is echoed back to the user. This
              can be useful for logging but is much less so for doing
              programmatic things after the method returns.
            - ``stderr`` will always be empty (same as `run` when
              ``pty==True``).
            - ``pty`` will always be True (because one was automatically used).
            - ``exited`` and similar attributes will only reflect the overall
              session, which may vary by shell or appliance but often has no
              useful relationship with the internally executed commands' exit
              codes.

        - This method behaves as if ``warn`` is set to ``True``: even if the
          remote shell exits uncleanly, no exception will be raised.
        - A pty is always allocated remotely, as with ``pty=True`` under `run`.
        - The ``inline_env`` setting is ignored, as there is no default shell
          command to add the parameters to (and no guarantee the remote end
          even is a shell!)

        It supports **only** the following kwargs, which behave identically to
        their counterparts in `run` unless otherwise stated:

        - ``encoding``
        - ``env``
        - ``in_stream`` (useful in niche cases, but make sure regular `run`
          with this argument isn't more suitable!)
        - ``replace_env``
        - ``watchers`` (note that due to pty echoing your stdin back to stdout,
          a watcher will see your input as well as program stdout!)

        Those keyword arguments also honor the ``run.*`` configuration tree, as
        in `run`/`sudo`.

        :returns: `~invoke.runners.Result`

        :raises:
            `~invoke.exceptions.ThreadException` (if the background I/O threads
            encountered exceptions other than
            `~invoke.exceptions.WatcherError`).

        .. versionadded:: 2.7
        )r   )encodingr7   	in_streamreplace_envwatchersr   T)ptyz.shell() got unexpected keyword arguments: {!r}r   Nr3   )r2   r   remote_shellglobal_defaultsitemsr%   r   update	TypeErrorrH   listkeys)r   r   runnerallowed
new_kwargsr   rg   rY   s           r   shellzConnection.shell  s    x $$11$1? N
++557>DDF 	(JCg~ #)**S$++//#2F"G
3"'
3	( 	d#BCCJJtFKKM':;<<vzz5$5*55r   c                 "    t        |   |i |S )z
        Execute a shell command on the local system.

        This method is effectively a wrapper of `invoke.run`; see its docs for
        details and call signature.

        .. versionadded:: 2.0
        )rB   r   )r   r   r   r[   s      r   localzConnection.local[  s     w{D+F++r   c                 p    | j                   | j                  j                         | _         | j                   S )ao  
        Return a `~paramiko.sftp_client.SFTPClient` object.

        If called more than one time, memoizes the first result; thus, any
        given `.Connection` instance will only ever have a single SFTP client,
        and state (such as that managed by
        `~paramiko.sftp_client.SFTPClient.chdir`) will be preserved.

        .. versionadded:: 2.0
        )r   rV   	open_sftpr   s    r   sftpzConnection.sftph  s,     ::..0DJzzr   c                 8     t        |       j                  |i |S )z
        Get a remote file to the local filesystem or file-like object.

        Simply a wrapper for `.Transfer.get`. Please see its documentation for
        all details.

        .. versionadded:: 2.0
        )r   rM   r   r   r   s      r   rM   zConnection.getx        "x~!!42622r   c                 8     t        |       j                  |i |S )z
        Put a local file (or file-like object) to the remote filesystem.

        Simply a wrapper for `.Transfer.put`. Please see its documentation for
        all details.

        .. versionadded:: 2.0
        )r   putr   s      r   r   zConnection.put  r   r   c              #     K   |s|}t               }t        ||||| j                  |      }|j                          	 d |j	                          |j                          |j                         }|*|j                  t        u r|j                  t        |g      y# |j	                          |j                          |j                         }|*|j                  t        u r|j                  t        |g      w xY ww)a  
        Open a tunnel connecting ``local_port`` to the server's environment.

        For example, say you want to connect to a remote PostgreSQL database
        which is locked down and only accessible via the system it's running
        on. You have SSH access to this server, so you can temporarily make
        port 5432 on your local system act like port 5432 on the server::

            import psycopg2
            from fabric import Connection

            with Connection('my-db-server').forward_local(5432):
                db = psycopg2.connect(
                    host='localhost', port=5432, database='mydb'
                )
                # Do things with 'db' here

        This method is analogous to using the ``-L`` option of OpenSSH's
        ``ssh`` program.

        :param int local_port: The local port number on which to listen.

        :param int remote_port:
            The remote port number. Defaults to the same value as
            ``local_port``.

        :param str local_host:
            The local hostname/interface on which to listen. Default:
            ``localhost``.

        :param str remote_host:
            The remote hostname serving the forwarded remote port. Default:
            ``localhost`` (i.e., the host this `.Connection` is connected to.)

        :returns:
            Nothing; this method is only useful as a context manager affecting
            local operating system state.

        .. versionadded:: 2.0
        )
local_port
local_hostremote_portremote_hostrW   finishedN)
r   r   rW   startsetr{   	exceptiontyper   rg   )r   r   r   r   r   r   managerwrappers           r   forward_localzConnection.forward_local  s     b $K
 7!!##nn
 		5 LLNLLN '')G"<<?2!--')7)44	 # LLNLLN '')G"<<?2!--')7)44	 #s   :C?B AC?AC<<C?c              #     K   s|g fd}	 | j                   j                  |||       d D ],  }|j                  j                          |j	                          . | j                   j                  ||       y# D ],  }|j                  j                          |j	                          . | j                   j                  ||       w xY ww)a  
        Open a tunnel connecting ``remote_port`` to the local environment.

        For example, say you're running a daemon in development mode on your
        workstation at port 8080, and want to funnel traffic to it from a
        production or staging environment.

        In most situations this isn't possible as your office/home network
        probably blocks inbound traffic. But you have SSH access to this
        server, so you can temporarily make port 8080 on that server act like
        port 8080 on your workstation::

            from fabric import Connection

            c = Connection('my-remote-server')
            with c.forward_remote(8080):
                c.run("remote-data-writer --port 8080")
                # Assuming remote-data-writer runs until interrupted, this will
                # stay open until you Ctrl-C...

        This method is analogous to using the ``-R`` option of OpenSSH's
        ``ssh`` program.

        :param int remote_port: The remote port number on which to listen.

        :param int local_port:
            The local port number. Defaults to the same value as
            ``remote_port``.

        :param str local_host:
            The local hostname/interface the forwarded connection talks to.
            Default: ``localhost``.

        :param str remote_host:
            The remote interface address to listen on when forwarding
            connections. Default: ``127.0.0.1`` (i.e. only listen on the remote
            localhost).

        :returns:
            Nothing; this method is only useful as a context manager affecting
            local operating system state.

        .. versionadded:: 2.0
        c                     t        j                          }|j                  f       t        | |t                     }|j	                          j                  |       y )N)r   r   r   )socketrR   r   r   r   rz   )r   src_addr_tupdst_addr_tupr   tunnelr   r   tunnelss        r   callbackz+Connection.forward_remote.<locals>.callback<  sF    ==?DLL*j12 G$IFLLNNN6"r   )addressr#   handlerN)r   r#   )rW   request_port_forwardr   r   r{   cancel_port_forward)r   r   r   r   r   r   r   r   s     ` `  @r   forward_remotezConnection.forward_remote  s     j $J  
	#	NN//#+x 0   " ##% NN..#+ /  " ##% NN..#+ / s   C"B ACACC)NNNNNNNN)N	localhostr   )Nz	127.0.0.1r   )1__name__
__module____qualname____doc__r"   rL   r!   r#   rK   rO   rP   rS   r0   rV   rW   r   r   classmethodr5   rC   rT   rN   r~   r   r   r   r   r,   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   rM   r   r   r   r   __classcell__)r[   s   @r   r.   r.   1   s   H` DMDDJGMONFIEN7 7F D-L:#<
215
4&
- B BN`&
P,*  

 C C  D D M6 M6^,  	3	3 
 Z5  Z5B 
 e  er   r.   )!
contextlibr   ior   	threadingr   r   r   invoker   invoke.exceptionsr   paramiko.agentr	   paramiko.clientr
   r   paramiko.configr   paramiko.proxyr   r2   r   
exceptionsr   transferr   r   r   r   r   r,   r.   r3   r   r   <module>r     sY    %      - . 4 % '  $  * ) )
6.j jr   