question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Huawei IndexError: string index out of range

See original GitHub issue

Hello @ktbyers , from time to time (seems like when my Huawei router is little bit slower than usual) I got this error:

 Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/nornir/core/task.py", line 85, in start
    r = self.task(self, **self.params)
  File "/usr/local/lib/python3.8/dist-packages/nornir/plugins/tasks/networking/netmiko_send_command.py", line 26, in netmiko_send_command
    net_connect = task.host.get_connection("netmiko", task.nornir.config)
  File "/usr/local/lib/python3.8/dist-packages/nornir/core/inventory.py", line 294, in get_connection
    self.open_connection(
  File "/usr/local/lib/python3.8/dist-packages/nornir/core/inventory.py", line 345, in open_connection
    self.connections[connection].open(
  File "/usr/local/lib/python3.8/dist-packages/nornir/plugins/connections/netmiko.py", line 58, in open
    self.connection = ConnectHandler(**parameters)
  File "/usr/local/lib/python3.8/dist-packages/netmiko/ssh_dispatcher.py", line 266, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 327, in __init__
    self._open()
  File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 333, in _open
    self._try_session_preparation()
  File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 756, in _try_session_preparation
    self.session_preparation()
  File "/usr/local/lib/python3.8/dist-packages/netmiko/huawei/huawei.py", line 13, in session_preparation
    self.set_base_prompt()
  File "/usr/local/lib/python3.8/dist-packages/netmiko/huawei/huawei.py", line 95, in set_base_prompt
    if not prompt[-1] in (pri_prompt_terminator, alt_prompt_terminator):
IndexError: string index out of range

I was trying to debug this and I added few print statement lines into the netmiko/huawei/huawei.py:

        # log.debug("In set_base_prompt")
        delay_factor = self.select_delay_factor(delay_factor)
        print(f'delay_factor: {delay_factor}')
        print(f'Buffer before clear: {self.read_channel()}')
        self.clear_buffer()
        self.write_channel(self.RETURN)
        time.sleep(0.5 * delay_factor)

        prompt = self.read_channel()
        print(f'prompt: "{prompt}"')

When there is a failure indeed the prompt variable is empty ( None ):

delay_factor: 2
Buffer before clear: 
prompt: ""

Because I suspect a delay issue I added a little check and hard coded a 5s wait:

        if not prompt:
            print('No prompt, waiting another 5s')
            time.sleep(5)
            prompt = self.read_channel()
            print(f'prompt after 5s: {prompt}')
            if not prompt:
                raise ValueError('Empty prompt')

And it works, prompt was found:

delay_factor: 3
Buffer before clear: 
prompt: ""
No prompt, waiting another 5s
prompt after 5s: "<my_router>"

More info about my setup:

  • netmiko==3.2.0

  • I am using Nornir and it’s plugin netmiko_send_command

  • my Nornir timers are configured as followed:

  connection_options:
        netmiko:
            extras:
                use_keys: True
                ssh_strict: False
                timeout: 60
                session_timeout: 60
                auth_timeout: 60
                blocking_timeout: 60
                banner_timeout: 60
                global_delay_factor: 2

Proposed solution

  • check if variable prompt is not empty before going further in the code (so if not prompt[-1] ... won’t fail with IndexError).

  • adjust time.sleep(0.5 * delay_factor) - sometimes it is not the best way to configure the delay_factor to some high value, e.g. if there are multiple connections done at the same time (like via Nornir) - it can delay other operations when a router is really not reachable (and the connection should fait/timeout) -> this would cause long delay before proceeding to other tasks or even failures in other time depended tasks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
m1009dcommented, Sep 10, 2020

Hi @stvnkem ,
thank you for your reply, I am using this workaround directly in the Huawei.py for now:

...
prompt = self.read_channel()

        if not prompt:
            time.sleep(5)
            prompt = self.read_channel()
            if not prompt:
                raise ValueError('Empty prompt')
...

This is working so far without any problem.

0reactions
ktbyerscommented, Sep 16, 2022

I am going to close this as it doesn’t look like there is any additional action to be taken.

If there is still something to do here, just re-open this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IndexError: string index out of range - STechies
To access a specific element, you have to mention its index value. But in some cases you may encounter an error called “IndexError...
Read more >
IndexError: String Index out of Range - The Renegade Coder
In case you're short on time, this IndexError arises when a string is accessed using an invalid index. For example, the string “VirtualFlat”...
Read more >
Python TypeError: string index out of range Solution
The “TypeError: string index out of range” error is raised when you try to access an item at an index position that does...
Read more >
IndexError: string index out of range - Net-Informations.Com
In Python, a string is a single-dimensional array of characters. Python: The string index out of range means that the index you are...
Read more >
String Index Out Of Range Python - MindMajix Community
The string index out of range indicates that the index you are attempting to reach does not exist. Within a string, that implies...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found