Ñò
ýÉªWc        	   @   sƒ   d  d k  Z  d  d k Z d  d k Z d  d k Z d  d k Z d  d k Z d e f d „  ƒ  YZ d „  Z e	 d j o e ƒ  n d S(   iÿÿÿÿNt   SSHCommanderc           B   sw   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z
 d	 „  Z d
 „  Z d „  Z d „  Z RS(   s
   class docsc         C   sÔ   | |  _  | |  _ | |  _ d |  _ d  |  _ d |  _ t i d ƒ |  _	 |  i	 i
 t i ƒ d } t i | ƒ } t i ƒ  } | i | ƒ |  i	 i | ƒ |  i	 i |  _ |  i	 i |  _ |  i	 i |  _ |  i ƒ  d  S(   Ni   i   R    s<   %(asctime)s SSHCommander:%(funcName)s:%(lineno)d %(message)s(   t	   _usernamet	   _passwordt   _hostt   _portt   Nonet	   transportt   bufsizet   loggingt	   getLoggert   loggert   setLevelt   INFOt	   Formattert   StreamHandlert   setFormattert
   addHandlert   infot   debugt   errort   connect(   t   selft   serveript   usernamet   passwordt   fmtt   formatert   handler(    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   __init__   s"    						c         C   s.   |  i  d  j	 o |  i  i ƒ  d  |  _  n d  S(   N(   R   R   t   close(   R   (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   __del__&   s    c         C   s+   |  i  ƒ  p |  i i ƒ  d  |  _ n d  S(   N(   t   isClosedt   clientR   R   (   R   (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   logout+   s    c         C   sÁ   t  i ƒ  |  _ |  i i ƒ  |  i i t  i ƒ  ƒ yK |  i i |  i |  i |  i	 |  i
 ƒ |  i i ƒ  |  _ |  i i t ƒ Wn= t i j
 o d |  _ n  t  i j
 o d |  _ n Xd S(   s   Try to connect, maybe again.N(   t   paramikot	   SSHClientR    t   load_system_host_keyst   set_missing_host_key_policyt   AutoAddPolicyR   R   R   R   R   t   get_transportR   t   use_compressiont   Truet   socketR   R   t   BadAuthenticationType(   R   (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyR   0   s    %c         C   s`   |  i  d j o t S|  i  i ƒ  } | d j p | i ƒ  p | i ƒ  o |  i ƒ  t St S(   s.   Try to judge whether connect is closed or not.N(   R    R   R)   R'   t	   is_activeR!   t   False(   R   R   (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyR   B   s    )
c         C   s¡   |  i  d | ƒ |  i i | ƒ \ } } } | i ƒ  } x% | o |  i | ƒ | i ƒ  } q; W| i ƒ  } x% | o |  i | ƒ | i ƒ  } qo W| i i ƒ  S(   Ns    Start to execute SSH command: %s(   R   R    t   exec_commandt   readlineR   t   readt   channelt   recv_exit_status(   R   t   cmdt	   ssh_stdint
   ssh_stdoutt
   ssh_stderrt   linestr(    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   executeN   s      i   c         C   sŒ   |  i  d  j o d d | f S|  i | ƒ } |  i  i ƒ  } | i t ƒ | i ƒ  | i | ƒ |  i | | | ƒ } | i	 ƒ  } | | f S(   Niÿÿÿÿs   SSH command: %s executed failed(
   R   R   t   _run_fix_input_datat   open_sessiont   set_combine_stderrR)   t   get_ptyR.   t	   _run_pollR2   (   R   R3   t
   input_datat   timeoutt   sessiont   outputt   status(    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   execute_interactive[   s    
c         C   sk   |  i  d i d | d | ƒ ƒ |  i i ƒ  } | i | | ƒ | i ƒ  |  i  d i d | d | ƒ ƒ d  S(   Ns&   SFTP Get: {remotepath} {localpath} ...t
   remotepatht	   localpaths(   SFTP Get: {remotepath} {localpath} done.(   R   t   formatR    t	   open_sftpt   getR   (   R   RD   RE   t   sftp(    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   executeSftpGetl   s
    
c         C   sk   |  i  d i d | d | ƒ ƒ |  i i ƒ  } | i | | ƒ | i ƒ  |  i  d i d | d | ƒ ƒ d  S(   Ns&   SFTP Put: {localpath} {remotepath} ...RE   RD   s'   SFTP Put: {localpath} {remotepath} done(   R   RF   R    RG   t   putR   (   R   RE   RD   RI   (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   executeSftpPuts   s
    
c         C   se   | d j	 oT t | ƒ d j o3 d | j o" | i d ƒ } d i | ƒ } qS n | i d ƒ Sg  S(   s­   
        Fix the input data supplied by the user for a command.

        @param input_data  The input data (default is None).
        @returns the fixed input data.
        i    s   \ns   
N(   R   t   lent   splitt   join(   R   R>   t   lines(    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyR9   z   s    c         C   s  d } | } | | } d } t  } t i i ƒ  }	 t i |	 i ƒ  ƒ }
 d } | i d ƒ xt o| i ƒ  o | i	 |  i
 ƒ } | | 7} |  i d t | ƒ t | ƒ f ƒ | i ƒ  o@ | t | ƒ j  o) | | d } | d 7} | i | ƒ qü q n | i ƒ  o Pn t i i ƒ  } t i | i ƒ  ƒ } | |
 } | | j o |  i d ƒ t } Pn t i d ƒ q\ W|  i d	 ƒ | i ƒ  oC | i	 |  i
 ƒ } | | 7} |  i d t | ƒ t | ƒ f ƒ n |  i d
 t | ƒ ƒ | o) |  i d ƒ | d | 7} | i ƒ  n | S(   sÖ   
        Poll until the command completes.

        @param session     The session.
        @param timeout     The timeout in seconds.
        @param input_data  The input data.
        @returns the output
        gš™™™™™¹?i    t    s   read %d bytes, total %ds   
i   s   polling finished - timeoutgš™™™™™É?s   polling loop endeds"   polling finished - %d output bytess   appending timeout messages!   
ERROR: timeout after %d seconds
(   R-   t   datetimet   nowt   timet   mktimet	   timetuplet   setblockingR)   t
   recv_readyt   recvR   R   RM   t
   send_readyt   sendt   exit_status_readyR   t   sleepR   (   R   R@   R?   R>   t   intervalt
   maxsecondst   maxcountt	   input_idxt   timeout_flagt   startt
   start_secsRA   t   dataRS   t   now_secst   et_secs(    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyR=   Š   sR    	
 
#


'N(   t   __name__t
   __module__t   __doc__R   R   R!   R   R   R8   R   RC   RJ   RL   R9   R=   (    (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyR       s   									c       
   C   s¢   d	 }  z yI t d d d ƒ }  |  i d d ƒ \ } } | d j o t | ƒ n Wn/ t j
 o# d t i ƒ  GHt i d ƒ n XWd	 |  o |  i ƒ  n Xd	 S(
   s   main function.s	   127.0.0.1t   cmst   cms1234s9   su root -c 'export PATH=$PATH:/sbin;  service cms status't   root1234i    s   Unexpected error:i   N(	   R   R    RC   t   exitt	   Exceptiont	   tracebackt
   format_exct   sysR!   (   R3   t   retCodeRA   (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   mainÊ   s     t   __main__(
   R"   Rp   R*   RT   RR   R   t   objectR    Rt   Rh   (    (    (    sf   /home/jenkins/workspace/prepack-aio-release-4.2/prepack-aio/src/main/resources/scripts/SSHCommander.pyt   <module>   s   ¿	