async def send_configs( self, configs: List[str], strip_prompt: bool = True, failed_when_contains: Optional[Union[str, List[str]]] = None, stop_on_failed: bool = False, privilege_level: str = "", ) -> ScrapliMultiResponse: """ Send configuration(s) Args: configs: list of strings to send to device in config mode strip_prompt: True/False strip prompt from returned output failed_when_contains: string or list of strings indicating failure if found in response stop_on_failed: True/False stop executing commands if a command fails, returns results as of current execution; aborts configuration session if applicable (iosxr/junos or eos/nxos if using a configuration session) privilege_level: name of configuration privilege level/type to acquire; this is platform dependent, so check the device driver for specifics. Examples of privilege_name would be "configuration_exclusive" for IOSXRDriver, or "configuration_private" for JunosDriver. You can also pass in a name of a configuration session such as "my-config-session" if you have registered a session using the "register_config_session" method of the EOSDriver or NXOSDriver. Returns: ScrapliMultiResponse: Scrapli MultiResponse object Raises: N/A """ resolved_privilege_level, failed_when_contains = self._pre_send_configs( configs=configs, failed_when_contains=failed_when_contains, privilege_level=privilege_level, ) if self._current_priv_level.name != resolved_privilege_level: await self.acquire_priv(desired_priv=resolved_privilege_level) responses = MultiResponse() _failed_during_execution = False for config in configs: response = await super().send_command( command=config, strip_prompt=strip_prompt, failed_when_contains=failed_when_contains, ) responses.append(response) if response.failed is True: _failed_during_execution = True if stop_on_failed is True: break if _failed_during_execution is True: await self._abort_config() return self._post_send_configs(responses=responses)
def test_post_send_configs(sync_cisco_iosxe_conn): response_one = Response("localhost", "some input", failed_when_contains=["something"]) response_one._record_response(result=b"greatsucccess") multi_response = MultiResponse() multi_response.append(response_one) updated_responses = sync_cisco_iosxe_conn._post_send_configs(responses=multi_response) assert updated_responses[0].textfsm_platform == "cisco_iosxe" assert updated_responses[0].genie_platform == "iosxe"
def test_post_send_config_failed(sync_cisco_iosxe_conn): response_one = Response("localhost", "some input", failed_when_contains=["something"]) response_one._record_response(result=b"something") multi_response = MultiResponse() multi_response.append(response_one) updated_responses = sync_cisco_iosxe_conn._post_send_config( config="whocares", multi_response=multi_response ) assert updated_responses.textfsm_platform == "cisco_iosxe" assert updated_responses.genie_platform == "iosxe" assert updated_responses.failed is True
def test_post_send_config(sync_cisco_iosxe_conn): response_one = Response("localhost", "some input", failed_when_contains=["something"]) response_one._record_response(result=b"greatsucccess") response_two = Response("localhost", "some input") response_two._record_response(result=b"alsosucess") multi_response = MultiResponse() multi_response.append(response_one) multi_response.append(response_two) unified_response = sync_cisco_iosxe_conn._post_send_config( config="interface loopback0\ndescription scrapli is neat", multi_response=multi_response) assert unified_response.failed is False assert unified_response.result == "greatsucccess\nalsosucess"
def send_commands( self, commands: List[str], strip_prompt: bool = True, failed_when_contains: Optional[Union[str, List[str]]] = None, stop_on_failed: bool = False, ) -> ScrapliMultiResponse: """ Send multiple commands Args: commands: list of strings to send to device in privilege exec mode strip_prompt: True/False strip prompt from returned output failed_when_contains: string or list of strings indicating failure if found in response stop_on_failed: True/False stop executing commands if a command fails, returns results as of current execution Returns: responses: list of Scrapli Response objects Raises: TypeError: if commands is anything but a list """ if not isinstance(commands, list): raise TypeError( f"`send_commands` expects a list of strings, got {type(commands)}. " "to send a single command use the `send_command` method instead." ) responses = MultiResponse() for command in commands: response = self.send_command( command=command, strip_prompt=strip_prompt, failed_when_contains=failed_when_contains, ) responses.append(response) if stop_on_failed is True and response.failed is True: return responses return responses