a )g$Y@sddlmZmZmZeZddlmZddlm Z ddl m Z ddl m Z mZddlmZmZzddlmZdZWneyd ZYn0d d ZGd d d e ZdS))absolute_importdivisionprint_function)abstractmethodwraps) AnsiblePlugin) AnsibleErrorAnsibleConnectionFailure)to_bytesto_text) SCPClientTFcstfdd}|S)Ncs>|j}t|ddds(td|g|Ri|S)NZsurrogate_or_stricterrors#z'operation requires privilege escalation) _connection get_promptr stripendswithr )selfargskwargspromptfuncD/usr/lib/python3.9/site-packages/ansible/plugins/cliconf/__init__.pywrapped%s zenable_mode..wrappedr)rrrrr enable_mode$srcseZdZdZgdZfddZddZd8d d Zd d ZddZ ddZ ddZ ddZ e d9ddZe d:ddZe d;ddZe ddZe dd Zdd)d*Zd?d+d,Zd@d-d.ZdAd/d0ZdBd1d2Zd3d4ZdCd6d7ZZS)D CliconfBasea" A base class for implementing cli connections .. note:: String inputs to :meth:`send_command` will be cast to byte strings within this method and as such are not required to be made byte strings beforehand. Please avoid using literal byte strings (``b'string'``) in :class:`CliConfBase` plugins as this can lead to unexpected errors when running on Python 3 List of supported rpc's: :get_config: Retrieves the specified configuration from the device :edit_config: Loads the specified commands into the remote device :get: Execute specified command on remote device :get_capabilities: Retrieves device information and supported rpc methods :commit: Load configuration from candidate to running :discard_changes: Discard changes to candidate datastore Note: List of supported rpc's for remote device can be extracted from output of get_capabilities() :returns: Returns output received from remote device as byte string Usage: from ansible.module_utils.connection import Connection conn = Connection() conn.get('show lldp neighbors detail') conn.get_config('running') conn.edit_config(['hostname test', 'netconf ssh']) ) get_config edit_configget_capabilitiesgetenable_response_loggingdisable_response_loggingcs&tt|||_t|_d|_dS)NF)superr__init__rlisthistoryresponse_logging)rZ connection __class__rrr'PszCliconfBase.__init__cCs$|jdd|jjj|dS)z0Alarm handler raised in case of command timeout logz2closing shell due to command timeout (%s seconds).N)r queue_message _play_contexttimeoutclose)rZsignumframerrr_alarm_handlerVszCliconfBase._alarm_handlerNFTc Cst|||||d}|durFt|tr:dd|D|d<n t||d<|durxt|trldd|D|d<n t||d<|jjfi|} |js|jdn|j|d | f| S) aExecutes a command over the device connection This method will execute a command over the device connection and return the results to the caller. This method will also perform logging of any commands based on the `nolog` argument. :param command: The command to send over the connection to the device :param prompt: A single regex pattern or a sequence of patterns to evaluate the expected prompt from the command :param answer: The answer to respond with if the prompt is matched. :param sendonly: Bool value that will send the command but not wait for a result. :param newline: Bool value that will append the newline character to the command :param prompt_retry_check: Bool value for trying to detect more prompts :param check_all: Bool value to indicate if all the values in prompt sequence should be matched or any one of given prompt. :returns: The output from the device after executing the command )commandsendonlynewlineprompt_retry_check check_allNcSsg|] }t|qSrr .0prrr vz,CliconfBase.send_command..rcSsg|] }t|qSrr9r:rrrr={r>answer)*****r@r4)r isinstancer(rsendr*r)append) rr4rr?r5r6r7r8rZresprrr send_command[s&    zCliconfBase.send_commandcCs|jS)z:Returns list of base rpc method supported by remote device)__rpc__rrrr get_base_rpcszCliconfBase.get_base_rpccCs|jS)aR Returns the history file for all commands This will return a log of all the commands that have been sent to the device and all of the output received. By default, all commands and output will be redacted unless explicitly configured otherwise. :return: An ordered list of command, output pairs )r)rFrrr get_historys zCliconfBase.get_historycCs t|_dS)zB Resets the history of run commands :return: None N)r(r)rFrrr reset_historyszCliconfBase.reset_historycCs d|_dS)zEnable logging command responseTNr*rFrrrr$sz#CliconfBase.enable_response_loggingcCs d|_dS)z Disable logging command responseFNrJrFrrrr%sz$CliconfBase.disable_response_loggingrunningcCsdS)aRetrieves the specified configuration from the device This method will retrieve the configuration specified by source and return it to the caller as a string. Subsequent calls to this method will retrieve a new configuration from the device :param source: The configuration source to return from the device. This argument accepts either `running` or `startup` as valid values. :param flags: For devices that support configuration filtering, this keyword argument is used to filter the returned configuration. The use of this keyword argument is device dependent and will be silently ignored on devices that do not support it. :param format: For devices that support fetching different configuration format, this keyword argument is used to specify the format in which configuration is to be retrieved. :return: The device configuration as specified by the source argument. Nr)rsourceflagsformatrrrr szCliconfBase.get_configcCsdS)aeLoads the candidate configuration into the network device This method will load the specified candidate config into the device and merge with the current configuration unless replace is set to True. If the device does not support config replace an errors is returned. :param candidate: The configuration to load into the device and merge with the current running configuration :param commit: Boolean value that indicates if the device candidate configuration should be pushed in the running configuration or discarded. :param replace: If the value is True/False it indicates if running configuration should be completely replace by candidate configuration. If can also take configuration file path as value, the file in this case should be present on the remote host in the mentioned path as a prerequisite. :param comment: Commit comment provided it is supported by remote host :return: Returns a json string with contains configuration applied on remote host, the returned response on executing configuration commands and platform relevant data. { "diff": "", "response": [], "request": [] } Nr)r candidatecommitreplacediffcommentrrrr!szCliconfBase.edit_configcCsdS)a.Execute specified command on remote device This method will retrieve the specified data and return it to the caller as a string. :param command: command in string format to be executed on remote device :param prompt: the expected prompt generated by executing command, this can be a string or a list of strings :param answer: the string to respond to the prompt with :param sendonly: bool to disable waiting for response, default is false :param newline: bool to indicate if newline should be added at end of answer or not :param output: For devices that support fetching command output in different format, this keyword argument is used to specify the output in which response is to be retrieved. :param check_all: Bool value to indicate if all the values in prompt sequence should be matched or any one of given prompt. :return: The output from the device after executing the command Nr)rr4rr?r5r6outputr8rrrr#szCliconfBase.getcCs(i}||d<||d<d|d<|S)a; Returns the basic capabilities of the network device This method will provide some basic facts about the device and what capabilities it has to modify the configuration. The minimum return from this method takes the following format. eg: { 'rpc': [list of supported rpcs], 'network_api': , # the name of the transport 'device_info': { 'network_os': , 'network_os_version': , 'network_os_model': , 'network_os_hostname': , 'network_os_image': , 'network_os_platform': , }, 'device_operations': { 'supports_diff_replace': , # identify if config should be merged or replaced is supported 'supports_commit': , # identify if commit is supported by device or not 'supports_rollback': , # identify if rollback is supported or not 'supports_defaults': , # identify if fetching running config with default is supported 'supports_commit_comment': , # identify if adding comment to commit is supported of not 'supports_onbox_diff': , # identify if on box diff capability is supported or not 'supports_generate_diff': , # identify if diff capability is supported within plugin 'supports_multiline_delimiter': , # identify if multiline demiliter is supported within config 'supports_diff_match': , # identify if match is supported 'supports_diff_ignore_lines': , # identify if ignore line in diff is supported 'supports_config_replace': , # identify if running config replace with candidate config is supported 'supports_admin': , # identify if admin configure mode is supported or not 'supports_commit_label': , # identify if commit label is supported or not } 'format': [list of supported configuration format], 'diff_match': [list of supported match values], 'diff_replace': [list of supported replace values], 'output': [list of supported command output format] } :return: capability as json string ZrpcZ device_infoZcliconfZ network_api)rGget_device_info)rresultrrrr"s )  zCliconfBase.get_capabilitiescCsdS)aCReturns basic information about the network device. This method will provide basic information about the device such as OS version and model name. This data is expected to be used to fill the 'device_info' key in get_capabilities() above. :return: dictionary of device information NrrFrrrrUs zCliconfBase.get_device_infocCs|jd|jjS)aCommit configuration changes This method will perform the commit operation on a previously loaded candidate configuration that was loaded using `edit_config()`. If there is a candidate configuration, it will be committed to the active configuration. If there is not a candidate configuration, this method should just silently return. :return: None z(commit is not supported by network_os %srZmethod_not_foundr/Z network_os)rrSrrrrP+s zCliconfBase.commitcCs|jd|jjS)aDiscard candidate configuration This method will discard the current candidate configuration if one is present. If there is no candidate configuration currently loaded, then this method should just silently return :returns: None z1discard_changes is not supported by network_os %srWrFrrrdiscard_changes8s zCliconfBase.discard_changescCsdS)z :param rollback_id: The commit id to which configuration should be rollbacked :param commit: Flag to indicate if changes should be committed or not :return: Returns diff between before and after change. Nr)rZ rollback_idrPrrrrollbackCszCliconfBase.rollbackscpc Cs|jj}|dkr^ts tdt||d}|||}Wdq1sR0Yn<|dkr|}|||Wdn1s0YdS)aCopies file over scp/sftp to remote device :param source: Source file path :param destination: Destination file path on remote device :param proto: Protocol to be used for file transfer, supported protocol: scp and sftp :param timeout: Specifies the wait time to receive response from remote host before triggering timeout exception :return: None rZQRequired library scp is not installed. Please install it using `pip install scp`Zsocket_timeoutNsftp) r paramiko_conn_connect_uncachedHAS_SCPr r get_transportZput open_sftp) rrL destinationprotor0sshrZoutr^rrr copy_fileLs , zCliconfBase.copy_filecCs|jj}|dkrvts tdz@t||d}|||Wdn1sT0YWqtyrYq0n<|dkr| }|||Wdn1s0YdS)aFetch file over scp/sftp from remote device :param source: Source file path :param destination: Destination file path :param proto: Protocol to be used for file transfer, supported protocol: scp and sftp :param timeout: Specifies the wait time to receive response from remote host before triggering timeout exception :return: None rZr\r]Nr^) rr_r`rar r rbr#EOFErrorrc)rrLrdrer0rfrZr^rrrget_fileas .  zCliconfBase.get_filecCsdS)a: Generate diff between candidate and running configuration. If the remote host supports onbox diff capabilities ie. supports_onbox_diff in that case candidate and running configurations are not required to be passed as argument. In case if onbox diff capability is not supported candidate argument is mandatory and running argument is optional. :param candidate: The configuration which is expected to be present on remote host. :param running: The base configuration which is used to generate diff. :param diff_match: Instructs how to match the candidate configuration with current device configuration Valid values are 'line', 'strict', 'exact', 'none'. 'line' - commands are matched line by line 'strict' - command lines are matched with respect to position 'exact' - command lines must be an equal match 'none' - will not compare the candidate configuration with the running configuration :param diff_ignore_lines: Use this argument to specify one or more lines that should be ignored during the diff. This is used for lines in the configuration that are automatically updated by the system. This argument takes a list of regular expressions or exact line matches. :param path: The ordered set of parents that uniquely identify the section or hierarchy the commands should be checked against. If the parents argument is omitted, the commands are checked against the set of top level or global commands. :param diff_replace: Instructs on the way to perform the configuration on the device. If the replace argument is set to I(line) then the modified lines are pushed to the device in configuration mode. If the replace argument is set to I(block) then the entire command block is pushed to the device in configuration mode if any line is not correct. :return: Configuration and/or banner diff in json format. { 'config_diff': '' } Nr)rrOrKZ diff_matchZdiff_ignore_linespathZ diff_replacerrrget_diffzs"zCliconfBase.get_diffcCsdS)aI Execute a list of commands on remote host and return the list of response :param commands: The list of command that needs to be executed on remote host. The individual command in list can either be a command string or command dict. If the command is dict the valid keys are { 'command': 'prompt': , 'answer': , 'output': , 'sendonly': } :param check_rc: Boolean flag to check if returned response should be checked for error or not. If check_rc is False the error output is appended in return response list, else if the value is True an exception is raised. :return: List of returned response Nr)rZcommandsZcheck_rcrrr run_commandsszCliconfBase.run_commandscCsl|s|std|dvr$td||r8|ds8td|rP|ddsPtd|rh|ddshtddS) Nz9must provide a candidate or replace to load configuration)TFz'commit' must be a bool, got %sZsupports_replacez&configuration replace is not supportedZsupports_commit_commentFzcommit comment is not supported) ValueErrorr#)rZ operationsrOrPrQrSrrrcheck_edit_config_capabilitys  z(CliconfBase.check_edit_config_capabilitycCsdS)z\ Ensure the command prompt on device is in right mode :return: None NrrFrrrset_cli_prompt_contextsz"CliconfBase.set_cli_prompt_contextexitcCsn|j}|dur$td|jjdt|dd}|rj||rj|jdd|||j}q$qjq$dS)a& Update the cli prompt context to ensure it is in operational mode :param config_context: It is string value to identify if the current cli prompt ends with config mode prompt :param exit_command: Command to execute to exit the config mode :return: None NzGcli prompt is not identified from the last received response window: %s)messageZsurrogate_then_replacerZvvvvz%wrong context, sending exit to device) rrr Z_last_recv_windowr rrr.rD)rZconfig_contextZ exit_commandrgrrr_update_cli_prompt_contexts   z&CliconfBase._update_cli_prompt_context)NNNFTFF)rKNN)NTNFN)NNNFTNF)N)T)NNrZr[)NNrZr[)NNNNNN)NT)NTNN)Nrq)__name__ __module__ __qualname____doc__rEr'r3rDrGrHrIr$r%rr r!r#r"rUrPrXrYrhrjrlrmrorprs __classcell__rrr+rr.s<  -     .    $  rN)Z __future__rrrtypeZ __metaclass__abcr functoolsrZansible.pluginsrZansible.errorsr r Zansible.module_utils._textr r rZr ra ImportErrorrrrrrrs