Network engineers learn Python-36-Netmiko, a multi-vendor network equipment automation management library

Python Netmiko is a multi-vendor network device automation management library based on Paramiko. It supports SSH and Telnet protocols, and can support network devices from multiple vendors, such as Cisco, Juniper, Arista, HP, etc.

This article will introduce the basic usage of Python Netmiko and several examples, including connecting devices, sending commands and configuring devices.

Install

Install Python Netmiko using pip:

pip install netmiko

Connect devices

from netmiko import ConnectHandler

# 设备连接信息
device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'admin',
}

# 连接设备
conn = ConnectHandler(**device)

# 断开连接
conn.disconnect()

The above code uses ConnectHandlermethods to connect to Cisco IOS devices. Among them, device_typethe parameter is used to specify the type of device, ipthe parameter specifies the IP address of the device, usernameand passwordthe parameter specifies the user name and password required to connect to the device. **deviceParameter means deviceunpacking all key-value pairs in the dictionary and passing them to ConnectHandlerthe method as parameters.

send command

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'admin',
}

# 连接设备
conn = ConnectHandler(**device)

# 发送命令
output = conn.send_command('show interface GigabitEthernet0/0/0')
print(output)

# 断开连接
conn.disconnect()

In the above code, we use send_commandmethods to send commands to the device. This method returns the output returned by the device and stores it in outputa variable.

Configure device

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'admin',
}

# 连接设备
conn = ConnectHandler(**device)

# 配置设备
config_commands = ['interface GigabitEthernet0/0/0', 'ip address 192.168.1.2 255.255.255.0']
output = conn.send_config_set(config_commands)
print(output)

# 保存配置
output = conn.save_config()
print(output)

# 断开连接
conn.disconnect()

In the above code, we use send_config_setthe method to send configuration commands to the device. This method sends the commands in the list to the device and returns the output returned by the device. We then use save_configthe method to save the configuration changes.

Batch configuration

from netmiko import ConnectHandler
from getpass import getpass

password = getpass()

with open('devices.txt') as f:
    devices = f.read().splitlines()

for ip in devices:
    device = {
        'device_type': 'cisco_ios',
        'ip': ip,
        'username': 'admin',
        'password': password,
        'port': 22,
    }
    ssh = ConnectHandler(**device)
    config_commands = ['interface loopback 1', 'ip address 1.1.1.1 255.255.255.255']
    output = ssh.send_config_set(config_commands)
    print(output)
    ssh.disconnect()

The above code shows how to use Netmiko to connect to multiple devices and execute configuration commands in batches. In this example, the IP address is read from the devices.txt file and the same username and password are used to connect to the device. Then, use the send_config_set method to send the configuration command and print the output.

Using Netmiko subclasses

from netmiko import ConnectHandler
from netmiko.cisco import CiscoIosBase

class MyDevice(CiscoIosBase):
    def backup_running_config(self):
        filename = f"{self.host}-running-config.txt"
        command = "show running-config"
        output = self.send_command(command)
        with open(filename, 'w') as f:
            f.write(output)

device = {
    'device_type': 'cisco_ios',
    'ip': '10.0.0.1',
    'username': 'admin',
    'password': 'password',
    'port': 22,
}

ssh = MyDevice(**device)
ssh.backup_running_config()
ssh.disconnect()

The above code shows how to use Netmiko's subclasses to extend its functionality. In this example, a subclass called MyDevice is created and a method is added to backup the running-config. Then, use the MyDevice class to connect to the device, call the backup_running_config method, and disconnect.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132638724