【问题标题】:getting errors while making a serial connection using Netmiko - i've tried /dev/ttyS , ttyS , COM1 but no luck使用 Netmiko 进行串行连接时出错 - 我尝试过 /dev/ttyS 、 ttyS 、 COM1 但没有运气
【发布时间】:2022-01-25 16:56:44
【问题描述】:

使用 Netmiko 通过串行连接时出现以下错误

  1. 我正在使用 OpenSUSE
  2. 使用 Netmiko 连接 Cisco 2960 交换机
  3. 该脚本使用名为 commands.ios 的文本文件在串行连接后读取并执行它们,例如 - show ip int brief、显示 cdp neigh 等。
from netmiko import ConnectHandler
import serial

with open('commands.ios') as f:
    commands_list = f.read().splitlines()

ios_device = {
    "device_type": "cisco_ios_serial",
    "port": "ttyS8",
    "username": "cisco",
    "password": "password",
    "serial_settings": {
        "baudrate": 9600,
        "bytesize": serial.EIGHTBITS,
        "parity": serial.PARITY_NONE,
        "stopbits": serial.STOPBITS_ONE,
    },
}

net_connect = ConnectHandler(**ios_device)
output = net_connect.send_config_set(commands_list)
print(output)
Traceback (most recent call last):
           File "configserial.py", line 25, in <module>
    net_connect = ConnectHandler(**ios_device)
  File "/usr/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 326, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/netmiko/cisco/cisco_ios.py", line 17, in __init__
    return super().__init__(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/netmiko/base_connection.py", line 250, in __init__
    self.port = int(port)
ValueError: invalid literal for int() with base 10: 'ttyS8'

【问题讨论】:

    标签: python python-3.x netmiko


    【解决方案1】:

    您应该在serial_settings 中指定COM 端口,并且您还应该将fast_cli 设置为False

    ios_device = {
        "device_type": "cisco_ios_serial",
        "username": "cisco",
        "password": "cisco",
        "secret": "cisco",
        "fast_cli": False,  # add this line
        "serial_settings": {
            "baudrate": serial.Serial.BAUDRATES[12],  # 9600
            "bytesize": serial.EIGHTBITS,  # 8
            "parity": serial.PARITY_NONE,  # 'N'
            "stopbits": serial.STOPBITS_ONE,  # 1
            "port": "COM3",  # define your COM port here (for example COM3 on Windows or /dev/ttyUSB0 on GNU/Linux)
        },
    }
    

    一个完整的工作演示应该如下所示

    import serial
    from netmiko import ConnectHandler
    
    device = {
        "device_type": "cisco_ios_serial",
        "username": "cisco",
        "password": "cisco",
        "secret": "cisco",
        "fast_cli": False,  # add this line
        "conn_timeout": 30.0, # add this line as well
        "serial_settings": {
            "baudrate": serial.Serial.BAUDRATES[12],  # 9600
            "bytesize": serial.EIGHTBITS,  # 8
            "parity": serial.PARITY_NONE,  # 'N'
            "stopbits": serial.STOPBITS_ONE,  # 1
            "port": "COM3",  # define your COM port here
        },
    }
    
    with open(file="commands.ios", mode="r") as f:
        commands_list = f.read().splitlines()
    
    print("Connecting...")
    with ConnectHandler(**device) as conn:
        print("Connected")
        if not conn.check_enable_mode():
            conn.enable()
        output = conn.send_config_set(commands_list)
    print(output)
    

    【讨论】:

    • 在我的 Linux Box 中,我看不到 /dev/ttyUSB0 之类的东西。我只能看到 /dev/ttyS8 与 pyserial 完美配合,但在这段代码中 /dev/ttyS8 不起作用,我也厌倦了 /dev/ttyUSB8 但没有运气。
    • @DeathS7roke 您可以在 Linux 上找到端口号: 1. 打开终端并输入 ls /dev/tty*。 2. 从输出中找出您连接到哪个端口并在片段中使用。
    • 当我发出 ls /dev/tty* 它给我以下输出:- ls /dev/tty* /dev/tty /dev/ttyS110 /dev/ttyS126 /dev/ttyS141 /开发/ttyS157 /dev/ttyS172 /dev/ttyS188 /dev/ttyS30 /dev/ttyS46 /dev/ttyS61 /dev/ttyS77 /dev/ttyS92 /dev/tty0 /dev/ttyS111 /dev/ttyS127 /dev/ttyS142 /dev/ ttyS158 /dev/ttyS173 /dev/ttyS189 /dev/ttyS31 /dev/ttyS47 /dev/ttyS62 /dev/ttyS78 /dev/ttyS93 /dev/tty1 /dev/ttyS112 /dev/ttyS128 /dev/ttyS8 ...等还有很多。但我知道我的实际端口号是 /dev/ttyS8。但是我的脚本中有“端口”:“/dev/ttyS8”?
    • 您在第一个字典中设置(“device_type”:“cisco_ios_serial”),但在最后您设置(ConnectHandler(**ios_device))。这些 *** 设备类型 *** 应该相同。
    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2010-12-03
    • 2015-02-12
    • 2021-09-29
    相关资源
    最近更新 更多