Huawei IndexError: string index out of range
See original GitHub issueHello @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 (soif not prompt[-1] ...
won’t fail withIndexError
). -
adjust
time.sleep(0.5 * delay_factor)
- sometimes it is not the best way to configure thedelay_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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Hi @stvnkem ,
thank you for your reply, I am using this workaround directly in the
Huawei.py
for now:This is working so far without any problem.
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.