【问题标题】:How do I connect to NetMiko through a serial connection?如何通过串行连接连接到 NetMiko?
【发布时间】:2021-10-10 20:27:31
【问题描述】:

如何通过 NetMiko 上的串行总线连接?我知道this question 3 年前问过。但是,它似乎不再相关。

我有以下代码。

from netmiko import ConnectHandler

device = {
    "device_type": "aruba_osswitch",
    "username": "manager",
    "password": "",
    "serial_settings": {"port": "COM4"}
}

net_connect = ConnectHandler(**device)

output = net_connect.send_command("show version")

print(output)

我收到错误:ValueError:必须设置 ip 或主机。 但是,由于它是串行的,据我所知,它不需要主机或 IP。有人可以建议吗?

谢谢,

【问题讨论】:

  • 不幸的是,到 aruba 交换机的串行连接未在 Netmiko 中实现。我唯一能看到的是思科设备。每当您想使用串行电缆连接到设备时,只需将_serial 附加到设备类型即可。对于 cisco 设备,它类似于cisco_ios_serial。如果你想自己做,尝试使用pyserial library。

标签: python netmiko


【解决方案1】:

我使用了以下代码片段: - https://semfionetworks.com/blog/establish-a-console-connection-within-a-python-script-with-pyserial/ An 通过在 OpenSUSE OS 上稍作修改来运行这个

$import serial
$from time import sleep


$def send_to_console(ser: serial.Serial, command: str, wait_time: float = 0.5):
     $command_to_send = command + "\r"
     $ser.write(command_to_send.encode('utf-8'))
     $sleep(wait_time)
     $print(ser.read(ser.inWaiting()). decode('utf-8'), end="") 

$with serial.Serial("/dev/ttyS8", timeout=1) as ser:
     $print(f"Connecting to {ser.name}...")
     $send_to_console(ser, "")
     $send_to_console(ser, "enable")
     $send_to_console(ser, "show ip interface brief", wait_time=2)
     $print(f"Connection to {ser.name} closed.")

【讨论】:

    【解决方案2】:

    正如我在评论中提到的,不幸的是,Netmiko 中没有实现到 Aruba 交换机的串行连接。我唯一能看到的是思科设备。每当您想使用串行电缆连接到设备时,只需将_serial 附加到设备类型(如果它在 Netmiko 中实现)。对于 Cisco 设备,它类似于{"device_type": "cisco_ios_serial"}。如果您想自己做,请尝试使用 pySerial 库。

    Netmiko 已经使用pySerial 进行串行连接。

    这是一个使用pySerial API 的gist,它与设备无关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多