ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.59k stars 1.31k forks source link

hp_comware H3C device which in RBM mode using send_config_set with netmiko.exceptions.ReadTimeout #3286

Closed yucai100 closed 1 year ago

yucai100 commented 1 year ago

I used nornir to set H3C devices with some configuration. The devices in RBM mode got exception, others were nornal.Then I wrote a test.py try to find out where the code got wrong.

netmiko version: 4.2.0

test.py code:

from netmiko import ConnectHandler
net_info = {
    'username': 'username',
    'password': 'password',
    'ip': 'ip',
    'device_type': 'hp_comware',
    'session_log': 'session.txt'
}

ssh_client = ConnectHandler(**net_info)
cmds = ["clock protocol ntp"]
output = ssh_client.send_config_set(cmds)
print(output)

Then I got exception

Traceback (most recent call last):
  File "d:/code/nornir/src/test/netmiko_test.py", line 18, in <module>
    output = ssh_client.send_config_set(cmds)
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 2252, in send_config_set
    output += self.config_mode()
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\huawei\huawei.py", line 44, in config_mode
    return super().config_mode(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\cisco_base_connection.py", line 52, in config_mode
    return super().config_mode(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 2088, in config_mode
    output += self.read_until_prompt(read_entire_line=True)
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 812, in read_until_prompt
    return self.read_until_pattern(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 721, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:

Pattern not detected: 'BM_P<H3C.*' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.

session_log

******************************************************************************
* Copyright (c) 2004-2022 New H3C Technologies Co., Ltd. All rights reserved.*
* Without the owner's prior written consent,                                 *
* no decompiling or reverse-engineering shall be allowed.                    *
******************************************************************************

RBM_P<H3C>
RBM_P<H3C>
RBM_P<H3C>screen-length 0 temporary
                                ^
 % Unrecognized command found at '^' position.
RBM_P<H3C>
RBM_P<H3C>system-view
System View: return to User View with Ctrl+Z.
RBM_P[H3C]
ktbyers commented 1 year ago

Your code and your exception don't match?

  File "D:\Program Files\Python38\Lib\site-packages\netmiko\huawei\huawei.py", line 44, in config_mode
    return super().config_mode(

The exception message indicates you are using the Huawei driver--your code and your description state that you are using the hp_comware driver?

ktbyers commented 1 year ago

But it does look broken in either case (i.e. the RBM mode is changing the prompt) which is then breaking the underlying driver (either hp_comware or huawei).

yucai100 commented 1 year ago

sorry, I paste wrong exception. I've tried huawei driver, the exception is similar.

Traceback (most recent call last):
  File "d:/code/nornir/src/test/netmiko_test.py", line 18, in <module>
    output = ssh_client.send_config_set(cmds)
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\hp\hp_comware.py", line 61, in send_config_set
    return super().send_config_set(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 2252, in send_config_set
    output += self.config_mode()
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\hp\hp_comware.py", line 31, in config_mode
    return super().config_mode(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\cisco_base_connection.py", line 52, in config_mode
    return super().config_mode(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 2088, in config_mode
    output += self.read_until_prompt(read_entire_line=True)
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 812, in read_until_prompt
    return self.read_until_pattern(
  File "D:\Program Files\Python38\Lib\site-packages\netmiko\base_connection.py", line 721, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:

Pattern not detected: 'BM_P<JWR_JLB_FWA.*' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.
yucai100 commented 1 year ago

But it does look broken in either case (i.e. the RBM mode is changing the prompt) which is then breaking the underlying driver (either hp_comware or huawei).

Yes, I suppose that the underlying driver get the pattern between the first letter and >.

yucai100 commented 1 year ago

Maybe we can add one line of code hp_comware.py set_base_prompt method like the code in huawei.py set_base_prompt method. https://github.com/ktbyers/netmiko/blob/679be2be58a975e874fd97616c7014f0726460c1/netmiko/huawei/huawei.py#L84

replace HRP with RBM

ktbyers commented 1 year ago

Is HRP Huawei only? Does the HP Comware driver need HRP as well.

Do both the Huawei and Comware drivers need the RPM_ stripped out?

yucai100 commented 1 year ago

Sorry, I typed the wrong word before. It should be RBM, not RPM. HRP should be Huawei only. HRP is short for Huawei Redundancy Protocol. HP Comware uses RBM instead. RBM is a private protocol belong H3C.

yucai100 commented 1 year ago

I add the code in hp_comware.py of my virtual environment. It worked well.

prompt = re.sub(r"^RBM_.", "", prompt, flags=re.M)
ktbyers commented 1 year ago

Sounds good. Do you want to submit a pull-request on that (or do you want me to do it)?

yucai100 commented 1 year ago

Sounds good. Do you want to submit a pull-request on that (or do you want me to do it)?

Yeah, I pulled the request.

ktbyers commented 1 year ago

Fixed in above PR.