# Buggy code
def receive(self, command=None, prompts=None, answer=None, newline=True):
    """
    Handles receiving of output from command
    """
    recv = BytesIO()
    handled = False

    self._matched_prompt = None

    while True:
        data = self._ssh_shell.recv(256)

        # when a channel stream is closed, received data will be empty
        if not data:
            break

        recv.write(data)
        offset = recv.tell() - 256 if recv.tell() > 256 else 0
        recv.seek(offset)

        window = self._strip(recv.read())
        if prompts and not handled:
            handled = self._handle_prompt(window, prompts, answer, newline)
        elif prompts and handled:
            # check again even when handled, a sub-prompt could be
            # repeating (like in the case of a wrong enable password, etc)
            self._handle_prompt(window, prompts, answer, newline)

        if self._find_prompt(window):
            self._last_response = recv.getvalue()
            resp = self._strip(self._last_response)
            return self._sanitize(resp, command)


# Traceback (ConnectionError)
TASK [Config Using Core Module] ************************************************************************************************************************************************************
task path: /Users/gnalawad/targets/ios_interface.yml:21
<ios> attempting to start connection
<ios> using connection plugin network_cli
<ios> found existing local domain socket, using it!
<ios>
The full traceback is:
Traceback (most recent call last):
File "/Users/home/ansible/bin/ansible-connection", line 253, in main
messages.extend(conn.update_play_context(pc_data))
File "/Users/home/ansible/lib/ansible/module_utils/connection.py", line 146, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
ConnectionError: 'NoneType' object has no attribute 'endswith'

fatal: [ios]: FAILED! => {
"failed": true,
"msg": "'NoneType' object has no attribute 'endswith'"


The full traceback is:
Traceback (most recent call last):
File "/Users/home/ansible/bin/ansible-connection", line 253, in main
messages.extend(conn.update_play_context(pc_data))
File "/Users/home/ansible/lib/ansible/module_utils/connection.py", line 146, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
ConnectionError: unable to elevate privilege to enable mode, at prompt [None]

fatal: [ios]: FAILED! => {
"failed": true,
"msg": "unable to elevate privilege to enable mode, at prompt [None]"
}

# Fixed code
def receive(
    self,
    command=None,
    prompts=None,
    answer=None,
    newline=True,
    prompt_retry_check=False,
):
    """
    Handles receiving of output from command
    """
    recv = BytesIO()
    handled = False

    self._matched_prompt = None
    self._matched_cmd_prompt = None
    matched_prompt_window = window_count = 0

    while True:
        data = self._ssh_shell.recv(256)

        # when a channel stream is closed, received data will be empty
        if not data:
            break

        recv.write(data)
        offset = recv.tell() - 256 if recv.tell() > 256 else 0
        recv.seek(offset)

        window = self._strip(recv.read())
        window_count += 1

        if prompts and not handled:
            handled = self._handle_prompt(window, prompts, answer, newline)
            matched_prompt_window = window_count
        elif (
            prompts
            and handled
            and prompt_retry_check
            and matched_prompt_window + 1 == window_count
        ):
            # check again even when handled, if same prompt repeats in next window
            # (like in the case of a wrong enable password, etc) indicates
            # value of answer is wrong, report this as error.
            if self._handle_prompt(
                window, prompts, answer, newline, prompt_retry_check
            ):
                raise AnsibleConnectionFailure(
                    "For matched prompt '%s', answer is not valid"
                    % self._matched_cmd_prompt
                )

        if self._find_prompt(window):
            self._last_response = recv.getvalue()
            resp = self._strip(self._last_response)
            return self._sanitize(resp, command)