HOME


Mini Shell 1.0
DIR: /usr/lib/python3/dist-packages/twisted/conch/ssh/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/twisted/conch/ssh/__pycache__/session.cpython-312.pyc
�

Ϫ�fN5���dZddlZddlZddlZddlZddlmZddlmZm	Z	m
Z
ddlmZm
Z
mZddlmZmZddlmZddlmZe�ZGd	�d
ej.�ZGd�dej2�ZGd
�d�Zd�Zd�Zgd�Zeej>�Gd�dej2��Z Gd�dejB�Z"d�Z#d�Z$d�Z%d�Z&y)z�
This module contains the implementation of SSHSession, which (by default)
allows access to a shell and a python interpreter over SSH.

Maintainer: Paul Swartz
�N)�implementer)�EnvironmentVariableNotPermitted�ISession�ISessionSetEnv)�channel�common�
connection)�
interfaces�protocol)�Logger)�
networkStringc�\�eZdZdZdZd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zy)�
SSHSessiona�
    A generalized implementation of an SSH session.

    See RFC 4254, section 6.

    The precise implementation of the various operations that the remote end
    can send is left up to the avatar, usually via an adapter to an
    interface such as L{ISession}.

    @ivar buf: a buffer for data received before making a connection to a
        client.
    @type buf: L{bytes}
    @ivar client: a protocol for communication with a shell, an application
        program, or a subsystem (see RFC 4254, section 6.5).
    @type client: L{SSHSessionProcessProtocol}
    @ivar session: an object providing concrete implementations of session
        operations.
    @type session: L{ISession}
    ssessionc�t�tjj|g|��i|��d|_d|_d|_y�N�)r�
SSHChannel�__init__�buf�client�session)�self�args�kws   �;/usr/lib/python3/dist-packages/twisted/conch/ssh/session.pyrzSSHSession.__init__8s5�����#�#�D�6�4�6�2�6���������rc�Z�tj|�\}}tjd|��|jj||�}|rIt
|�}t|�}|j|�|jt|��||_
ytjd�y)Nz"Asking for subsystem "{subsystem}")�	subsystem�zFailed to get subsystemr)r�getNS�log�info�avatar�lookupSubsystem�SSHSessionProcessProtocol�wrapProcessProtocol�makeConnection�wrapProtocolr�error)r�datar�ignoredr�pp�protos       r�request_subsystemzSSHSession.request_subsystem>s���#�\�\�$�/��	�7����5���K����,�,�Y��=���*�4�0�B�'��+�E��!�!�%�(����l�6�2�3��D�K���I�I�/�0�rc��tjd�|jst|j�|_	t|�}|jj
|�||_y#t$rtjd�YywxYw)Nz
Getting shellrzError getting shellr)
r r!rrr"r$�	openShellr�	Exception�failure)rr)r+s   r�
request_shellzSSHSession.request_shellMss������!��|�|�#�D�K�K�0�D�L�	�*�4�0�B��L�L�"�"�2�&�
�D�K����	��K�K�-�.��	�s�&A+�+B�Bc�X�|jst|j�|_tj|�\}}t
j
d|��	t|�}|jj||�||_	y#t$rt
jd|��YywxYw)NzExecuting command "{f}")�frzError executing command "{f}"r)rrr"rrr r!r$�execCommandrr0r1)rr)r4r+s    r�request_execzSSHSession.request_exec[s����|�|�#�D�K�K�0�D�L��,�,�t�$���4����*�a��0�	�*�4�0�B��L�L�$�$�R��+�
�D�K����	��K�K�7�1�K�=��	�s�'B� B)�(B)c�"�|jst|j�|_t|�\}}}tjd||��	|jj
|||�y#t$rtjd�YywxYw)Nz-Handling pty request: {term!r} {windowSize!r})�term�
windowSizerzError handling pty requestr)	rrr"�parseRequest_pty_reqr r!�getPtyr0r1)rr)r8r9�modess     r�request_pty_reqzSSHSession.request_pty_reqjs����|�|�#�D�K�K�0�D�L�"6�t�"<���j�%����;��!�	�	
�
	��L�L����j�%�8�
