What is a Netmiko

Is based on paramiko secondary encapsulation, you can connect a variety of network devices through SSH library, from 3.0 version, added to the support of domestic manufacturers, is a very good network automation tool.

At present, the supported devices almost cover most of the devices on the market, including Ruijie, Huasan, Huawei, Cisco, F5 and so on. See the detailed list below.

How to use

The installation

Python3.6 or above is required, which can be installed directly with PIP. Domestic download speed is slow, you can refer to # Python for PIP acceleration: Set domestic pypi source for configuration.

# installation
pip install netmiko
Copy the code

If Successfully Installed Netmiko is displayed, the installation is successful.

use

Experimental environment: Windows 10, Python 3.8, HCL 2.1.2

Basic usage

# Import handles the connection first
from netmiko import ConnectHandler as ch

# Define device login information in dictionary mode
host = {
    'device_type': 'hp_comware'.'host': '192.168.56.20'.'username': 'netdevops'.'password': 'netdevops'.'port': 22.'secret': ' '.# enable: no password is allowed
}
Conn can be interpreted as a terminal already connected to the device and can be executed directly
conn = ch(**host)
# Use the send_command method to execute the command to view the interface information. The return value is a string
output = conn.send_command('display ip int brief')
print(output)
Copy the code

Below is the output information. You can see that the required information has been collected from the device

Running Multiple Commands

from netmiko import ConnectHandler as ch
# Define device login information in dictionary mode
host = {
    'device_type': 'hp_comware'.'host': '192.168.56.20'.'username': 'netdevops'.'password': 'netdevops'.'port': 22.'secret': ' '.# enable: no password is allowed
}
# Connect device
conn = ch(**host)
# Define a list of commands, such as an IP address for g0/1
commands = ['int g0/1'.'ip add 1.1.1.1 30'.'desc netmiko_config']
You can use the send_config_set method to execute multiple commands at this time
output = conn.send_config_set(commands)
print(output)
Copy the code

The following is the result after execution

The configuration is complete on the device.

What happens when you log in to the device?

Netmiko can configure network devices through SSH, so what does it do when it logs in to the device? We can see relevant information from the LOG of the network device. The following are some logs in this experiment:

def send_command():

As can be seen from the second part of the LOG, when the send_command method is executed, Netmiko first enters the screen-length disable command to cancel the split screen, and then runs the corresponding command. In this way, the output content is guaranteed to be complete when there isa large amount of output.

In fact, Netmiko ADAPTS other devices, such as Cisco and Huawei devices. After logging in to the device, you will run the corresponding command to cancel the split screen.

Note the following: The user name used to log in to the device must have the permission to run the split screen command. For example, if the level 0 user does not run the command, the information cannot be collected.

def send_config_set():

As can be seen from the first part of the LOG, when the send_config_set method was executed, Netmiko not only entered the command to cancel the split screen, but also executed the system-view command to enter the system view, and then executed the command we entered.

The method’s name send_config_set shows that Netmiko thinks this is a series of configuration commands, so it directly puts us into the system view.

Compared with other devices such as Cisco, Netmiko will run the config terminal command for us to enter the configuration mode. At this time, the show command for Cisco devices cannot be executed in batches (do show is required for Cisco devices in the configuration mode).

When the command is executed, Netmiko helps us exit the system view and then disconnects the SSH connection.

A second point here: To use the send_config_set method, you need the user to have configuration rights. This method is usually used to configure the brush in.


List of supported devices

a10
accedian
alcatel_aos
alcatel_sros
apresia_aeos
arista_eos
aruba_os
avaya_ers
avaya_vsp
brocade_fastiron
brocade_netiron
brocade_nos
brocade_vdx
brocade_vyos
calix_b6
checkpoint_gaia
ciena_saos
cisco_asa
cisco_ios
cisco_nxos
cisco_s300
cisco_tp
cisco_wlc
cisco_xe
cisco_xr
cloudgenix_ion
coriant
dell_dnos9
dell_force10
dell_isilon
dell_os10
dell_os6
dell_os9
dell_powerconnect
dlink_ds
eltex
eltex_esr
endace
enterasys
extreme
extreme_ers
extreme_exos
extreme_netiron
extreme_nos
extreme_slx
extreme_vdx
extreme_vsp
extreme_wing
f5_linux
f5_ltm
f5_tmsh
flexvnf
fortinet
generic_termserver
hp_comware
hp_procurve
huawei
huawei_olt
huawei_smartax
huawei_vrpv8
ipinfusion_ocnos
juniper
juniper_junos
juniper_screenos
keymile
keymile_nos
linux
mellanox
mellanox_mlnxos
mikrotik_routeros
mikrotik_switchos
mrv_lx
mrv_optiswitch
netapp_cdot
netscaler
nokia_sros
oneaccess_oneos
ovs_linux
paloalto_panos
pluribus
quanta_mesh
rad_etx
ruckus_fastiron
ruijie_os
sophos_sfos
ubiquiti_edge
ubiquiti_edgeswitch
ubiquiti_unifiswitch
vyatta_vyos
vyos
watchguard_fireware
Copy the code