Netmiko is a multivendor library that simplifies the
process of creating ssh connections to different
network devices.
Netmiko
The library is based on the Paramiko SSH library and
is named Netmiko.
The purposes of this library are the following:
Successfully establish an SSH connection to the device
Simplify the execution of show commands and the retrieval of
output data
Simplify execution of configuration commands including possibly
commit actions
Do the above across a broad set of networking vendors and
platforms
Many individuals encountered similar
issues with Python-SSH and network
devices.
For example, HP ProCurve switches
have ANSI escape codes in the output or
the Cisco WLC has an extra 'login as:'
message.
These types of issues can soak up
a lot of development and
troubleshooting time and, what is
worse, people keep solving the
same issues over and over again.
So Netmiko was created to simplify
this lower-level SSH management
across a wide set of networking
vendors and platforms.
Platform Support
Testing
Regularly Limited
Experimental
tested testing
Regularly
tested
Arista EOS
Cisco ASA
Cisco IOS/IOS-XE
Cisco IOS-XR
Cisco NX-OS
Cisco SG300
HP Comware7
HP ProCurve
Juniper Junos
Linux
Limited
testing
Alcatel AOS6/AOS8
Apresia Systems AEOS
Calix B6
Cisco AireOS (Wireless
LAN Controllers)
Dell OS10
Dell PowerConnect
Extreme MLX/NetIron
(Brocade/Foundry)
Huawei
and many more..
Experimental
A10
Accedian
Aruba
Ciena SAOS
Citrix Netscaler
Cisco Telepresence
Check Point GAiA
Coriant
Dell OS6
Dell EMC Isilon
Nokia/Alcatel SR-OS
QuantaMesh
Example 1: Simple SSH session to a Cisco router;
execute and return the 'show ip int brief'
command.
First, we must import the ConnectHandler factory function from
Netmiko.
This factory function selects the correct Netmiko class based upon the
device_type.
Then define a network device dictionary consisting of a device_type,
ip, username, and password.
In [1]: from netmiko import ConnectHandler
In [2]: cisco = { ...: 'device_type': 'cisco_ios',
...: 'host': 'cisco.domain.com',
...: 'username': 'admin',
...: 'password': 'cisco123',
...: }
At this point, we should be able to connect to the device.
Notice above that we have specified the device_type as 'cisco_ios'.
The supported device_types can generally be found here.
Now in order to connect all we need to do is call ConnectHandler and
pass in my earlier defined device dictionary:
In [3]: net_connect = ConnectHandler(**cisco)
Alternatively, we could just call the ConnectHandler function directly
and not use a dictionary (as follows):
net_connect2 = ConnectHandler(device_type='cisco_ios',
host='cisco.domain.com', username='admin',
password='cisco123')
Now at this point we should have an established SSH connection. We
can verify this by executing the find_prompt() method
In [5]: net_connect.find_prompt()
Out[5]: 'cisco3#'
We can also send commands down the SSH channel and receive the
output back. Here, we use the .send_command() method to send the
'show ip int brief' command:
In [6]: output = net_connect.send_command("show ip int brief")
Let's also try to make a configuration change to this router. First, let's look
at the current logging configuration:
In [8]: output = net_connect.send_command("show run | inc logging")
In [9]: print(output)
logging synchronous
Now in order to make configuration changes, we create a list of
configuration commands that we want to execute. This could be a
single command or multiple commands.
In [10]: config_commands = ['logging buffered 19999']
Then execute the send_config_set() method. This method will enter
configuration mode, execute the commands, and then exit configuration
mode (note, there will be some exceptions to this behavior depending on
the platform--for example, IOS-XR will not exit configuration mode due to
pending changes).
In [11]: output = net_connect.send_config_set(config_commands)
In [12]: print(output)
config term
Enter configuration commands, one per line. End with CNTL/Z.
cisco3(config)#logging buffered 19999
cisco3(config)#end
cisco3#
Simple Connection using a Dictionary
from netmiko import Netmiko
from getpass import getpass
cisco1 = {
"host": "cisco1.twb-tech.com",
"username": "pyclass",
"password": getpass(),
"device_type": "cisco_ios",
}
net_connect = Netmiko(**cisco1)
print(net_connect.find_prompt())
net_connect.disconnect()
Basic Threads Example
from datetime import datetime
from netmiko import ConnectHandler
from my_devices import device_list as devices
def show_version(a_device):
"""Execute show version command using Netmiko."""
remote_conn = ConnectHandler(**a_device)
print()
print("#" * 80)
print(remote_conn.send_command_expect("show version"))
print("#" * 80)
print()
def main():
"""
Use threads and Netmiko to connect to each of the devices. Execute
'show version' on each device. Record the amount of time required to do
this.
"""
start_time = datetime.now()
for a_device in devices:
my_thread = threading.Thread(target=show_version,
args=(a_device,))
my_thread.start()
main_thread = threading.currentThread()
for some_thread in threading.enumerate():
if some_thread != main_thread:
print(some_thread)
some_thread.join()
print("\nElapsed time: " + str(datetime.now() - start_time))
if __name__ == "__main__":
main()