ó
:ôÕFc           @   sd   d  Z  d d l Td d l Z d d l Z d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s¿   This class extends pexpect.spawn to specialize setting up SSH connections.
This adds methods for login, logout, and expecting the shell prompt.

$Id: pxssh.py 487 2007-08-29 22:33:29Z noah $
iÿÿÿÿ(   t   *Nt   ExceptionPxssht   pxsshc           B   s   e  Z d  Z RS(   s!   Raised for pxssh exceptions.
    (   t   __name__t
   __module__t   __doc__(    (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyR      s   c           B   st   e  Z d  Z d d d d d d d „ Z d „  Z d „  Z d d d d	 d e d
 „ Z d „  Z	 d d „ Z
 d „  Z RS(   sƒ	  This class extends pexpect.spawn to specialize setting up SSH
    connections. This adds methods for login, logout, and expecting the shell
    prompt. It does various tricky things to handle many situations in the SSH
    login process. For example, if the session is your first login, then pxssh
    automatically accepts the remote certificate; or if you have public key
    authentication setup then pxssh won't wait for the password prompt.

    pxssh uses the shell prompt to synchronize output from the remote host. In
    order to make this more robust it sets the shell prompt to something more
    unique than just $ or #. This should work on most Borne/Bash or Csh style
    shells.

    Example that runs a few commands on a remote server and prints the result::
        
        import pxssh
        import getpass
        try:                                                            
            s = pxssh.pxssh()
            hostname = raw_input('hostname: ')
            username = raw_input('username: ')
            password = getpass.getpass('password: ')
            s.login (hostname, username, password)
            s.sendline ('uptime')  # run a command
            s.prompt()             # match the prompt
            print s.before         # print everything before the prompt.
            s.sendline ('ls -l')
            s.prompt()
            print s.before
            s.sendline ('df')
            s.prompt()
            print s.before
            s.logout()
        except pxssh.ExceptionPxssh, e:
            print "pxssh failed on login."
            print str(e)

    Note that if you have ssh-agent running while doing development with pxssh
    then this can lead to a lot of confusion. Many X display managers (xdm,
    gdm, kdm, etc.) will automatically start a GUI agent. You may see a GUI
    dialog box popup asking for a password during development. You should turn
    off any key agents during testing. The 'force_password' attribute will turn
    off public key authentication. This will only work if the remote SSH server
    is configured to allow password logins. Example of using 'force_password'
    attribute::

            s = pxssh.pxssh()
            s.force_password = True
            hostname = raw_input('hostname: ')
            username = raw_input('username: ')
            password = getpass.getpass('password: ')
            s.login (hostname, username, password)
    i   iÐ  c         C   sƒ   t  j |  d  d | d | d | d | d | d | ƒd |  _ d |  _ |  j |  _ d	 |  _ d
 |  _ d |  _ t	 |  _
 t |  _ d  S(   Nt   timeoutt   maxreadt   searchwindowsizet   logfilet   cwdt   envs   <pxssh>s   \[PEXPECT\][\$\#] s   PS1='[PEXPECT]\$ 's   set prompt='[PEXPECT]\$ 's5   -o'RSAAuthentication=no' -o 'PubkeyAuthentication=no'(   t   spawnt   __init__t   Nonet   namet   UNIQUE_PROMPTt   PROMPTt   PROMPT_SET_SHt   PROMPT_SET_CSHt   SSH_OPTSt   Falset   force_passwordt   Truet   auto_prompt_reset(   t   selfR   R   R   R	   R
   R   (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyR   I   s    4						c         C   s  t  | ƒ t  | ƒ } } | | k rB | | } } | | } } n  t | d ƒ } xÂ t d | d ƒ D]­ } | | g d g | } } x‰ t d | d ƒ D]t } | | d | | d d }	 }
 | | d } | | d | | d k rù | d } n  t |	 |
 | ƒ | | <q› Wqf W| | S(   sB   This calculates the Levenshtein distance between a and b.
        i   i    (   t   lent   ranget   min(   R   t   at   bt   nt   mt   currentt   it   previoust   jt   addt   deletet   change(    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyt   levenshtein_distanceg   s    !c         C   s  |  j  d d d d ƒ t j d ƒ |  j ƒ  t j d ƒ |  j  d d d d ƒ } t j d ƒ |  j ƒ  t j d ƒ |  j  d d d d ƒ } t j d ƒ |  j ƒ  t j d ƒ |  j  d d d d ƒ } |  j | | ƒ } t | ƒ } | d k rø t St | ƒ | d	 k  rt St S(
   sÙ   This attempts to find the prompt. Basically, press enter and record
        the response; press enter again and record the response; if the two
        responses are similar then assume we are at the original prompt. t   sizei'  R   i   gš™™™™™¹?g      à?iè  i    gš™™™™™Ù?(	   t   read_nonblockingt   timet   sleept   sendlineR(   R   R   t   floatR   (   R   t   xR   R   t   ldt   len_a(    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyt   synch_original_prompt{   s(    


t    t   ansis   [#$]i
   c	         C   s•  d }	 |  j  r# |	 d |  j }	 n  | d k	 rF |	 d t | ƒ }	 n  d |	 | | f }
 t j |  |
 ƒ |  j d | d d d t d	 g d
 | ƒ} | d k rÐ |  j d ƒ |  j d | d d d t g ƒ } n  | d k r|  j | ƒ |  j d | d d d t g ƒ } n  | d k rJ|  j | ƒ |  j d | d d d t g ƒ } n  | d k ro|  j	 ƒ  t
 d ƒ ‚ nÈ | d k r~n¹ | d k r£|  j	 ƒ  t
 d ƒ ‚ n” | d k rÈ|  j	 ƒ  t
 d ƒ ‚ no | d k rí|  j	 ƒ  t
 d ƒ ‚ nJ | d k rün; | d k r!|  j	 ƒ  t
 d ƒ ‚ n |  j	 ƒ  t
 d ƒ ‚ |  j ƒ  s\|  j	 ƒ  t
 d ƒ ‚ n  | r‘|  j ƒ  s‘|  j	 ƒ  t
 d |  j ƒ ‚ q‘n  t S(   s  This logs the user into the given server. It uses the
        'original_prompt' to try to find the prompt right after login. When it
        finds the prompt it immediately tries to reset the prompt to something
        more easily matched. The default 'original_prompt' is very optimistic
        and is easily fooled. It's more reliable to try to match the original
        prompt as exactly as possible to prevent false matches by server
        strings such as the "Message Of The Day". On many systems you can
        disable the MOTD on the remote server by creating a zero-length file
        called "~/.hushlogin" on the remote server. If a prompt cannot be found
        then this will not necessarily cause the login to fail. In the case of
        a timeout when looking for the prompt we assume that the original
        prompt was so weird that we could not match it, so we use a few tricks
        to guess when we have reached the prompt. Then we hope for the best and
        blindly try to reset the prompt to something more unique. If that fails
        then login() raises an ExceptionPxssh exception.
        
        In some situations it is not possible or desirable to reset the
        original prompt. In this case, set 'auto_prompt_reset' to False to
        inhibit setting the prompt to the UNIQUE_PROMPT. Remember that pxssh
        uses a unique prompt in the prompt() method. If the original prompt is
        not reset then this will disable the prompt() method unless you
        manually set the PROMPT attribute. s   -qt    s    -p %ss   ssh %s -l %s %ss0   (?i)are you sure you want to continue connectings'   (?i)(?:password)|(?:passphrase for key)s   (?i)permission denieds   (?i)terminal types$   (?i)connection closed by remote hostR   i    t   yesi   i   s-   Weird error. Got "are you sure" prompt twice.i   s   password refusedi   s   permission denieds.   Weird error. Got "terminal type" prompt twice.i   i   s   connection closeds   unexpected login responses*   could not synchronize with original prompts   could not set shell prompt
N(   R   R   R   t   strR   t   _spawnt   expectt   TIMEOUTR-   t   closeR   R2   t   set_unique_promptt   beforeR   (   R   t   servert   usernamet   passwordt   terminal_typet   original_promptt   login_timeoutt   portR   t   ssh_optionst   cmdR"   (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyt   login   s\    	*$$$







c         C   sY   |  j  d ƒ |  j t d g ƒ } | d k rK |  j  d ƒ |  j t ƒ n  |  j ƒ  d S(   sq   This sends exit to the remote shell. If there are stopped jobs then
        this automatically sends exit twice. t   exits   (?i)there are stopped jobsi   N(   R-   R9   t   EOFR;   (   R   t   index(    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyt   logoutþ   s    i   c         C   s2   |  j  |  j t g d | ƒ} | d k r. t St S(   s‰  This matches the shell prompt. This is little more than a short-cut
        to the expect() method. This returns True if the shell prompt was
        matched. This returns False if there was a timeout. Note that if you
        called login() with auto_prompt_reset set to False then you should have
        manually set the PROMPT attribute to a regex pattern for matching the
        prompt. R   i   (   R9   R   R:   R   R   (   R   R   R"   (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyt   prompt
  s    	c         C   sŒ   |  j  d ƒ |  j  |  j ƒ |  j t |  j g d d ƒ} | d k rˆ |  j  |  j ƒ |  j t |  j g d d ƒ} | d k rˆ t Sn  t S(   s~  This sets the remote prompt to something more unique than # or $.
        This makes it easier for the prompt() method to match the shell prompt
        unambiguously. This method is called automatically by the login()
        method, but you may want to call it manually if you somehow reset the
        shell prompt. For example, if you 'su' to a different user then you
        will need to manually reset the prompt. This sends shell commands to
        the remote host to set the prompt, so this assumes the remote host is
        ready to receive commands.

        Alternatively, you may use your own prompt pattern. Just set the PROMPT
        attribute to a regular expression that matches it. In this case you
        should call login() with auto_prompt_reset=False; then set the PROMPT
        attribute. After that the prompt() method will try to match your prompt
        pattern.s   unset PROMPT_COMMANDR   i
   i    (   R-   R   R9   R:   R   R   R   R   (   R   R"   (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyR<     s    N(   R   R   R   R   R   R(   R2   R   RG   RK   RL   R<   (    (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyR      s   5		"a	(   R   t   pexpectR+   t   __all__t   ExceptionPexpectR   R   R   (    (    (    s)   /usr/lib/python2.7/dist-packages/pxssh.pyt   <module>   s   
