代码之家  ›  专栏  ›  技术社区  ›  radmanesh1982

我想使用python代码来配置cisco交换机

  •  0
  • radmanesh1982  · 技术社区  · 2 年前

    我刚刚为cisco交换机使用netmiko创建了一个python代码,但当我运行它时收到了错误。。。 我想使用命令来启用开关模式,但它不起作用。。。

    from netmiko import ConnectHandler
    
    
    cisco_Router = {
        "device_type": "cisco_ios_telnet",
        "host": "123.123.1.2",
        "username": "username",
        "password": "password"}
    
    with ConnectHandler(**cisco_Router) as net_connect:
    
        net_connect.enable()
        result = net_connect.send_command('sh run')
        net_connect.disconnect()
    
    print(result)
    

    这是错误:

    Traceback (most recent call last):
      File "E:\netmiko\sw_enable.py", line 12, in <module>
        net_connect.enable()
      File "C:\Users\Siavash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\netmiko\cisco_base_connection.py", line 26, in enable
        return super().enable(
      File "C:\Users\Siavash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\netmiko\base_connection.py", line 2042, in enable
        output += self.read_until_prompt()
      File "C:\Users\Siavash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\netmiko\base_connection.py", line 837, in read_until_prompt
        return self.read_until_pattern(
      File "C:\Users\Siavash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\netmiko\base_connection.py", line 746, in read_until_pattern
        raise ReadTimeout(msg)
    netmiko.exceptions.ReadTimeout: 
    
    Pattern not detected: 'switch' 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.
    
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   meldl2    2 年前

    一个问题可能是netmiko设备类型不支持在交换机上运行的操作系统。如果不能选择通过SSH连接,则有4个telnet设备类型可用,即ciscoios_telnet、ciscoxr_telnet、cisco s200_telnet和cisco s300_telet。试着将你的设备类型更改为其中一个,看看其中一个是否有效。否则,请将交换机配置为使用SSH,然后尝试与交换机操作系统最匹配的众多设备类型之一。

        2
  •  0
  •   pyjedy    2 年前

    看起来Netmiko在识别Cisco交换机上的启用模式提示时遇到了问题。Netmiko使用正则表达式来检测提示并确定其何时处于启用模式。

    在您的情况下,错误消息表明在尝试进入启用模式时,在输出中未检测到模式“switch”。要解决此问题,可以在设备字典中显式设置enable_prompt,以帮助Netmiko识别启用提示。

    from netmiko import ConnectHandler
    
    cisco_Router = {
        "device_type": "cisco_ios_telnet",
        "host": "123.123.1.2",
        "username": "username",
        "password": "password",
        "secret": "password",  # Add the enable password here
        "fast_cli": False,  # Disable fast_cli to improve reliability
        "verbose": True  # Enable verbose mode for debugging
    }
    
    with ConnectHandler(**cisco_Router) as net_connect:
        net_connect.enable()
        result = net_connect.send_command('sh run')
        net_connect.disconnect()
    
    print(result)