
j4Vdc           @  s4  d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z d d l m Z m Z d d l m Z m Z m Z d d l m Z y, d d l Z e j d	 e  d d l Z WnV e k
 rDZ d d l Z e j   d
 j   Z e
 j  j! e d  e
 j" d  n Xe j# d  Z$ d   Z% d   Z& d   Z' e( d  Z) d e* f d     YZ+ d d  Z- d   Z. d d  Z/ d   Z0 e( d  Z1 d   Z2 d   Z3 d   Z4 d d  Z5 d   Z6 e7 d  Z8 d   Z9 d e( d d   Z: d!   Z; d"   Z< d S(#   sN   
Classes and subroutines dealing with network connections and related topics.
i(   t   with_statement(   t   wrapsN(   t   StringIO(   t   get_passwordt   set_password(   t   abortt   handle_prompt_abortt   warn(   t   NetworkErrort   ignores   
There was a problem importing our SSH library (see traceback above).
Please make sure all dependencies are installed and importable.
s   
i   s?   ^\[?(?P<host>[0-9A-Fa-f:]+(?:%[a-z]+\d+)?)\]?(:(?P<port>\d+))?$c         C  s%   |  j    j d | t |  f d  S(   Ns   direct-tcpipt    i    (   R
   i    (   t   get_transportt   open_channelt   int(   t   clientt   hostt   port(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   direct_tcpip(   s    c         C  s"   |  j  t j k o! d t |   k S(   Ns   Unable to parse key file(   t	   __class__t   ssht   SSHExceptiont   str(   t   e(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   is_key_load_error0   s    c         C  s   d d l  m } |  | j k S(   Ni(   t   env(   t   fabric.stateR   t   connection_attempts(   t   triesR   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   _tried_enough7   s    c   	      C  s   d d l  m } m } d } t   j d d  } | j r t | j  } | s[ | | k r | j rp d | GHn  t	 t
 |  | t f   | | <n  t t j | |  |  |  } n | r t j |  } n  | S(   s  
    Create and return a gateway socket, if one is needed.

    This function checks ``env`` for gateway or proxy-command settings and
    returns the necessary socket-like object for use by a final host
    connection.

    :param host:
        Hostname of target server.

    :param port:
        Port to connect to on target server.

    :param cache:
        A ``HostConnectionCache`` object, in which gateway ``SSHClient``
        objects are to be retrieved/cached.

    :param replace:
        Whether to forcibly replace a cached gateway client object.

    :returns:
        A ``socket.socket``-like object, or ``None`` if none was created.
    i(   R   t   outputt   proxycommands%   Creating new gateway connection to %rN(   R   R   R   t   Nonet
   ssh_configt   gett   gatewayt   normalize_to_stringt   debugt   connectt	   normalizet   FalseR   t   dictt   __getitem__R   t   ProxyCommand(	   R   R   t   cachet   replaceR   R   t   sockt   proxy_commandR"   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   get_gateway<   s    		#!t   HostConnectionCachec           B  s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s:  
    Dict subclass allowing for caching of host connections/clients.

    This subclass will intelligently create new client connections when keys
    are requested, or return previously created connections instead.

    It also handles creating new socket-like objects when required to implement
    gateway connections and `ProxyCommand`, and handing them to the inner
    connection methods.

    Key values are the same as host specifiers throughout Fabric: optional
    username + ``@``, mandatory hostname, optional ``:`` + port number.
    Examples:

    * ``example.com`` - typical Internet host address.
    * ``firewall`` - atypical, but still legal, local host address.
    * ``user@example.com`` - with specific username attached.
    * ``bob@smith.org:222`` - with specific nonstandard port attached.

    When the username is not given, ``env.user`` is used. ``env.user``
    defaults to the currently running user at startup but may be overwritten by
    user code or by specifying a command-line flag.

    Note that differing explicit usernames for the same hostname will result in
    multiple client connections being made. For example, specifying
    ``user1@example.com`` will create a connection to ``example.com``, logged
    in as ``user1``; later specifying ``user2@example.com`` will create a new,
    2nd connection as ``user2``.

    The same applies to ports: specifying two different ports will result in
    two different connections to the same host being made. If no port is given,
    22 is assumed, so ``example.com`` is equivalent to ``example.com:22``.
    c         C  s~   d d l  m } t |  \ } } } t |  } t } | j rX t | j  | k } n  t | | | d |  d | |  | <d S(   s@   
        Force a new connection to ``key`` host string.
        i(   R   R+   t   seek_gatewayN(   R   R   R&   R#   t   TrueR"   R%   (   t   selft   keyR   t   userR   R   R1   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR%      s    	c         C  s8   t  |  } | |  k r( |  j |  n  t j |  |  S(   s8   
        Autoconnect + return connection object
        (   R#   R%   R(   R)   (   R3   R4   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR)      s    c         C  s   t  j |  t |  |  S(   N(   R(   t   __setitem__R#   (   R3   R4   t   value(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR6      s    c         C  s   t  j |  t |   S(   N(   R(   t   __delitem__R#   (   R3   R4   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR8      s    c         C  s   t  j |  t |   S(   N(   R(   t   __contains__R#   (   R3   R4   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR9      s    (   t   __name__t
   __module__t   __doc__R%   R)   R6   R8   R9   (    (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR0   h   s   !				c         C  s   d d l  m } i  } | j s# | Sd | k r yP t j   } t j j | j  } t	 |   } | j
 |  | | _ Wd QXWq t k
 r t d |  | SXn  t |  p | j  d } | j j |  S(   s  
    Return ssh configuration dict for current env.host_string host value.

    Memoizes the loaded SSH config file, but not the specific per-host results.

    This function performs the necessary "is SSH config enabled?" checks and
    will simply return an empty dict if not. If SSH config *is* enabled and the
    value of env.ssh_config_path is not a valid file, it will abort.

    May give an explicit host string as ``host_string``.
    i(   R   t   _ssh_configNs#   Unable to load SSH config file '%s'R   (   R   R   t   use_ssh_configR   t	   SSHConfigt   ost   patht
   expandusert   ssh_config_patht   opent   parseR=   t   IOErrorR   t   parse_host_stringt   host_stringt   lookup(   RH   R   t   dummyt   confRA   t   fdR   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR       s     	c          C  s   d d l  m }  |  j } t |  j t  s: |  j d k rF | g } n  t t |  } t   } d | k r~ | j	 | d  n  t
 t j j |  S(   s   
    Returns list of SSH key filenames for the current env.host_string.

    Takes into account ssh_config and env.key_filename, including normalization
    to a list. Also performs ``os.path.expanduser`` expansion on any key
    filenames.
    i(   R   t   identityfileN(   R   R   t   key_filenamet
   isinstancet
   basestringR   t   filtert   boolR    t   extendt   mapR@   RA   RB   (   R   t   keysRK   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   key_filenames   s    	!	c         C  s   d d l  m } m } d | k r | j rE t j j d | j  n  x t j	 j
 t j j f D]k } | j r t j j d |  n  y | j t | j  |   SWq^ t k
 r } d | k r   q q^ Xq^ Wn  d S(   sJ   
    Returns a paramiko-ready key from a text string of a private key
    i(   R   R   R4   s!   Trying to honor in-memory key %r
s   Trying to load it as %s
s   Private key file is encryptedN(   R   R   R   R$   t   syst   stderrt   writeR4   R   t   rsakeyt   RSAKeyt   dsskeyt   DSSKeyt   from_private_keyR   t	   Exception(   t
   passphraseR   R   t
   pkey_classR   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   key_from_env   s    		c         C  s   |  j  d d  } | j   } | r8 | d r8 | d n d  } | j d  d k r t j |  j   } | d pu d  } | d p d  } nG | j  d d  } | j d  p d  } | r | d r | d n d  } i | d 6| d 6| d 6S(   Nt   @i   i    t   :R   R   R5   (   t   rsplitt   popR   t   countt
   ipv6_regext   matcht	   groupdict(   RH   t   user_hostportt   hostportR5   t   rR   R   t	   host_port(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyRG     s      c   	      C  s  d d l  m } |  s$ | r  d Sd	 St |   } | d } | j pI | j } t |   } | | j k r d | k r | d } n  d | k r | d } n  | d p | } | r | | f Sd
 } | j r | j d d
  } n  | d p | p | j	 p | j
 } | | | f S(   s7  
    Normalizes a given host string, returning explicit host, user, port.

    If ``omit_port`` is given and is True, only the host and user are returned.

    This function will process SSH config files if Fabric is configured to do
    so, and will use them to fill in some default values or swap in hostname
    aliases.

    Regarding SSH port used:

    * Ports explicitly given within host strings always win, no matter what.
    * When the host string lacks a port, SSH-config driven port configurations
      are used next.
    * When the SSH config doesn't specify a port (at all - including a default
      ``Host *`` block), Fabric's internal setting ``env.port`` is consulted.
    * If ``env.port`` is empty, ``env.default_port`` is checked (which should
      always be, as one would expect, port ``22``).
    i(   R   R
   R   R5   t   hostnameR   (   R
   R
   (   R
   R
   R
   N(   R   R   RG   R5   t
   local_userR    R   R>   R!   R   t   default_port(	   RH   t	   omit_portR   Rm   R   R5   RK   t   ssh_config_portR   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR&     s&    

	"c         C  s5   t  |   \ } } } i | d 6| d 6| d 6|  d 6S(   NR5   R   R   RH   (   R&   (   RH   R5   R   R   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   to_dictU  s    c         C  s   t  |  d |  d |  d  S(   NR5   R   R   (   t   join_host_strings(   t   arg(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt	   from_dict\  s    c         C  s   d d l  m } t |   } d } | d d k	 rV | d | j k rV | d d } n  d } | d d k	 r | d d k r d | d } n  | d	 } | r | j d  d
 k r d | n | } | | | S(   s   
    Strips out default values for the given host string.

    If the user part is the default user, it is removed;
    if the port is port 22, it also is removed.
    i(   R   R
   R5   Rc   R   t   22Rd   R   i   s   [%s]N(   R   R   RG   R   R5   Rg   (   RH   R   Rm   R5   R   R   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   denormalize`  s    # 
+c         C  sJ   | r8 | j  d  d k r! d n d } | |  | | f Sd |  | f Sd S(   st  
    Turns user/host/port strings into ``user@host:port`` combined string.

    This function is not responsible for handling missing user/port strings;
    for that, see the ``normalize`` function.

    If ``host`` looks like IPv6 address, it will be enclosed in square brackets

    If ``port`` is omitted, the returned string will be of the form
    ``user@host``.
    Rd   i   s
   %s@[%s]:%ss   %s@%s:%ss   %s@%sN(   Rg   (   R5   R   R   t   template(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyRu   u  s    !c         C  s   t  t |     S(   sN   
    normalize() returns a tuple; this returns another valid host string.
    (   Ru   R&   (   RH   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR#     s    c         C  s  d d l  m } m } t j   } | j d  } | rG | j |  n  | j s] | j   n  | j s| | j	 t j
    n  t }	 t |  | | d t }
 d } d$ } x|	 szy| d 7} | r t | | | d | d k } n  t d | d	 t |  d
 |  d |
 d t |
  d t   d | j d | j d | j d |  
} xH d% D]@ } d j |  } | j | d$  } | d$ k	 rL| | | <qLqLW| j |   t }	 | j r| j   j | j  n  | SWnt j k
 r} t d | |   n|t j t j t j  f k
 r} t! |  } | j" t j  k rd| d k rdt# |  r^t | |   n  w n  | j" t j  k r|
 s| j$ d  rt% |  rt | |   n  d$ } | j" t j k st% |  rd } | | j& } n  t' |  }
 t( |  | | |
  nkt) t* f k
 r.d GHt+ j, d  nCt- j. k
 rV} t d | |   nt- j/ k
 rp} t0 |  t- j k	 } t# |  } d | } | d | | j1 f } | r| d 7} n  | d 7} | j2 rt+ j3 j4 | d   n  | s| rt5 j6 | j  n  w n  | r,d! | | | d f } n  | j1 d k rAd" n d } | d# | j1 | f 7} t | |   n XWd$ |	 r| d$ k	 r| j7   n  Xq Wd$ S(&   s  
    Create and return a new SSHClient instance connected to given host.

    :param user: Username to connect as.

    :param host: Network hostname.

    :param port: SSH daemon port.

    :param cache:
        A ``HostConnectionCache`` instance used to cache/store gateway hosts
        when gatewaying is enabled.

    :param seek_gateway:
        Whether to try setting up a gateway socket for this connection. Used so
        the actual gateway connection can prevent recursion.
    i(   R   R   t   system_known_hostst
   login_onlyi    i   R,   Ro   R   t   usernamet   passwordt   pkeyRN   t   timeoutt   allow_agentt   look_for_keysR-   t   autht   deleg_credst   kexs   gss_{0}sx   Host key for %s did not match pre-existing key! Server's key was changed recently, or possible man-in-the-middle attack.s!   Error reading SSH protocol banners   Unknown servers   [%s] Passphrase for private keyR
   s   Name lookup failed for %ss!   Timed out trying to connect to %ss    (attempt %s of %s)s   , giving upt   )s   
s;   Low level socket error connecting to host %s on port %s: %st   ss    (tried %s time%s)N(   R   R   R   (8   R   R   R   R   t	   SSHClientR!   t   load_system_host_keyst   disable_known_hostst   reject_unknown_hostst   set_missing_host_key_policyt   AutoAddPolicyR'   R   R2   R   R/   R(   R   Rb   RV   R   t   no_agentt   no_keyst   formatR%   t	   keepaliveR   t   set_keepalivet   BadHostKeyExceptionR   t   AuthenticationExceptiont   PasswordRequiredExceptionR   R   R   R   t
   startswithR   RH   t   prompt_for_passwordR   t   EOFErrort	   TypeErrorRW   t   exitt   sockett   gaierrort   errort   typeR   R$   RX   RY   t   timet   sleept   close(   R5   R   R   R+   R1   R   R   R   t   known_hostst	   connectedR~   R   R-   t   kwargst   suffixt   namet   valR   t   msgt   textt   promptt   not_timeoutt	   giving_upt   errR   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR%     s    			 
!		

		

	c         C  s   t  j  |  j d d  |  S(   Nt   asciiR	   (   t   getpasst   encode(   R   t   stream(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   _password_promptc  s    c         C  s   d d l  m } t d  | p& t j } d | j | j f } |  d k	 rQ |  n | } | sj | d 7} n  t | |  } x | s d GHt | |  } q| W| S(   s#  
    Prompts for and returns a new password if required; otherwise, returns
    None.

    A trailing colon is appended unless ``no_colon`` is True.

    If the user supplies an empty password, the user will be re-prompted until
    they enter a non-empty password.

    ``prompt_for_password`` autogenerates the user prompt based on the current
    host being connected to. To override this, specify a string value for
    ``prompt``.

    ``stream`` is the stream the prompt will be printed to; if not given,
    defaults to ``sys.stderr``.
    i(   R   s   a connection or sudo passwords   [%s] Login password for '%s's   : s;   Sorry, you can't enter an empty password. Please try again.N(	   R   R   R   RW   RX   RH   R5   R   R   (   R   t   no_colonR   R   t   defaultt   password_promptt   new_password(    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyR   h  s    
	c           s;   d d l  m   t      f d    }  | _ | S(   s  
    Prompt user for value of ``env.host_string`` when ``env.host_string`` is
    empty.

    This decorator is basically a safety net for silly users who forgot to
    specify the host/host list in one way or another. It should be used to wrap
    operations which require a network connection.

    Due to how we execute commands per-host in ``main()``, it's not possible to
    specify multiple hosts at this point in time, so only a single host will be
    prompted for.

    Because this decorator sets ``env.host_string``, it will prompt once (and
    only once) per command. As ``main()`` clears ``env.host_string`` between
    commands, this decorator will also end up prompting the user once per
    command (in the case where multiple commands have no hosts set, of course.)
    i(   R   c            sO   x?   j  d t  sA t d  t d  }   j t |   q W |  |   S(   NRH   s!   the target host connection stringsD   No hosts found. Please specify (single) host string for connection: (   R!   R'   R   t	   raw_inputt   updateRt   (   t   argsR   RH   (   R   t   func(    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   host_prompting_wrapper  s
    
(   R   R   R   t   undecorated(   R   R   (    (   R   R   s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt
   needs_host  s    	c          C  s   d d l  m }  m } xk |  j   D]] } | j rO t j j d t |   n  |  | j	   |  | =| j r# t j j d  q# q# Wd S(   s   
    Disconnect from all currently connected servers.

    Used at the end of ``fab``'s main loop, and also intended for use by
    library users.
    i(   t   connectionsR   s   Disconnecting from %s... s   done.
N(
   R   R   R   RU   t   statusRW   t   stdoutRY   Ry   R   (   R   R   R4   (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   disconnect_all  s    		(=   R<   t
   __future__R    t	   functoolsR   R   R@   t   reR   R   RW   R   t   fabric.authR   R   t   fabric.utilsR   R   R   t   fabric.exceptionsR   t   warningst   simplefiltert   DeprecationWarningt   paramikoR   t   ImportErrorR   t	   tracebackt	   print_exct   rstripR   RX   RY   R   t   compileRh   R   R   R   R'   R/   R(   R0   R   R    RV   Rb   RG   R&   Rt   Rw   Ry   Ru   R#   R2   R%   R   R   R   R   (    (    (    s\   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/network.pyt   <module>   sX   
				,H		>					#	