简体   繁体   English

如何在 python 中使用 netmiko 在多个 cisco 设备上执行 ssh

[英]How to do ssh on multiple cisco devices using netmiko in python

I am trying run this below script for multiple devices and it is working only for last device according to below script.我正在尝试为多个设备运行下面的脚本,它仅适用于根据以下脚本的最后一个设备。

Please can you verify the below script as I need to execute both device output using for loop statement.请您验证以下脚本,因为我需要使用 for 循环语句执行两个设备输出。

from netmiko import ConnectHandler 
from getpass import getpass

password= getpass()

RTR_01 = {

         'device_type': 'cisco_ios',
         'host': '10.10.10.10',
         'username': 'admin',
         'password': password,
}

RTR_02 = { 'device_type': 'cisco_ios', 
           'host': '10.10.10.11', 
           'username': 'admin', 
           'password': password, }

device_list = [RTR_01,RTR_02] 

for device in device_list: print('Connecting to the device :' + device ['host']) 

net_connect = ConnectHandler(**device)

output = net_connect.send_command('show ip interface brief')
print(output)
output = net_connect.send_command('show version')
print(output)

I now how it working 100% correctly.我现在如何 100% 正确工作。 I had tried net_connect.disconnect() with the appropriate indentations which didn't work because once you exit the indent for the loop, it automatically exits.我曾尝试过使用适当的缩进的net_connect.disconnect() ,但它不起作用,因为一旦退出循环的缩进,它就会自动退出。 The problem, silly as it is, was that I was still connected to the actual device and was receiving an error that I had overlooked complaining about not being able to create /var/home/myusername/.ssh问题虽然很愚蠢,但我仍然连接到实际设备并收到一个我忽略的错误,抱怨无法创建/var/home/myusername/.ssh

All I needed to do was issue the following command at the very end of the loop:我需要做的就是在循环的最后发出以下命令:

net_connect.write_channel('exit\\n')

Such as silly mistake and so much time wasted but the lesson was valuable!诸如愚蠢的错误和浪费了这么多时间,但教训是宝贵的! Maybe it can help someone else here.也许它可以帮助这里的其他人。

You need these lines to indented within the for loop你需要这些行在 for 循环中缩进

net_connect = ConnectHandler(**device)

output = net_connect.send_command('show ip interface brief')
print(output)
output = net_connect.send_command('show version')
print(output)

Your code should look like this:您的代码应如下所示:

from getpass import getpass

from netmiko import ConnectHandler

password = getpass()

RTR_01 = {
    "device_type": "cisco_ios",
    "host": "10.10.10.10",
    "username": "admin",
    "password": password,
}

RTR_02 = {
    "device_type": "cisco_ios",
    "host": "10.10.10.11",
    "username": "admin",
    "password": password,
}

device_list = [RTR_01, RTR_02]

for device in device_list:
    print("Connecting to the device :" + device["host"])

    net_connect = ConnectHandler(**device)

    output = net_connect.send_command("show ip interface brief")
    print(output)
    output = net_connect.send_command("show version")
    print(output)

    net_connect.disconnect() # to clear the vty line when done

And this is a better version of your code that does the same exact thing:这是你的代码的更好版本,它做同样的事情:

from getpass import getpass

from netmiko import ConnectHandler

password = getpass()

ipaddrs = ["10.10.10.10", "10.10.10.11"]

# A list comprehension
devices = [
    {
        "device_type": "cisco_ios",
        "host": ip,
        "username": "admin",
        "password": password,
    }
    for ip in ipaddrs
]

for device in devices:
    print(f'Connecting to the device: {device["host"]}')

    with ConnectHandler(**device) as net_connect:  # Using Context Manager
        intf_brief = net_connect.send_command(
            "show ip interface brief"
        )  # Inside the connection
        facts = net_connect.send_command("show version")  # Inside the connection

        # Notice here I didn't call the `net_connect.disconnect()`
        # because the `with` statement automatically disconnects the session.

    # On this indentation level (4 spaces), the connection is terminated
    print(intf_brief)
    print(facts)

The outputs ( intf_brief and facts ) are printed outside the connection, because the session is not needed anymore to print any collected values.输出( intf_brieffacts )在连接外打印,因为不再需要会话来打印任何收集的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM