for 循环迭代和 netmiko 只是做第一个设备
我一直在做一些网络自动化,现在我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是我的错。该代码运行良好。我只是忘记在其他 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.