��	�	��K�K�4�5��	�s�A-�-B�
Bc�^�|jst|j�|_tj|j�sytj|d�\}}}	|jj||�y#t$rYyt$rtjd|��YywxYw)ag
        Process a request to pass an environment variable.

        @param data: The environment variable name and value, each encoded
            as an SSH protocol string and concatenated.
        @type data: L{bytes}
        @return: A true value if the request to pass this environment
            variable was accepted, otherwise a false value.
        r�rz)Error setting environment variable {name})�name)rrr"r�
providedByrr�setEnvrr0r r1)rr)r@�values    r�request_envzSSHSession.request_env{s����|�|�#�D�K�K�0�D�L��(�(����6��"�L�L��q�1���e�T�	��L�L����e�,���
/�	���	��K�K�C�$�K�O��	�s�"A?�?	B,�
B,�+B,c���|jst|j�|_t|�}	|jj	|�y#t
$rtjd�YywxYw)NrzError changing window sizer)rrr"�parseRequest_window_change�
windowChangedr0r r1)rr)�winSizes   r�request_window_changez SSHSession.request_window_change�s`���|�|�#�D�K�K�0�D�L�,�T�2��	��L�L�&�&�w�/�
��	�	��K�K�4�5��	�s�A�A0�/A0c��|js|xj|z
c_y|jjj|�y�N)rr�	transport�write�rr)s  r�dataReceivedzSSHSession.dataReceived�s2���{�{��H�H���H�������#�#�D�)rc��|tjk(rT|jrGt|jjd�r&|jjj|�yyytjd|��y)N�writeErrzWeird extended data: {dataType})�dataType)r	�EXTENDED_DATA_STDERRr�hasattrrLrQr �warn)rrRr)s   r�extReceivedzSSHSession.extReceived�s]���z�6�6�6��{�{�w�t�{�{�'<�'<�j�I����%�%�.�.�t�4� J�{�
�H�H�6��H�Jrc��|jr|jj�y|jr|jj	|�yyrK)r�eofReceivedr�conn�	sendClose�rs rrXzSSHSession.eofReceived�s9��
�<�<��L�L�$�$�&�
�[�[��I�I����%�rc���|jr:|jjr$|jjj�|jr|jj	�yyrK)rrL�loseConnectionr�closedr[s rr^zSSHSession.closed�sG���;�;�4�;�;�0�0��K�K�!�!�0�0�2��<�<��L�L���!�rc��|jr$|jjj�tjj|�yrK)rrLr]rrr[s rr]zSSHSession.loseConnection�s3���;�;��K�K�!�!�0�0�2����)�)�$�/rN)�__name__�
__module__�__qualname__�__doc__r@rr-r2r6r=rDrIrOrVrXr^r]�rrrr!sK���(�D��
��
��"�2
�*�K�&�"�0rrc�(�eZdZdZd�Zd�Zd�Zd�Zy)�_ProtocolWrapperzS
    This class wraps a L{Protocol} instance in a L{ProcessProtocol} instance.
    c��||_yrK�r,�rr,s  rrz_ProtocolWrapper.__init__��	����
rc�8�|jj�yrK)r,�connectionMader[s rrlz_ProtocolWrapper.connectionMade�s���
�
�!�!�#rc�:�|jj|�yrK�r,rOrNs  r�outReceivedz_ProtocolWrapper.outReceived�����
�
����%rc�:�|jj|�yrK)r,�connectionLost�r�reasons  r�processEndedz_ProtocolWrapper.processEnded�s���
�
�!�!�&�)rN)r`rarbrcrrlrorurdrrrfrf�s����$�&�*rrfc�*�eZdZd�Zd�Zd�Zd�Zd�Zy)�_DummyTransportc��||_yrKrhris  rrz_DummyTransport.__init__�rjrc�N�|jjj|�yrK)r,rLrMrNs  rrOz_DummyTransport.dataReceived�s���
�
���"�"�4�(rc�:�|jj|�yrKrnrNs  rrMz_DummyTransport.write�rprc�D�|jdj|��yr)rM�join�r�seqs  r�
writeSequencez_DummyTransport.writeSequence�s���
�
�3�8�8�C�=�!rc�V�|jjtj�yrK)r,rrr�connectionDoner[s rr]z_DummyTransport.loseConnection�s���
�
�!�!�(�"9�"9�:rN)r`rarbrrOrMrr]rdrrrwrw�s���)�&�"�;rrwc�P�t|tj�rt|�S|SrK)�
isinstancer�Protocolrf)�insts rr%r%�s"���$��)�)�*���%�%��rc��t|�SrK)rwrhs rr'r'�s
���5�!�!r)
�ABRT�ALRM�FPE�HUP�ILL�INT�KILL�PIPE�QUIT�SEGV�TERM�USR1�USR2c�l�eZdZdZdZd�Zd�Zd�Zd�Zd�Z	d�Z
dd	�Zd
�Zdd�Z
d�Zd
�Zd�Zd�Zd�Zy)r$z�I am both an L{IProcessProtocol} and an L{ITransport}.

    I am a transport to the remote endpoint and a process protocol to the
    local subsystem.
    Nc� �||_d|_y)NF)r�lostOutOrErrFlag)rrs  rrz"SSHSessionProcessProtocol.__init__s����� %��rc��|jjrA|jj|jj�d|j_yyrK)rrrLrMr[s rrlz(SSHSessionProcessProtocol.connectionMades<���<�<����N�N� � ����!1�!1�2�#�D�L�L��rc�:�|jj|�yrK�rrMrNs  rroz%SSHSessionProcessProtocol.outReceived��������4� rc�X�|jjtj|�yrK)r�
writeExtendedr	rS)r�errs  r�errReceivedz%SSHSessionProcessProtocol.errReceived s�����"�"�:�#B�#B�C�Hrc��|jr0|jjj|j�yd|_y)zW
        EOF should only be sent when both STDOUT and STDERR have been closed.
        TN)r�rrY�sendEOFr[s r�outConnectionLostz+SSHSessionProcessProtocol.outConnectionLost#s1��� � ��L�L���%�%�d�l�l�3�$(�D�!rc�$�|j�y)z*
        See outConnectionLost().
        N)r�r[s r�errConnectionLostz+SSHSessionProcessProtocol.errConnectionLost,s��	
��� rc�8�|jj�yrK�rr]rss  rrrz(SSHSessionProcessProtocol.connectionLost2������#�#�%rc��|j��i|_tD]*}d|z}tt|d�}|��||j|<�,tjj�D][\}}|j
d�s�|j
d�r�*||jvs�9|dztjz|j|<�]|j|S)z:
        Get a signal name given a signal number.
        N�SIG�SIG_�@)	�_signalValuesToNames�SUPPORTED_SIGNALS�getattr�signal�__dict__�items�
startswith�sys�platform)r�signum�signame�sigvalue�k�vs      r�_getSignalNamez(SSHSessionProcessProtocol._getSignalName5s����$�$�,�(*�D�%�,�
B���'�/��"�6�7�D�9���'�:A�D�-�-�h�7�	
B�
���-�-�/�
N���1��<�<��&�q�|�|�F�/C��� 9� 9�9�78�3�w����7M��1�1�!�4�
N��(�(��0�0rc�l�|���|j}|j��|j|j�}ttdd��9t	j
|j�rtjd|��d}ntjd|��d}|jjj|jdtjt|d	d��|rd
ndztjd�ztjd�z�n||j�ptjd
|j��|jjj|jdt!j"d|j��|jj%�y)z�
        When we are told the process ended, try to notify the other side about
        how the process ended using the exit-signal or exit-status requests.
        Also, close the channel.
        N�	WCOREDUMPz#exitSignal: {signame} (core dumped))r�TzexitSignal: {}Fsexit-signal���rzexitCode: {exitCode!r})�exitCodesexit-status�>L)rCr�r�r��osr��statusr r!rrY�sendRequestr�NSr
r��struct�packr])rrtr�r��
coreDumpeds     rruz&SSHSessionProcessProtocol.processEndedIsM�����,�,�C��z�z�%��-�-�c�j�j�9���2�{�D�1�=�"�,�,��J�J�C��H�H�B�G�H�T�!%�J��H�H�-�w�H�?�!&�J����!�!�-�-��L�L�"��I�I�m�G�A�B�K�8�9� *�u��7��i�i��n�%��i�i��n�%�����)����1�C�L�L��I����!�!�-�-��L�L�.�&�+�+�d�C�L�L�2Q��	
���#�#�%rc�^�|jjjj�S)z>
        Return the host from my session's transport.
        )rrYrL�getHostr[s rr�z!SSHSessionProcessProtocol.getHostj�#���|�|� � �*�*�2�2�4�4rc�^�|jjjj�S)z>
        Return the peer from my session's transport.
        )rrYrL�getPeerr[s rr�z!SSHSessionProcessProtocol.getPeerpr�rc�:�|jj|�yrKr�rNs  rrMzSSHSessionProcessProtocol.writevr�rc�X�|jjdj|��yr)rrMr|r}s  rrz'SSHSessionProcessProtocol.writeSequenceys�������3�8�8�C�=�)rc�8�|jj�yrKr�r[s rr]z(SSHSessionProcessProtocol.loseConnection|r�rrK)r`rarbrcr�rrlror�r�r�rrr�rur�r�rMrr]rdrrr$r$sW��� ��&�$�
