Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
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"
Exemplo n.º 3
0
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"
Exemplo n.º 5
0
def test_multi_response():
    response1 = Response("localhost", "ls -al")
    response2 = Response("localhost", "ls -al")
    multi_response = MultiResponse([response1, response2])
    assert len(multi_response) == 2
    assert multi_response.failed is True
    assert repr(multi_response) == "MultiResponse <Success: False; Response Elements: 2>"
    assert str(multi_response) == "MultiResponse <Success: False; Response Elements: 2>"
    with pytest.raises(ScrapliCommandFailure):
        multi_response.raise_for_status()
    multi_response[0].failed = False
    multi_response[1].failed = False
    assert multi_response.failed is False
    assert multi_response.raise_for_status() is None
    assert repr(multi_response) == "MultiResponse <Success: True; Response Elements: 2>"
    assert str(multi_response) == "MultiResponse <Success: True; Response Elements: 2>"
Exemplo n.º 6
0
def test_multi_response():
    host = "localhost"
    response1 = Response(host, "ls -al")
    response2 = Response(host, "ls -al")
    multi_response = MultiResponse()
    assert multi_response.host == ""
    multi_response.extend([response1, response2])
    assert len(multi_response) == 2
    assert multi_response.failed is True
    assert (
        repr(multi_response) ==
        "[Response(host='localhost',channel_input='ls -al',textfsm_platform='',genie_platform='',failed_when_contains=None), Response(host='localhost',channel_input='ls -al',textfsm_platform='',genie_platform='',failed_when_contains=None)]"
    )
    assert str(multi_response
               ) == "MultiResponse <Success: False; Response Elements: 2>"
    with pytest.raises(ScrapliCommandFailure):
        multi_response.raise_for_status()
    multi_response[0].failed = False
    multi_response[1].failed = False
    assert multi_response.failed is False
    assert multi_response.raise_for_status() is None
    assert multi_response.host == host
    assert (
        repr(multi_response) ==
        "[Response(host='localhost',channel_input='ls -al',textfsm_platform='',genie_platform='',failed_when_contains=None), Response(host='localhost',channel_input='ls -al',textfsm_platform='',genie_platform='',failed_when_contains=None)]"
    )
    assert str(multi_response
               ) == "MultiResponse <Success: True; Response Elements: 2>"
    assert multi_response.result == "ls -al\nls -al\n"
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
    def _pre_send_commands(commands: List[str]) -> MultiResponse:
        """
        Handle pre "send_command" tasks for consistency between sync/async versions

        Args:
            commands: list of strings to send to device in privilege exec mode

        Returns:
            MultiResponse: Scrapli MultiResponse object

        Raises:
            ScrapliTypeError: if command is anything but a string

        """
        if not isinstance(commands, list):
            raise ScrapliTypeError(
                f"`send_commands` expects a list of strings, got {type(commands)}, "
                "to send a single command use the `send_command` method instead."
            )

        return MultiResponse()