pynet.twb-tech.com Open in urlscan Pro
3.12.0.219  Public Scan

URL: https://pynet.twb-tech.com/blog/netmiko-python-library.html
Submission: On May 23 via api from US — Scanned from DE

Form analysis 1 forms found in the DOM

POST /join_emaillist.html

<form method="POST" action="/join_emaillist.html" class="newsletter-form dlfl"> <input type="hidden" name="csrfmiddlewaretoken" value="sJmFmafH38bz4RI6SNfw624QzB7byVHDCChzx5Sy8qlQSs9CH7kT3Uopid7rP4og">
  <input type="email" name="email" placeholder="Enter your email">
  <input type="submit" value="Join Course">
</form>

Text Content

 * Home
 * Our Courses
 * Articles
 * Free Course
   
   

Free Course


NETMIKO LIBRARY

Author: Kirk Byers
Date: Sept 30, 2021

Since late 2014, I have been working on an open-source Python library that
simplifies SSH management to network devices. The library is based on the
Paramiko SSH library and is named Netmiko.

You can find the library at https://github.com/ktbyers/netmiko and the latest
released version of the software can be installed using pip.




LIBRARY PURPOSE

The purposes of this library are the following:

 * Successfully establish an SSH connection to the device.
 * Simplify the execution, retrieval, and formatting of show commands.
 * Simplify the execution of configuration commands.
 * Abstract away much of the low-level mechanics of interacting with devices.
 * Provide a (relatively) uniform API for interacting with devices.
 * Do the above across a broad set of networking vendors and platforms.


SUPPORTED PLATFORMS

Netmiko currently supports about eighty different platforms.

For a mostly complete list of supported platforms see PLATFORMS.md.

In addition to SSH support, Netmiko also supports Secure Copy, telnet
connections, and serial connections. The platform support for each of these is
more limited than SSH. Once again see the PLATFORMS.md file for more details on
the platforms supported for these use cases.


GETTING STARTED

Import "ConnectHandler" from the Netmiko library. You can think of
ConnectHandler as your main entry point into the library. It picks the right
class for you, creates a Netmiko object based on that class, and establishes an
SSH connection to the remote device.

from netmiko import ConnectHandler

You would then use ConnectHandler to pick the class and establish the SSH
connection.

This requires us to pass in certain arguments namely our:

 * device_type
 * host (hostname or IP)
 * username
 * password

Netmiko's available device types are once again listed in the PLATFORMS.md file.

net_connect = ConnectHandler(
    device_type="cisco_xe",
    host="cisco5.domain.com",
    username="admin",
    password="password",
)

At this point the variable 'net_connect' should be a usable SSH connection to
the remote device.

>>> net_connect
<netmiko.cisco.cisco_ios.CiscoIosSSH object at 0x7f3403b16fd0>
>>> net_connect.find_prompt()
'cisco5#'


EXECUTING SHOW COMMANDS

One of the next things that you probably want to do is retrieve show command
output from your device. This can be accomplished by using the 'send_command()'
method.

>>> output = net_connect.send_command("show ip arp")
>>> print(output)
Protocol  Address    Age (min)  Hardware Addr   Type   Interface
Internet  10.0.2.2         57   5255.0a00.0202  ARPA   GigabitEthernet1
Internet  10.0.2.3        116   5255.0a00.0203  ARPA   GigabitEthernet1
Internet  10.0.2.15         -   5254.0012.3456  ARPA   GigabitEthernet1

Notice that Netmiko automatically processes the output and strips the command
echo. Additionally, it strips the trailing router prompt. In other words, it
attempts to leave you with only the show command output and nothing else.

Also you should not have to disable output paging. Netmiko should do this
automatically when it connects to the device.

Our complete script at this point would be the following:

from netmiko import ConnectHandler

net_connect = ConnectHandler(
    device_type="cisco_xe",
    host="cisco5.domain.com",
    username="admin",
    password="password",
)

output = net_connect.send_command(
    "show ip arp"
)
print(output) 

And execution of this script would print the following:

$ python show_command.py 
Protocol  Address   Age (min)  Hardware Addr   Type   Interface
Internet  10.0.2.2        64   5255.0a00.0202  ARPA   GigabitEthernet1
Internet  10.0.2.3       123   5255.0a00.0203  ARPA   GigabitEthernet1
Internet  10.0.2.15        -   5254.0012.3456  ARPA   GigabitEthernet1


MAKING CONFIGURATION CHANGES

Before too long, you will probably want to make some configuration changes using
Netmiko. You can accomplish this using Netmiko's send_config_set() method.

cfg_list = [
    "ip access-list extended TEST1",
    "permit ip any host 1.1.1.1",
    "permit ip any host 1.1.1.2",
    "permit ip any host 1.1.1.3",
    "permit ip any host 1.1.1.4",
    "permit ip any host 1.1.1.5",
]
cfg_output = net_connect.send_config_set(cfg_list)

print(cfg_output)

Here I create a list of configuration commands and then provide that list to the
'send_config_set()' method.

The 'cfg_output' variable will show me what occurred during that SSH session.
Once again, Netmiko will do a set of things automatically—in particular, it will
both enter and exit configuration mode (if necessary).

You can then save your running-config to startup-config by executing the
'save_config()' method.

net_connect.save_config()


ADDITIONAL ARTICLES

Of course, there is a lot more that you can do using Netmiko. I have written
articles about a set of these features across time.

 * Netmiko and TextFSM
 * Netmiko and SSH Proxies
 * Expanding Netmiko's Secure Copy Support

I have also written the following article describing some of the characteristics
of how Netmiko deteremines when a command is done. Note, this article mostly
pertains to Netmiko 3.X and does not really apply to Netmiko 4.X or later.

 * Netmiko and What Constitutes Done

Finally, I have a set of articles detailing new features in Netmiko 4.X. Netmiko
4.X should come out in the fall of 2021.

 * Netmiko4 Feature: send_multiline_timing()
 * Netmiko4 Feature: send_multiline() for handling additional prompts


NETMIKO'S COMMAND-LINE TOOLS

Netmiko has a set of command-line tools. For more details on these tools see the
following articles.

 * netmiko-grep
 * Expanding netmiko-tools


ADDITIONAL EXAMPLES

There are lots of additional examples here.


ADDITIONAL DOCUMENTATION

 * API Documentation
 * Main GitHub Page
 * Netmiko Releases

Kirk Byers

@kirkbyers


YOU MIGHT BE INTERESTED IN

   

 * Netmiko
   
   Netmiko4 Feature: send_multiline() for handling additional prompts
   
   In an earlier article I introduced the send_multiline_timing feature. In this
   article let's talk about a closely related feature, send_multiline().
   
   Read More

 * Netmiko
   
   Netmiko4 Feature: send_multiline_timing()
   
   I have been working for quite a while developing new features in Netmiko4.
   Over time, I will be introducing these features to you.
   
   Read More

 * Netmiko
   
   Netmiko SSH Proxy Support
   
   Netmiko supports SSH proxies. By this I mean you can 'bounce' through an
   intermediate server while connecting to a remote network device.
   
   Read More


Join the free Learning Python Course.
Starts August 8th

Unsubscribe at any time.


support@twb-tech.com


ABOUT US




OUR COURSES

Python Network Automation Ansible Network Automation Netmiko by Example Nornir
Automation


OTHER LINKS

Articles & Videos

support@twb-tech.com

Copyright © 2022 Twin Bridges Technology