!�I�)�!�&�1�(&�B5�5�!�*�&rr$c��eZdZd�Zy)�SSHSessionClientc�T�|jr|jj|�yyrK)rLrMrNs  rrOzSSHSessionClient.dataReceived�s ���>�>��N�N� � ��&�rN)r`rarbrOrdrrr�r��s��'rr�c�r�tj|�\}}tjd|dd�\}}}}tj|dd�\}}||||f}	t	dt|�dz
d�D�
cgc]5}
t
||
|
dz�tjd||
dz|
dz�df��7}}
||	|fScc}
w)z�Parse the data from a pty-req request into usable data.

    @returns: a tuple of (terminal type, (rows, cols, xpixel, ypixel), modes)
    �>4LN�rr�r�)rrr��unpack�range�len�ord)r)r8�rest�cols�rows�xpixel�ypixelr<r*rH�is           rr:r:�s���
���d�#�J�D�$�!'���u�d�3�B�i�!@��D�$����\�\�$�r�s�)�,�N�E�7��T�6�6�*�G��q�#�e�*�q�.�!�,�
�
�

�U�1�q�1�u�
�	��
�
�d�E�!�a�%�!�a�%�4H� I�!� L�M�
�E�
���%����	
s�3:B4c��|\}}}}tj|�}tjd||||�}tj|�}	||z|	zS)z�
    Pack a pty-req request so that it is suitable for sending.

    NOTE: modes must be packed before being sent here.

    @type geometry: L{tuple}
    @param geometry: A tuple of (rows, columns, xpixel, ypixel)
    r�)rr�r�r�)
r8�geometryr<r�r�r�r��
termPacked�
winSizePacked�modesPackeds
          r�packRequest_pty_reqr��sS��$,� �T�4������4��J��K�K��t�T�6�6�B�M��)�)�E�"�K��
�%��3�3rc�D�tjd|�\}}}}||||fS)zzParse the data from a window-change request into usuable data.

    @returns: a tuple of (rows, cols, xpixel, ypixel)
    r�)r�r�)r)r�r�r�r�s     rrFrF�s,��
"(���u�d�!;��D�$�����v�v�%�%rc�B�|\}}}}tjd||||�S)z�
    Pack a window-change request so that it is suitable for sending.

    @type geometry: L{tuple}
    @param geometry: A tuple of (rows, columns, xpixel, ypixel)
    r�)r�r�)r�r�r�r�r�s     r�packRequest_window_changer��s)��$,� �T�4����;�;�u�d�D�&�&�9�9r)'rcr�r�r�r��zope.interfacer�twisted.conch.interfacesrrr�twisted.conch.sshrrr	�twisted.internetr
r�twisted.loggerr�twisted.python.compatr
r rr�ProcessProtocolrfrwr%r'r��
ITransportr$r�r�r:r�rFr�rdrr�<module>r�s���
�
�
�
�
�&���
:�9�1�!�/��h��c0��#�#�c0�L*�x�/�/�*�$;�;�"�"���"
�Z�
"�
"�#�t&�� 8� 8�t&�$�t&�n'�x�(�(�'�
 � 
4� &�:r