관리-도구
편집 파일: paramiko_ssh.cpython-39.pyc
a �����)gj����������������������@���s��d�dl�mZmZmZ�eZdZd�dlZd�dlZd�dl Z d�dl Z d�dlZd�dlZd�dl Z d�dlmZmZ�d�dlmZ�d�dlmZ�d�dlmZmZmZmZ�d�dlmZmZ�d�d lmZ�d�d lm Z �d�dl!m"Z"�d�dl#m$Z$m%Z%m&Z&�e ��Z'd Z(e �)d�Z*G�dd��de+�Z,i�Z-i�Z.G�dd��de�Z/dS�)�����)�absolute_import�division�print_functionaN�� author: Ansible Core Team name: paramiko short_description: Run tasks via python ssh (paramiko) description: - Use the python ssh implementation (Paramiko) to connect to targets - The paramiko transport is provided because many distributions, in particular EL6 and before do not support ControlPersist in their SSH implementations. - This is needed on the Ansible control machine to be reasonably efficient with connections. Thus paramiko is faster for most users on these platforms. Users with ControlPersist capability can consider using -c ssh or configuring the transport in the configuration file. - This plugin also borrows a lot of settings from the ssh plugin as they both cover the same protocol. version_added: "0.1" options: remote_addr: description: - Address of the remote target default: inventory_hostname vars: - name: inventory_hostname - name: ansible_host - name: ansible_ssh_host - name: ansible_paramiko_host remote_user: description: - User to login/authenticate as - Can be set from the CLI via the C(--user) or C(-u) options. vars: - name: ansible_user - name: ansible_ssh_user - name: ansible_paramiko_user env: - name: ANSIBLE_REMOTE_USER - name: ANSIBLE_PARAMIKO_REMOTE_USER version_added: '2.5' ini: - section: defaults key: remote_user - section: paramiko_connection key: remote_user version_added: '2.5' keyword: - name: remote_user password: description: - Secret used to either login the ssh server or as a passphrase for ssh keys that require it - Can be set from the CLI via the C(--ask-pass) option. vars: - name: ansible_password - name: ansible_ssh_pass - name: ansible_ssh_password - name: ansible_paramiko_pass - name: ansible_paramiko_password version_added: '2.5' use_rsa_sha2_algorithms: description: - Whether or not to enable RSA SHA2 algorithms for pubkeys and hostkeys - On paramiko versions older than 2.9, this only affects hostkeys - For behavior matching paramiko<2.9 set this to C(False) vars: - name: ansible_paramiko_use_rsa_sha2_algorithms ini: - {key: use_rsa_sha2_algorithms, section: paramiko_connection} env: - {name: ANSIBLE_PARAMIKO_USE_RSA_SHA2_ALGORITHMS} default: True type: boolean version_added: '2.14' host_key_auto_add: description: 'Automatically add host keys' env: [{name: ANSIBLE_PARAMIKO_HOST_KEY_AUTO_ADD}] ini: - {key: host_key_auto_add, section: paramiko_connection} type: boolean look_for_keys: default: True description: 'False to disable searching for private key files in ~/.ssh/' env: [{name: ANSIBLE_PARAMIKO_LOOK_FOR_KEYS}] ini: - {key: look_for_keys, section: paramiko_connection} type: boolean proxy_command: default: '' description: - Proxy information for running the connection via a jumphost - Also this plugin will scan 'ssh_args', 'ssh_extra_args' and 'ssh_common_args' from the 'ssh' plugin settings for proxy information if set. env: [{name: ANSIBLE_PARAMIKO_PROXY_COMMAND}] ini: - {key: proxy_command, section: paramiko_connection} ssh_args: description: Only used in parsing ProxyCommand for use in this plugin. default: '' ini: - section: 'ssh_connection' key: 'ssh_args' env: - name: ANSIBLE_SSH_ARGS vars: - name: ansible_ssh_args version_added: '2.7' ssh_common_args: description: Only used in parsing ProxyCommand for use in this plugin. ini: - section: 'ssh_connection' key: 'ssh_common_args' version_added: '2.7' env: - name: ANSIBLE_SSH_COMMON_ARGS version_added: '2.7' vars: - name: ansible_ssh_common_args cli: - name: ssh_common_args default: '' ssh_extra_args: description: Only used in parsing ProxyCommand for use in this plugin. vars: - name: ansible_ssh_extra_args env: - name: ANSIBLE_SSH_EXTRA_ARGS version_added: '2.7' ini: - key: ssh_extra_args section: ssh_connection version_added: '2.7' cli: - name: ssh_extra_args default: '' pty: default: True description: 'SUDO usually requires a PTY, True to give a PTY and False to not give a PTY.' env: - name: ANSIBLE_PARAMIKO_PTY ini: - section: paramiko_connection key: pty type: boolean record_host_keys: default: True description: 'Save the host keys to a file' env: [{name: ANSIBLE_PARAMIKO_RECORD_HOST_KEYS}] ini: - section: paramiko_connection key: record_host_keys type: boolean host_key_checking: description: 'Set this to "False" if you want to avoid host key checking by the underlying tools Ansible uses to connect to the host' type: boolean default: True env: - name: ANSIBLE_HOST_KEY_CHECKING - name: ANSIBLE_SSH_HOST_KEY_CHECKING version_added: '2.5' - name: ANSIBLE_PARAMIKO_HOST_KEY_CHECKING version_added: '2.5' ini: - section: defaults key: host_key_checking - section: paramiko_connection key: host_key_checking version_added: '2.5' vars: - name: ansible_host_key_checking version_added: '2.5' - name: ansible_ssh_host_key_checking version_added: '2.5' - name: ansible_paramiko_host_key_checking version_added: '2.5' use_persistent_connections: description: 'Toggles the use of persistence for connections' type: boolean default: False env: - name: ANSIBLE_USE_PERSISTENT_CONNECTIONS ini: - section: defaults key: use_persistent_connections banner_timeout: type: float default: 30 version_added: '2.14' description: - Configures, in seconds, the amount of time to wait for the SSH banner to be presented. This option is supported by paramiko version 1.15.0 or newer. ini: - section: paramiko_connection key: banner_timeout env: - name: ANSIBLE_PARAMIKO_BANNER_TIMEOUT # TODO: #timeout=self._play_context.timeout, N)�tcflush�TCIFLUSH)�LooseVersion)�hexlify)�AnsibleAuthenticationFailure�AnsibleConnectionFailure�AnsibleError�AnsibleFileNotFound)�PARAMIKO_IMPORT_ERR�paramiko)�ConnectionBase)�Display)� makedirs_safe)�to_bytes� to_native�to_textz� paramiko: The authenticity of host '%s' can't be established. The %s key fingerprint is %s. Are you sure you want to continue connecting (yes/no)? z(\w+)(?:\s*=\s*|\s+)(.+)c�������������������@���s ���e�Zd�ZdZdd��Zdd��ZdS�)�MyAddPolicya �� Based on AutoAddPolicy in paramiko so we can determine when keys are added and also prompt for input. 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���s���||�_�||�_|j|�_d�S��N)� _new_stdin� connection�_options)�selfZ new_stdinr�����r����K/usr/lib/python3.9/site-packages/ansible/plugins/connection/paramiko_ssh.py�__init__����s����zMyAddPolicy.__init__c�����������������C���s����t�|�jd�|�jd��f�r�t|����}|���}|�j�d�sB|�jjr\tt dd��|||f���|�j� ���tj}|�j t_ttjt��tt |||f��}|t_|�j����|dvr�td��d|_|j�||���|��d�S�) N�host_key_checkingZhost_key_auto_addZuse_persistent_connections�����\���)Zyes�y��z host connection rejected by userT)�allr���r���Zget_fingerprintZget_namer���� get_optionZforce_persistencer����AUTHENTICITY_MSGZconnection_lock�sys�stdinr���r���r����inputZconnection_unlock�_added_by_ansible_this_time� _host_keys�add)r���Zclient�hostname�keyZfingerprintZktypeZ old_stdin�inpr���r���r����missing_host_key����s ���� zMyAddPolicy.missing_host_keyN)�__name__� __module__�__qualname__�__doc__r���r/���r���r���r���r���r�������s��� r���c�����������������������s����e�Zd�ZdZdZdZdd��Zdd��Zdd ��Zd dd�Z d d��Z d!��fdd� Z��fdd�Zdd��Z ��fdd�Zdd��Zdd��Zdd��Zdd��Z���ZS�)"� Connectionz% SSH based connections with Paramiko r���Nc�����������������C���s���d|�j�j|�j�jf�S��Nz%s__%s__)� _play_context�remote_addr�remote_user�r���r���r���r���� _cache_key.��s����zConnection._cache_keyc�����������������C���s8���|�����}|tv�rt|�|�_n|�����|�_t|<�d|�_|�S�)NT)r:����SSH_CONNECTION_CACHE�ssh�_connect_uncached� _connected)r���� cache_keyr���r���r����_connect1��s����zConnection._connectc�����������������C���s ���||�_�dS�)z(Mimic paramiko.SSHClient.set_log_channelN)�_log_channel)r����namer���r���r����_set_log_channel;��s����zConnection._set_log_channel����c�����������������C���s*��d�}|���d�|���d�|���dd�g}|��d�|��}t|�D�]T\}}|���dkr\||d��}n*t�|�}|r�|�d����dkr�|�d�}|r:�q�q:|���d �p�|}i�}|�r&|�jj ||�jj d �} | ���D�]\} }|�| t |��}q�z(dt�|�i}tjd|�|�jj d ��W�n�t�y$���t�d��Y�n0�|S�)NZssh_extra_argsZssh_common_args�ssh_argsr"���� Zproxycommandr�������� proxy_command)z%hz%pz%rZsockz*CONFIGURE PROXY COMMAND FOR CONNECTION: %s��hostzwParamiko ProxyCommand support unavailable. Please upgrade to Paramiko 1.9.0 or newer. Not using configured ProxyCommand)r$���Z_split_ssh_args�join� enumerate�lower�SETTINGS_REGEX�match�groupr6���r7���r8����items�replace�strr���ZProxyCommand�display�vvv�AttributeErrorZwarning)r����portrH���rE����args�i�argrO���Z sock_kwargZ replacers�findrR���r���r���r����_parse_proxy_command?��s<���� � �zConnection._parse_proxy_commandc�����������������C���s���t�du�rtdtt����|�jjp"d}tjd|�jj||�jj f�|�jj d��t�� ��}tt�jdd�}tt�jdd�}|�� d �}i�}|s�|r�td d��|D���|d<�|r�td d��|D���|d<�|�jdur�|�|�j��tj�d�|�_|�� d��r dD�]0}z|�|��W���qW�q��t�y���Y�q�0�q�|����|��|�}|�t|�j|����|�� d��pN|�jj} d} | du�rbd} z�d}|�jj�r�tj�|�jj�}tt�j�td�k�r�|�jj|d<�tt�j�td�k�r�|�� d�|d<�|j |�jj �!��f|�jj| |�� d�|| |�jj||d�|���W�n��t�j"j#�y8�}�zt$d|j%���W�Y�d}~n�d}~0��t�j"j&�yv�}�z"d�'t(|��} t)| ��W�Y�d}~n~d}~0��t*�y��}�z\t(|�} d| v��r�td��n4d| v��r�d |�jj|�jj || f�} t$| ��nt$| ��W�Y�d}~n d}~0�0�|S�)!z! activates the connection object Nzparamiko is not installed: %srD���z?ESTABLISH PARAMIKO SSH CONNECTION FOR USER: %s on PORT %s TO %srI���Z_preferred_pubkeysr���Z_preferred_keys�use_rsa_sha2_algorithmsc�����������������s���s���|�]}d�|v�r|V��qdS��zrsa-sha2Nr�����.0�ar���r���r���� <genexpr>��������z/Connection._connect_uncached.<locals>.<genexpr>Zpubkeysc�����������������s���s���|�]}d�|v�r|V��qdS�r^���r���r_���r���r���r���rb������rc����keysz~/.ssh/known_hostsr���)z/etc/ssh/ssh_known_hostsz/etc/openssh/ssh_known_hosts�passwordTFz2.2.0Zauth_timeoutz1.15.0Zbanner_timeout� look_for_keys)�username�allow_agentrf����key_filenamere����timeoutrW����disabled_algorithmszhost key mismatch for %szFailed to authenticate: {0}zPID check failedzNparamiko version issue, please upgrade paramiko on the machine running ansiblezPrivate key file is encryptedzDssh %s@%s:%s : %s To connect as a different user, use -u <username>.)+r���r���r���r ���r6���rW���rT���rU���r8���r7���Z SSHClient�getattrZ Transportr$����tuplerA���Zset_log_channel�os�path� expanduser�keyfile�load_system_host_keys�IOErrorr\���Zset_missing_host_key_policyr���r���re���Zprivate_key_filer����__version__rj���ZconnectrM���Z ssh_exceptionZBadHostKeyExceptionr ���r,���ZAuthenticationException�formatr���r ���� Exception)r���rW���r<���Zparamiko_preferred_pubkeysZparamiko_preferred_hostkeysr]���rk���Zssh_known_hostsZssh_connect_kwargsZ conn_passwordrh���ri����e�msgr���r���r���r=���l��s������ �� � " � zConnection._connect_uncachedTc�������������� ������s���t�t|��j|||d��|r"td��d}z"|�j����d��|�j������}W�nL�ty��}�z4t |�}d}|rt|d|�7�}t t|���W�Y�d}~n d}~0�0�|��d�r�|r�|j t�d d �tt�dd��tt�d d��d��tjd|�|�jjd��t|dd�}d} d} d}�z2|�|��|�j�r4|�j����r4d}d} | �s�|�s�t�d��|�|�}t�d|���|�s�d|v��r�t|�jjd|�jd��}td|���n�q�||7�}|�d�D�]8}|�j�|��r�d} ��q,n|�j�|��r�d}��q,�q��q,|�r$|�j�r|�jjd|�jd�}|�t|dd�d���ntd��n| |7�} | |7�} W�n"�tj�yX���td|���Y�n0�d� |�!d |��}d� |�"d |��}|�#��| |�| |�fS�)!z" run a command on the remote host )�in_data�sudoablezHInternal Error: this module does not support optimized module pipeliningi�������zFailed to open sessionz: %sNZptyZTERMZvt100�COLUMNSr����LINES)Zterm�widthZheightzEXEC %srI����surrogate_or_strict��errorsrc���Fz&Waiting for Privilege Escalation inputzchunk is: %ss���unknown userZbecome_user)Zplaycontextzuser %s does not existT�become_pass���� z,A password is required but none was suppliedz0ssh timed out waiting for privilege escalation. �rb)$�superr4����exec_commandr���r<���Z get_transportZ set_keepaliveZopen_sessionrv���r���r ���r���r$���Zget_ptyrn����getenv�intrT���rU���r6���r7���r���ZbecomeZ expect_prompt�debugZrecv� splitlinesZ check_successZcheck_password_promptZsendall�socketrj���rK���ZmakefileZmakefile_stderrZrecv_exit_status)r����cmdry���rz����bufsizeZchanrw���Ztext_erx���Z no_prompt_outZ no_prompt_errZ become_outputZ passpromptZ become_sucess�chunkZ n_become_user�lr�����stdout�stderr�� __class__r���r���r�������sp����"0 � zConnection.exec_commandc�������������� ������s����t�t|���||��tjd||f�|�jjd��tj� t |dd��sLtd|���z|�j� ��|�_W�n0�ty��}�ztd|���W�Y�d}~n d}~0�0�z"|�j�t |dd�t |dd���W�n�ty����td|���Y�n0�dS�) z& transfer a file from local to remote zPUT %s TO %srI���r���r����z!file or module does not exist: %s�%failed to open a SFTP connection (%s)Nzfailed to transfer file to %s)r����r4����put_filerT���rU���r6���r7���rn���ro����existsr���r���r<���� open_sftp�sftprv���r���Zputrs����r���Zin_pathZout_pathrw���r����r���r���r������s����""zConnection.put_filec�����������������C���sB���d|�j�j|�j�jf�}|tv�r$t|�S�|����j����}t|<�|S�d�S�r5���)r6���r7���r8����SFTP_CONNECTION_CACHEr@���r<���r����)r���r?����resultr���r���r���� _connect_sftp/��s ����zConnection._connect_sftpc�������������� ������s����t�t|���||��tjd||f�|�jjd��z|����|�_W�n4�t yn�}�zt dt|����W�Y�d}~n d}~0�0�z"|�j�t |dd�t |dd���W�n�ty����t d|���Y�n0�dS�)z* save a remote file to the specified path zFETCH %s TO %srI���r����Nr���r����zfailed to transfer file from %s)r����r4���� fetch_filerT���rU���r6���r7���r����r����rv���r���r����getr���rs���r����r����r���r���r����8��s����&"zConnection.fetch_filec�����������������C���sD���|�j�j���D�]2\}}|���D�] \}}t|dd�}|r��dS�qqdS�)Nr)���FT)r<���r*���rQ���rl���)r���r,���rd����keytyper-����added_this_timer���r���r����_any_keys_addedI��s����zConnection._any_keys_addedc����������� ��� ���C���s����|�����sdS�tj�d�}t|��t|d���}|�jj���D�]B\}}|���D�]0\}}t |dd�}|sH|� d|||���f���qHq8|�jj���D�]B\}}|���D�]0\}}t |dd�}|r�|� d|||���f���q�q�W�d����n1�s�0����Y��dS�)z� not using the paramiko save_ssh_host_keys function as we want to add new SSH keys at the bottom so folks don't complain about it :) Fz~/.ssh�wr)���z %s %s %s N)r����rn���ro���rp���r����openr<���r*���rQ���rl����writeZ get_base64) r����filenamero����fr,���rd���r����r-���r����r���r���r����_save_ssh_host_keysR��s����zConnection._save_ssh_host_keysc�����������������C���s���|�j�s d�S�|�����|�����d�S�r���)r>����closer@���r9���r���r���r����resetp��s����zConnection.resetc�����������������C���s���|�����}t�|d��t�|d��t|�d�r>|�jdur>|�j����|��d��r�|��d��r�|�����r�|�j � dd�}tj� |�j �}t|��t|d�}t�|tj��z�|�j����|�jj�|�jj��tj� |�j �}tj�|�j �r�t�|�j �}|j}|j}|j} nd}t���}t���} tj |d d �} t�!| j"|d@���t�#| j"|| ��|��$| j"��| ����t�%| j"|�j ��W�n�t&�y|���t'�(���Y�n0�t�|tj)��|�j����d |�_*dS�)z terminate the connection Nr����r���Zrecord_host_keysZknown_hostsz.known_hosts.lockr����i����F)�dir�deletei���)+r:���r;����popr�����hasattrr����r����r$���r����rq���rR���rn���ro����dirnamer���r�����fcntl�lockfZLOCK_EXr<���rr���r*����updateZ_system_host_keysr�����stat�st_mode�st_uid�st_gid�getuid�getgid�tempfileZNamedTemporaryFile�chmodrB����chownr�����renamerv���� traceback� print_excZLOCK_UNr>���)r���r?���Zlockfiler����ZKEY_LOCKZkey_dirZkey_stat�mode�uid�gidZtmp_keyfiler���r���r���r����v��sF���� " zConnection.close)rD���)NT)r0���r1���r2���r3���Z transportrA���r:���r@���rC���r\���r=���r����r����r����r����r����r����r����r����� __classcell__r���r���r����r���r4���(��s ��� -^Q r4���)0Z __future__r���r���r����typeZ __metaclass__Z DOCUMENTATIONrn���r����r����r����r����r&����reZtermiosr���r���Z#ansible.module_utils.compat.versionr���Zbinasciir���Zansible.errorsr ���r ���r���r���Z$ansible.module_utils.compat.paramikor ���r���Zansible.plugins.connectionr���Zansible.utils.displayr���Zansible.utils.pathr���Zansible.module_utils._textr���r���r���rT���r%����compilerN����objectr���r;���r����r4���r���r���r���r����<module>���s4����C 6