관리-도구
편집 파일: pxssh.cpython-39.opt-1.pyc
a ������!^�^����������������������@���s����d�Z�ddlmZmZmZmZ�ddlZddlZddlZddl Z ddgZ G�dd��de�Zejdkrlddl mZ�ne �d �jZd d��ZG�dd��de�ZdS�) a4��This class extends pexpect.spawn to specialize setting up SSH connections. This adds methods for login, logout, and expecting the shell prompt. PEXPECT LICENSE This license is approved by the OSI and FSF as GPL-compatible. http://opensource.org/licenses/isc-license.txt Copyright (c) 2012, Noah Spurrier <noah@noah.org> PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. �����)�ExceptionPexpect�TIMEOUT�EOF�spawnN�ExceptionPxssh�pxsshc�������������������@���s���e�Zd�ZdZdS�)r���z!Raised for pxssh exceptions. N)�__name__� __module__�__qualname__�__doc__��r���r����1/usr/lib/python3.9/site-packages/pexpect/pxssh.pyr��� ���s���)����r���)�quotez[^\w@%+=:,./-]c�����������������C���s,���|�sdS�t�|��du�r|�S�d|��dd��d�S�)z1Return a shell-escaped version of the string *s*.z''N�'z'"'"')�_find_unsafe�replace)�sr���r���r ���r���)���s ����r���c�������������������@���s����e�Zd�ZdZddddddddi�ddddf dd �Zd d��Zdd ��Zd!dd�Zddddddddddddi�ddddfdd�Zdd��Z d"dd�Z dd ��ZdS�)#r���a&��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:: from pexpect 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 as e: print("pxssh failed on login.") print(e) Example showing how to specify SSH options:: from pexpect import pxssh s = pxssh.pxssh(options={ "StrictHostKeyChecking": "no", "UserKnownHostsFile": "/dev/null"}) ... 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) `debug_command_string` is only for the test suite to confirm that the string generated for SSH is correct, using this will not allow you to do anything other than get a string back from `pxssh.pxssh.login()`. ����i���NT�strictFc�����������������C���s`���t�j|�d�||||||||| || d� �d|�_d|�_|�j|�_d|�_d|�_d|�_d|�_||�_ | |�_ d�S�)N)�timeout�maxread�searchwindowsize�logfile�cwd�env� ignore_sighup�echo�encoding�codec_errors�use_pollz<pxssh>z\[PEXPECT\][\$\#] zPS1='[PEXPECT]\$ 'zset prompt='[PEXPECT]\$ 'z5-o'RSAAuthentication=no' -o 'PubkeyAuthentication=no'F)r����__init__�nameZ UNIQUE_PROMPT�PROMPT� PROMPT_SET_SH�PROMPT_SET_CSH�SSH_OPTS�force_password�debug_command_string�options)�selfr���r���r���r���r���r���r���r���r)���r���r���r(���r ���r���r���r ���r!���v���s����� zpxssh.__init__c�����������������C���s����t�|�t�|��}}||kr.||�}}||�}}t|d��}td|d��D�]�}||gdg|���}}td|d��D�]^}||�d�||d��d��} } ||d��}||d��||d��kr�|d�}t| | |�||<�qpqH||�S�)zBThis calculates the Levenshtein distance between a and b. ����r���)�len�range�min)r*����a�b�n�m�current�iZprevious�j�add�deleteZchanger���r���r ����levenshtein_distance����s���� zpxssh.levenshtein_distancec����������� ������C���s|���|d�}|d�}|d�}|�����}t���}d}|}||k�rxz&||�jd|d�7�}t���|�}|}W�q0�tyt���Y�qxY�q00�q0|S�)aX��This facilitates using communication timeouts to perform synchronization as quickly as possible, while supporting high latency connections with a tunable worst case performance. Fast connections should be read almost immediately. Worst case performance for this method is timeout_multiplier * 3 seconds. g�������?皙�����?g������@g��������r+���)�sizer���)Zstring_type�timeZread_nonblockingr���) r*���Ztimeout_multiplierZfirst_char_timeoutZinter_char_timeoutZ total_timeout�promptZbeginZexpiredr���r���r���r ����try_read_prompt����s���� zpxssh.try_read_prompt��������?c�����������������C���s����|������t�d��z|��|��W�n�ty2���Y�n0�|������|��|�}|������|��|�}|������|��|�}|��||�}t|�}|dkr�dS�t|�|�dk�r�dS�dS�)a���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. This can be a slow function. Worst case with the default sync_multiplier can take 12 seconds. Low latency connections are more likely to fail with a low sync_multiplier. Best case sync time gets worse with a high sync multiplier (500 ms with default). r9���r���Fg�������?T)�sendliner;����sleepr=���r���r8���r,����float)r*����sync_multiplier�xr/���r0���ZldZlen_ar���r���r ����sync_original_prompt����s&���� zpxssh.sync_original_prompt���ansiz[#$]� ���r+���z((?i)(?:password:)|(?:passphrase for key)Zsshc�����������#������C���sp��d|| ddt�g}g�}|�|��|�dtg��d�dd��|�j���D���}| rR|d�}|s^|d �}|�jrr|d �|�j�}|dur�|r�tj � |�s�td��|d �|�}|dur�|dt|���}| dur�| dkr�|d�}n$|r�tj � | �s�td��|d| ��}|i�k�rzt i�t|���rzdddd�}|D�]Z}||�}||v��r||�}|D�]4}|dk�rZtt|��}|d�|�d �t|��}�q@�q|du�r�|d�|�}�n|du��r�td���nt|d��"}dd��|���D��}W�d����n1��s�0����Y��d|�}d}d}d} |D�]|}!|�s"t�||!tj��r"d}nX|�r8d|!���v��r8nB|�rXd |!���v��rXd} ��q~n"|�r�t�||!tj��r�d} ��q~�q�|�r�~!~|�s�td!|���n| �s�td"|���|d#||f�7�}|�j�r�|S�|�r�t�|�|��n |��|��|�j||d$�}"|"d%k�r|��d&��|��|�}"|"d'k�r2|��|��|��|�}"|"d(k�rP|��|��|��|�}"|"d)k�rj|�����td*��|"d%k�r�|�����td+��n�|"d,k�r�n�|"d'k�r�|�����td-��np|"d.k�r�|�����td/��nT|"d(k�r�|�����td0��n8|"d1k�r�n,|"d2k�r|�����td3��n|�����td4��|�r@|��|��s@|�����td5��|�rl|�����sl|�����td6|�j|�j f���dS�)7a��This logs the user into the given server. It uses '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 :file:`~/.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 :class:`ExceptionPxssh` exception. In some situations it is not possible or desirable to reset the original prompt. In this case, pass ``auto_prompt_reset=False`` to inhibit setting the prompt to the UNIQUE_PROMPT. Remember that pxssh uses a unique prompt in the :meth:`prompt` method. If the original prompt is not reset then this will disable the :meth:`prompt` method unless you manually set the :attr:`PROMPT` attribute. Set ``password_regex`` if there is a MOTD message with `password` in it. Changing this is like playing in traffic, don't (p)expect it to match straight away. If you require to connect to another SSH server from the your original SSH connection set ``spawn_local_ssh`` to `False` and this will use your current session to do so. Setting this option to `False` and not having an active session will trigger an error. Set ``ssh_key`` to a file path to an SSH private key to use that SSH key for the session authentication. Set ``ssh_key`` to `True` to force passing the current SSH authentication socket to the desired ``hostname``. Set ``ssh_config`` to a file path string of an SSH client config file to pass that file to the client to handle itself. You may set any options you wish in here, however doing so will require you to post extra information that you may not want to if you run into issues. Alter the ``cmd`` to change the ssh client used, or to prepend it with network namespaces. For example ```cmd="ip netns exec vlan2 ssh"``` to execute the ssh in network namespace named ```vlan```. z0(?i)are you sure you want to continue connectingz(?i)permission deniedz(?i)terminal typez$(?i)connection closed by remote hostrE���c�����������������S���s���g�|�]\}}d�||f��qS�)z -o '%s=%s'r���)�.0�o�vr���r���r ���� <listcomp><�������zpxssh.login.<locals>.<listcomp>z -qz) -o'NoHostAuthenticationForLocalhost=yes'� Nz+SSH config does not exist or is not a file.z -F z -p %sTz -Az0private ssh key does not exist or is not a file.z -i %s�L�R�D)�localZremoteZdynamicFz -z -l z0login() needs either a username or an ssh_configZrtc�����������������S���s���g�|�]}|�����qS�r���)�strip)rH����lr���r���r ���rK���o��rL���z^Host\s+%s\s*$z^User\s+\w+\s*$�hostname�hostz+login() ssh_config has no Host entry for %sz+login() ssh_config has no user entry for %sz %s %s�r���r���Zyes������������z&Could not establish connection to hostz-Weird error. Got "are you sure" prompt twice.r+���zpassword refusedr���zpermission deniedz.Weird error. Got "terminal type" prompt twice.��������zconnection closedzunexpected login responsez*could not synchronize with original promptz8could not set shell prompt (received: %r, expected: %r).)!r����extendr����joinr)����itemsr'���r&����os�path�isfiler����str� isinstance�typer���� TypeError�open� readlines�re�match� IGNORECASE�lowerr(���r���Z_spawnr?����expect�closerD����set_unique_prompt�beforer#���)#r*���Zserver�username�passwordZ terminal_typeZoriginal_promptZ login_timeout�portZauto_prompt_resetZssh_key�quietrB���Zcheck_local_ipZpassword_regexZssh_tunnelsZspawn_local_sshrD���Z ssh_config�cmdZsession_regex_arrayZsession_init_regex_arrayZssh_optionsZtunnel_typesZtunnel_typeZcmd_typeZtunnelsZtunnel�f�linesZserver_regexZ user_regexZconfig_has_serverZserver_has_username�liner4���r���r���r ����login���s�����7 � 2 ��zpxssh.loginc�����������������C���s@���|���d��|��tdg�}|dkr4|���d��|��t��|�����dS�)zuSends exit to the remote shell. If there are stopped jobs then this automatically sends exit twice. �exitz(?i)there are stopped jobsr+���N)r?���rl���r���rm���)r*����indexr���r���r ����logout���s���� zpxssh.logout���c�����������������C���s2���|dkr|�j�}|�j|�jtg|d�}|dkr.dS�dS�)a���Match the next shell prompt. This is little more than a short-cut to the :meth:`~pexpect.spawn.expect` method. Note that if you called :meth:`login` with ``auto_prompt_reset=False``, then before calling :meth:`prompt` you must set the :attr:`PROMPT` attribute to a regex that it will use for matching the prompt. Calling :meth:`prompt` will erase the contents of the :attr:`before` attribute even if no prompt is ever matched. If timeout is not given or it is set to -1 then self.timeout is used. :return: True if the shell prompt was matched, False if the timeout was reached. r|���rV���r+���FT)r���rl���r#���r���)r*���r���r4���r���r���r ���r<������s����zpxssh.promptc�����������������C���sb���|���d��|���|�j��|�jt|�jgdd�}|dkr^|���|�j��|�jt|�jgdd�}|dkr^dS�dS�)a}��This sets the remote prompt to something more unique than ``#`` or ``$``. This makes it easier for the :meth:`prompt` method to match the shell prompt unambiguously. This method is called automatically by the :meth:`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. In this case you should call :meth:`login` with ``auto_prompt_reset=False``; then set the :attr:`PROMPT` attribute to a regular expression. After that, the :meth:`prompt` method will try to match your prompt pattern. zunset PROMPT_COMMANDrG���rV���r���FT)r?���r$���rl���r���r#���r%���)r*���r4���r���r���r ���rn������s���� zpxssh.set_unique_prompt)r>���)r|���)r���r ���r ���r���r!���r8���r=���rD���rx���r{���r<���rn���r���r���r���r ���r���4���s*���A � . )� �\ )r���Zpexpectr���r���r���r���r;���r_����sysrh����__all__r����version_infoZshlexr����compile�searchr���r���r���r���r���r ����<module>���s���