관리-도구
편집 파일: control_persist.cpython-39.pyc
a -�_g�! � @ sl d dl Z d dlZd dlZd dlmZ d dlmZmZmZm Z m Z mZmZm Z d dlmZ G dd� de�ZdS )� N)�RemoteTransport)�InvalidPasswordException�TimeoutPasswordAuthException�PasswordRequestException�AuthPermissionDeniedException�ConnectionException�ConnectionTimeoutException�ControlSocketMissingException�"ControlPersistUnsupportedException)�sos_get_command_outputc @ sV e Zd ZdZdZdd� Zddd�Zdd � Zed d� �Z edd � �Z dd� Zdd� ZdS )�SSHControlPersista� A transport for collect that leverages OpenSSH's ControlPersist functionality which uses control sockets to transparently keep a connection open to the remote host without needing to rebuild the SSH connection for each and every command executed on the node. This transport will by default assume the use of SSH keys, meaning keys have already been distributed to target nodes. If this is not the case, users will need to provide a password using the --password or --password-per-node option, depending on if the password to connect to all nodes is the same or not. Note that these options prevent the use of the --batch option, as they require user input. Zcontrol_persistc C sj g d�}t j|t jt jd��:}|�� \}}|�d�}d|v sDd|v rHt�W d � n1 s\0 Y dS )a Checks to see if the local system supported SSH ControlPersist. ControlPersist allows OpenSSH to keep a single open connection to a remote host rather than building a new session each time. This is the same feature that Ansible uses in place of paramiko, which we have a need to drop in sos collect. This check relies on feedback from the ssh binary. The command being run should always generate stderr output, but depending on what that output reads we can determine if ControlPersist is supported or not. For our purposes, a host that does not support ControlPersist is not able to run sos collect. Returns True if ControlPersist is supported, else raise Exception. )Zsshz-oZControlPersist)�stdout�stderr�utf-8zBad configuration optionzUsage:NT)� subprocess�Popen�PIPEZcommunicate�decoder )�self�ssh_cmd�cmd�_�err� r �L/usr/lib/python3.9/site-packages/sos/collector/transports/control_persist.py�_check_for_control_persist- s � "z,SSHControlPersist._check_for_control_persist� c C s� z| � � W n ty* | �d� � Y n0 | �d� | j� d| j� �| _d| _d}d}d}| jj dkrxd| jj � d�}| jj r�d | jj � �}d |� d|� d| j� d| jj� d| j� d �}tj |dd�}dddddtjg}|j|dd�}|dkr�d}n�|dk�r`|�rZddtjg} |�|� |j| dd�} | dk�r:d}n| dk�rJt�n| dk�r^t�nt�nb|dk�rpt�nR|dk�r�t| j| jj ��n6|dk�r�t| j��n |dk�r�t�ntd|j� ���|�r�tj�| j��s�t�| �d | j� �� dS dS )!a. Using ControlPersist, create the initial connection to the node. This will generate an OpenSSH ControlPersist socket within the tmp directory created or specified for sos collect to use. At most, we will wait 30 seconds for a connection. This involves a 15 second wait for the initial connection attempt, and a subsequent 15 second wait for a response when we supply a password. Since we connect to nodes in parallel (using the --threads value), this means that the time between 'Connecting to nodes...' and 'Beginning collection of sosreports' that users see can be up to an amount of time equal to 30*(num_nodes/threads) seconds. Returns True if session is successfully opened, else raise Exception zYOpenSSH ControlPersist is not locally supported. Please update your OpenSSH installation.z,Opening SSH session to create control socketz/.sos-collector-r F� z-p� z-izssh zT -oControlPersist=600 -oControlMaster=auto -oStrictHostKeyChecking=no -oControlPath=�@z "echo Connected"r )�encodingZ Connectedz password:z.*Permission denied.*z.* port .*: No route to hostz.*Could not resolve hostname.*� ��timeoutr T� z$Permission denied, please try again.� � � � zUnknown error, client returned z'Successfully created control socket at )r r Z log_errorZlog_infoZtmpdir�address�control_pathr �opts�ssh_port�ssh_key�ssh_user�pexpectZspawnZTIMEOUT�expectZsendliner r r r r r � Exception�before�os�path�existsr � log_debug)r �password� connectedr- r, r �resZconnect_expects�indexZpass_expectsZ pass_indexr r r �_connectH s� ���� � �zSSHControlPersist._connectc C sf t j�| j�rXzt �| j� W dS tyV } z| �d|� �� W Y d }~dS d }~0 0 | �d� dS )NTzCould not disconnect properly: Fz?Control socket not present when attempting to terminate session)r3 r4 r5 r* �remover1 r6 )r r r r r �_disconnect� s zSSHControlPersist._disconnectc C s t j�| j�S )a� Check if the SSH control socket exists The control socket is automatically removed by the SSH daemon in the event that the last connection to the node was greater than the timeout set by the ControlPersist option. This can happen for us if we are collecting from a large number of nodes, and the timeout expires before we start collection. )r3 r4 r5 r* �r r r r r8 � s zSSHControlPersist.connectedc C s, | j s&d| j� d| jj� d| j� �| _ | j S )Nzssh -oControlPath=r r )r r* r+ r. r) r>