for 循环迭代和 netmiko 只是做第一个设备

发布于 2025-01-10 04:20:42 字数 1358 浏览 0 评论 0原文

我一直在做一些网络自动化,现在我正在使用 lops 来一次不仅可以做一个,所以当我运行下面的代码时,我看到第一个路由器是成功的,但第二个路由器有错误 netmiko。 ssh_exception.NetmikoTimeoutException:读取通道超时,数据不可用。

所以我用 3 个路由器进行测试,第一个运行良好,但第二个却无法工作。

from netmiko import Netmiko
from getpass import getpass
import re

router1 = {
    "host": "10.0.0.248",
    "username": "cisco",
    "password": "cisco",
    "device_type": "cisco_ios",
}

router2 = {
    "host": "10.0.0.247",
    "username": "cisco",
    "password": "cisco",
    "device_type": "cisco_ios",
}

router3 = {
    "host": "10.0.0.246",
    "username": "cisco",
    "password": "cisco",
    "device_type": "cisco_ios",
}

routers = [router1, router2, router3]

interface_shut = list()

for router in routers:
    net_connect = Netmiko(**router)
    print(net_connect.find_prompt())
    output = net_connect.send_command("show ip int br")
    print(output)
    find_interfaces = re.findall("GigabitEthernet\d.\d", output)
    print(find_interfaces)
    for int in find_interfaces:
        interface_shut.append("Interface " + int)
        interface_shut.append("no shut")
        output2 = net_connect.send_config_set(interface_shut)
        print(output2)
        interface_shut = list() # I am doing this so the list doesnt have to have more than 3 items for each device
        print(interface_shut)
    net_connect.disconnect()    

I have been doing some network automation, now I am working with for lops to be able to do not only one at time, so when I ran the code below I see the first router it is successful but the second one has the error netmiko.ssh_exception.NetmikoTimeoutException: Timed-out reading channel, data not available.

So I am testing with 3 routers, the first goes well but the second one never works.

from netmiko import Netmiko
from getpass import getpass
import re

router1 = {
    "host": "10.0.0.248",
    "username": "cisco",
    "password": "cisco",
    "device_type": "cisco_ios",
}

router2 = {
    "host": "10.0.0.247",
    "username": "cisco",
    "password": "cisco",
    "device_type": "cisco_ios",
}

router3 = {
    "host": "10.0.0.246",
    "username": "cisco",
    "password": "cisco",
    "device_type": "cisco_ios",
}

routers = [router1, router2, router3]

interface_shut = list()

for router in routers:
    net_connect = Netmiko(**router)
    print(net_connect.find_prompt())
    output = net_connect.send_command("show ip int br")
    print(output)
    find_interfaces = re.findall("GigabitEthernet\d.\d", output)
    print(find_interfaces)
    for int in find_interfaces:
        interface_shut.append("Interface " + int)
        interface_shut.append("no shut")
        output2 = net_connect.send_config_set(interface_shut)
        print(output2)
        interface_shut = list() # I am doing this so the list doesnt have to have more than 3 items for each device
        print(interface_shut)
    net_connect.disconnect()    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2025-01-17 04:20:42

那是我的错。该代码运行良好。我只是忘记在其他 2 个路由器中添加 priv 15。

That was my bad. The code is working fine. I just forgot to add the priv 15 in the other 2 routers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文