Ñò
fúíJc           @   s>  d  Z  d d k l Z d d k Z d d k Z d d k Z d d k Z d d k l Z d d k	 Td d k
 l Z d d k l Z d d k l Z d d	 k l Z d d
 k l Z l Z d d k l Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s   
L{SSHClient}.
iÿÿÿÿ(   t   hexlifyN(   t   Agent(   t   *(   t   DSSKey(   t   HostKeys(   t   ResourceManager(   t   RSAKey(   t   SSHExceptiont   BadHostKeyException(   t	   Transporti   t   MissingHostKeyPolicyc           B   s   e  Z d  Z d „  Z RS(   sÄ  
    Interface for defining the policy that L{SSHClient} should use when the
    SSH server's hostname is not in either the system host keys or the
    application's keys.  Pre-made classes implement policies for automatically
    adding the key to the application's L{HostKeys} object (L{AutoAddPolicy}),
    and for automatically rejecting the key (L{RejectPolicy}).

    This function may be used to ask the user to verify the key, for example.
    c         C   s   d S(   s  
        Called when an L{SSHClient} receives a server key for a server that
        isn't in either the system or local L{HostKeys} object.  To accept
        the key, simply return.  To reject, raised an exception (which will
        be passed to the calling application).
        N(    (   t   selft   clientt   hostnamet   key(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   missing_host_key4   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR
   )   s   	t   AutoAddPolicyc           B   s   e  Z d  Z d „  Z RS(   sš   
    Policy for automatically adding the hostname and new host key to the
    local L{HostKeys} object, and saving it.  This is used by L{SSHClient}.
    c         C   ss   | i  i | | i ƒ  | ƒ | i d  j	 o | i | i ƒ n | i t d | i ƒ  | t | i	 ƒ  ƒ f ƒ d  S(   Ns   Adding %s host key for %s: %s(
   t
   _host_keyst   addt   get_namet   _host_keys_filenamet   Nonet   save_host_keyst   _logt   DEBUGR    t   get_fingerprint(   R   R   R   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   D   s
    (   R   R   R   R   (    (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   >   s   t   RejectPolicyc           B   s   e  Z d  Z d „  Z RS(   sk   
    Policy for automatically rejecting the unknown hostname & key.  This is
    used by L{SSHClient}.
    c         C   sC   | i  t d | i ƒ  | t | i ƒ  ƒ f ƒ t d | ƒ ‚ d  S(   Ns    Rejecting %s host key for %s: %ss   Unknown server %s(   R   R   R   R    R   R   (   R   R   R   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   R   s    #(   R   R   R   R   (    (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   L   s   t   WarningPolicyc           B   s   e  Z d  Z d „  Z RS(   s€   
    Policy for logging a python-style warning for an unknown host key, but
    accepting it. This is used by L{SSHClient}.
    c         C   s0   t  i d | i ƒ  | t | i ƒ  ƒ f ƒ d  S(   Ns   Unknown %s host key for %s: %s(   t   warningst   warnR   R    R   (   R   R   R   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   ]   s    	(   R   R   R   R   (    (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   X   s   t	   SSHClientc        	   B   s¼   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 e d d d d d e e d „ Z d	 „  Z d
 d „ Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   se  
    A high-level representation of a session with an SSH server.  This class
    wraps L{Transport}, L{Channel}, and L{SFTPClient} to take care of most
    aspects of authenticating and opening channels.  A typical use case is::

        client = SSHClient()
        client.load_system_host_keys()
        client.connect('ssh.example.com')
        stdin, stdout, stderr = client.exec_command('ls -l')

    You may pass in explicit overrides for authentication and server host key
    checking.  The default mechanism is to try to use local key files or an
    SSH agent (if one is running).

    @since: 1.6
    c         C   sC   t  ƒ  |  _ t  ƒ  |  _ d |  _ d |  _ t ƒ  |  _ d |  _ d S(   s)   
        Create a new SSHClient.
        N(	   R   t   _system_host_keysR   R   R   t   _log_channelR   t   _policyt
   _transport(   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   __init__t   s    		c         C   sb   | d j oA t i i d ƒ } y |  i i | ƒ Wn t j
 o n Xd S|  i i | ƒ d S(   s  
        Load host keys from a system (read-only) file.  Host keys read with
        this method will not be saved back by L{save_host_keys}.

        This method can be called multiple times.  Each new set of host keys
        will be merged with the existing set (new replacing old if there are
        conflicts).

        If C{filename} is left as C{None}, an attempt will be made to read
        keys from the user's local "known hosts" file, as used by OpenSSH,
        and no exception will be raised if the file can't be read.  This is
        probably only useful on posix.

        @param filename: the filename to read, or C{None}
        @type filename: str

        @raise IOError: if a filename was provided and the file could not be
            read
        s   ~/.ssh/known_hostsN(   R   t   ost   patht
   expanduserR"   t   loadt   IOError(   R   t   filename(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   load_system_host_keys   s    c         C   s   | |  _  |  i i | ƒ d S(   sö  
        Load host keys from a local host-key file.  Host keys read with this
        method will be checked I{after} keys loaded via L{load_system_host_keys},
        but will be saved back by L{save_host_keys} (so they can be modified).
        The missing host key policy L{AutoAddPolicy} adds keys to this set and
        saves them, when connecting to a previously-unknown server.

        This method can be called multiple times.  Each new set of host keys
        will be merged with the existing set (new replacing old if there are
        conflicts).  When automatically saving, the last hostname is used.

        @param filename: the filename to read
        @type filename: str

        @raise IOError: if the filename could not be read
        N(   R   R   R*   (   R   R,   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   load_host_keys   s    	c         C   s‡   t  | d ƒ } | i d ƒ xZ |  i i ƒ  D]I \ } } x: | i ƒ  D], \ } } | i d | | | i ƒ  f ƒ qE Wq, W| i ƒ  d S(   sa  
        Save the host keys back to a file.  Only the host keys loaded with
        L{load_host_keys} (plus any added directly) will be saved -- not any
        host keys loaded with L{load_system_host_keys}.

        @param filename: the filename to save to
        @type filename: str

        @raise IOError: if the file could not be written
        t   ws&   # SSH host keys collected by paramiko
s	   %s %s %s
N(   t   opent   writeR   t	   iteritemst
   get_base64t   close(   R   R,   t   fR   t   keyst   keytypeR   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   ±   s      (c         C   s   |  i  S(   s¾   
        Get the local L{HostKeys} object.  This can be used to examine the
        local host keys or change them.

        @return: the local host keys
        @rtype: L{HostKeys}
        (   R   (   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   get_host_keysÃ   s    c         C   s   | |  _  d S(   sÑ   
        Set the channel for logging.  The default is C{"paramiko.transport"}
        but it can be set to anything you want.

        @param name: new channel name for logging
        @type name: str
        N(   R#   (   R   t   name(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   set_log_channelÍ   s    c         C   s   | |  _  d S(   sÜ  
        Set the policy to use when connecting to a server that doesn't have a
        host key in either the system or local L{HostKeys} objects.  The
        default policy is to reject all unknown servers (using L{RejectPolicy}).
        You may substitute L{AutoAddPolicy} or write your own policy class.

        @param policy: the policy to use when receiving a host key from a
            previously-unknown server
        @type policy: L{MissingHostKeyPolicy}
        N(   R$   (   R   t   policy(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   set_missing_host_key_policy×   s    c
         C   sg  xf t  i | | t  i t  i ƒ D]6 \ }
 } } } } | t  i j o |
 } | } Pq q Wt d | ƒ ‚ t  i  | t  i ƒ } | d j	 o y | i | ƒ Wqª qª Xn | i | ƒ t | ƒ } |  _	 |  i
 d j	 o | i |  i
 ƒ n | i ƒ  t i |  | ƒ | i ƒ  } | i ƒ  } | t j o
 | } n d | | f } |  i i | h  ƒ i | d ƒ } | d j o% |  i i | h  ƒ i | d ƒ } n | d j o  |  i i |  | | ƒ | } n | | j o t | | | ƒ ‚ n | d j o t i ƒ  } n | d j o
 g  } n* t | t t f ƒ o | g } n | } |  i | | | | | |	 ƒ d S(   s<	  
        Connect to an SSH server and authenticate to it.  The server's host key
        is checked against the system host keys (see L{load_system_host_keys})
        and any local host keys (L{load_host_keys}).  If the server's hostname
        is not found in either set of host keys, the missing host key policy
        is used (see L{set_missing_host_key_policy}).  The default policy is
        to reject the key and raise an L{SSHException}.

        Authentication is attempted in the following order of priority:

            - The C{pkey} or C{key_filename} passed in (if any)
            - Any key we can find through an SSH agent
            - Any "id_rsa" or "id_dsa" key discoverable in C{~/.ssh/}
            - Plain username/password auth, if a password was given

        If a private key requires a password to unlock it, and a password is
        passed in, that password will be used to attempt to unlock the key.

        @param hostname: the server to connect to
        @type hostname: str
        @param port: the server port to connect to
        @type port: int
        @param username: the username to authenticate as (defaults to the
            current local username)
        @type username: str
        @param password: a password to use for authentication or for unlocking
            a private key
        @type password: str
        @param pkey: an optional private key to use for authentication
        @type pkey: L{PKey}
        @param key_filename: the filename, or list of filenames, of optional
            private key(s) to try for authentication
        @type key_filename: str or list(str)
        @param timeout: an optional timeout (in seconds) for the TCP connect
        @type timeout: float
        @param allow_agent: set to False to disable connecting to the SSH agent
        @type allow_agent: bool
        @param look_for_keys: set to False to disable searching for discoverable
            private key files in C{~/.ssh/}
        @type look_for_keys: bool

        @raise BadHostKeyException: if the server's host key could not be
            verified
        @raise AuthenticationException: if authentication failed
        @raise SSHException: if there was any other error connecting or
            establishing an SSH session
        @raise socket.error: if a socket error occurred while connecting
        s!   No suitable address family for %ss   [%s]:%dN(   t   sockett   getaddrinfot	   AF_UNSPECt   SOCK_STREAMR   R   t
   settimeoutt   connectR	   R%   R#   R:   t   start_clientR   t   registert   get_remote_server_keyR   t   SSH_PORTR"   t   getR   R$   R   R   t   getpasst   getusert
   isinstancet   strt   unicodet   _auth(   R   R   t   portt   usernamet   passwordt   pkeyt   key_filenamet   timeoutt   allow_agentt   look_for_keyst   familyt   socktypet   protot	   canonnamet   sockaddrt   aft   addrt   sockt   tt
   server_keyR7   t   server_hostkey_namet   our_server_keyt   key_filenames(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyRB   ä   sP    2 	

!%

c         C   s/   |  i  d j o d S|  i  i ƒ  d |  _  d S(   sG   
        Close this SSHClient and its underlying L{Transport}.
        N(   R%   R   R4   (   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR4   I  s    iÿÿÿÿc         C   s_   |  i  i ƒ  } | i | ƒ | i d | ƒ } | i d | ƒ } | i d | ƒ } | | | f S(   s«  
        Execute a command on the SSH server.  A new L{Channel} is opened and
        the requested command is executed.  The command's input and output
        streams are returned as python C{file}-like objects representing
        stdin, stdout, and stderr.

        @param command: the command to execute
        @type command: str
        @param bufsize: interpreted the same way as by the built-in C{file()} function in python
        @type bufsize: int
        @return: the stdin, stdout, and stderr of the executing command
        @rtype: tuple(L{ChannelFile}, L{ChannelFile}, L{ChannelFile})

        @raise SSHException: if the server fails to execute the command
        t   wbt   rb(   R%   t   open_sessiont   exec_commandt   makefilet   makefile_stderr(   R   t   commandt   bufsizet   chant   stdint   stdoutt   stderr(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyRf   R  s    t   vt100iP   i   c         C   s0   |  i  i ƒ  } | i | | | ƒ | i ƒ  | S(   s†  
        Start an interactive shell session on the SSH server.  A new L{Channel}
        is opened and connected to a pseudo-terminal using the requested
        terminal type and size.

        @param term: the terminal type to emulate (for example, C{"vt100"})
        @type term: str
        @param width: the width (in characters) of the terminal window
        @type width: int
        @param height: the height (in characters) of the terminal window
        @type height: int
        @return: a new channel connected to the remote shell
        @rtype: L{Channel}

        @raise SSHException: if the server fails to invoke a shell
        (   R%   Re   t   get_ptyt   invoke_shell(   R   t   termt   widtht   heightRk   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyRq   i  s    
c         C   s   |  i  i ƒ  S(   sƒ   
        Open an SFTP session on the SSH server.

        @return: a new SFTP session object
        @rtype: L{SFTPClient}
        (   R%   t   open_sftp_client(   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt	   open_sftp  s    c         C   s   |  i  S(   s  
        Return the underlying L{Transport} object for this SSH connection.
        This can be used to perform lower-level tasks, like opening specific
        kinds of channels.

        @return: the Transport for this connection
        @rtype: L{Transport}
        (   R%   (   R   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   get_transportˆ  s    	c         C   s¡  d } | d j	 o] y; |  i t d t | i ƒ  ƒ ƒ |  i i | | ƒ d SWqp t j
 o } | } qp Xn x– | D]Ž }	 x… t t	 f D]w }
 yS |
 i
 |	 | ƒ } |  i t d t | i ƒ  ƒ |	 f ƒ |  i i | | ƒ d SWqŠ t j
 o } | } qŠ XqŠ Wqw W| ow xt t ƒ  i ƒ  D]_ } y; |  i t d t | i ƒ  ƒ ƒ |  i i | | ƒ d SWq t j
 o } | } q Xq Wn g  } t i i d ƒ } t i i d ƒ } t i i | ƒ o | i t | f ƒ n t i i | ƒ o | i t	 | f ƒ n t i i d ƒ } t i i d ƒ } t i i | ƒ o | i t | f ƒ n t i i | ƒ o | i t	 | f ƒ n | p
 g  } n xŸ | D]— \ }
 } yS |
 i
 | | ƒ } |  i t d	 t | i ƒ  ƒ | f ƒ |  i i | | ƒ d SWq•t j
 o } | } q•t j
 o } | } q•Xq•W| d j	 o= y |  i i | | ƒ d SWqzt j
 o } | } qzXn | d j	 o
 | ‚ n t d
 ƒ ‚ d S(   st  
        Try, in order:

            - The key passed in, if one was passed in.
            - Any key we can find through an SSH agent (if allowed).
            - Any "id_rsa" or "id_dsa" key discoverable in ~/.ssh/ (if allowed).
            - Plain username/password auth, if a password was given.

        (The password might be needed to unlock a private key.)
        s   Trying SSH key %sNs   Trying key %s from %ss   Trying SSH agent key %ss   ~/.ssh/id_rsas   ~/.ssh/id_dsas   ~/ssh/id_rsas   ~/ssh/id_dsas   Trying discovered key %s in %ss#   No authentication methods available(   R   R   R   R    R   R%   t   auth_publickeyR   R   R   t   from_private_key_fileR   t   get_keysR'   R(   R)   t   isfilet   appendR+   t   auth_password(   R   RO   RP   RQ   Rb   RT   RU   t   saved_exceptiont   eRR   t
   pkey_classR   t   keyfilest   rsa_keyt   dsa_keyR,   (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyRM   “  s~       &  
 &

c         C   s   |  i  i | | ƒ d  S(   N(   R%   R   (   R   t   levelt   msg(    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR   ä  s    N(   R   R   R   R&   R   R-   R.   R   R8   R:   R<   RF   t   TrueRB   R4   Rf   Rq   Rv   Rw   RM   R   (    (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyR!   b   s"   				
	
	d						Q(   R   t   binasciiR    RH   R'   R=   R   t   paramiko.agentR   t   paramiko.commont   paramiko.dsskeyR   t   paramiko.hostkeysR   t   paramiko.resourceR   t   paramiko.rsakeyR   t   paramiko.ssh_exceptionR   R   t   paramiko.transportR	   RF   t   objectR
   R   R   R   R!   (    (    (    sN   C:\Documents and Settings\red08xgu\Desktop\python_webupdate\paramiko\client.pyt   <module>   s&   

