Getting Started with Python Network Libraries for Network Engineers – Part 1

Blog Detail

This blog post will be the first in a series covering common Python libraries that can be used to interact with network devices. In this post we will cover the Netmiko Python library by Kirk Byers. Netmiko is based on the Paramiko Python library, but whereas Paramiko was designed to interact with standard OpenSSH devices (like Linux), Netmiko was designed to interact with network devices. It has a large number of supported platforms included for connecting via SSH, and it can also accommodate limited Telnet or serial connections as well as Secure Copy (SCP) for file transfers.

Installation

You can install Netmiko via pip install netmiko. Or, if you are using Poetry, you can use poetry add netmiko.

Getting Connected

Note: We will only be covering connecting via SSH in this blog post.

Like all SSH connections, Netmiko requires a hostname (IP or DNS name) and an authentication method (generally username and password) to get connected. In addition to this, you will also need to specify the device type you will be connecting to.

>>> from netmiko import ConnectHandler
>>> 
>>> conn = ConnectHandler(
...     host="192.0.2.3",
...     username="cisco",
...     password="cisco",
...     device_type="cisco_ios"
... )

There are two ways of determining the device type: looking it up in a list or having Netmiko try to detect the device type automatically. You can see the list of current device types by digging into the code on GitHub, specifically the CLASS_MAPPER_BASE dictionary in the ssh_dispatcher.py file. If, however, you aren’t exactly sure which device type you need to choose, you can use the SSHDetect class to have Netmiko help:

>>> from netmiko import ConnectHandler, SSHDetect
>>> 
>>> detect = SSHDetect(
...     host="192.0.2.3",
...     username="cisco",
...     password="cisco",
...     device_type="autodetect"  # Note specifically passing 'autodetect' here is required
... )
>>> detect.autodetect()  # This method returns the most likely device type
'cisco_ios'
>>> detect.potential_matches  # You can also see all the potential device types and their corresponding accuracy rating
{'cisco_ios': 99}
>>> conn = ConnectHandler(
...     host="192.0.2.3",
...     username="cisco",
...     password="cisco",
...     device_type=detect.autodetect()
... )

Common Methods

Once you have instantiated your ConnectHandler object, you can send single show commands via the .send_command() method. You will use the same command syntax you would type in if you were directly connected to the device via SSH:

>>> output = conn.send_command("show ip int br")
>>> print(output)
Interface              IP-Address      OK? Method Status                Protocol
FastEthernet0          unassigned      YES NVRAM  down                  down
GigabitEthernet1/0/1   unassigned      YES unset  up                    up
GigabitEthernet1/0/2   unassigned      YES unset  up                    up
GigabitEthernet1/0/3   unassigned      YES unset  up                    up
...

Note: You can send multiple show commands back-to-back with the .send_multiline(["command1", "command2"]).

If you want to run a command to edit the configuration, you would use the .send_config_set() method instead. This method takes care of entering and exiting configuration mode for you, and it requires the commands be in a list or set:

>>> output = conn.send_config_set(("interface Gi1/0/3", "no description"))
>>> print(output)
configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
cisco(config)#interface Gi1/0/3
cisco(config-if)#no description
cisco(config-if)#end
cisco#

And since we are good network engineers, we know we should always save our configuration after making changes with the .save_config() method:

>>> output = conn.save_config()
>>> print(output)
write mem
Building configuration...
[OK]
cisco#

Command Output Parsing

We’ve covered parsing strategies here on this blog before, including the three currently supported parsers Netmiko supports which are TextFSMTTP, and Genie. Let’s take a quick look on how to use them with Netmiko.

TextFSM

If you are just starting out, the easiest way to get structured output data would be to use the included TextFSM parser. By default, Netmiko includes the TextFSM library for the parsing as well as NTC Templates to use as the default templates. To get structured output, simply add use_textfsm=True to the parameters of the .send_command() method:

>>> output = conn.send_command("show interfaces", use_textfsm=True)
>>> pprint(output)
[{'abort': '',
  'address': '381c.1ae6.cd81',
  'bandwidth': '100000 Kbit',
  'bia': '381c.1ae6.cd81',
  'crc': '0',
  'delay': '100 usec',
...

You can see the template used for this command here.

TTP

Netmiko also supports the TTP parsing library, but you will need to install it via pip install ttp or poetry add ttp first. It also does not currently include any templates, so you will need to find or create your own and then provide the path to those templates when you send your command.

Creating TTP templates yourself is definitely more of a manual process, but it gives you the freedom to pare down to only the information that you need. For example, if you just need the interface name, status, and description you can have a template like so:

{{ interface }} is {{ link_status }}, line protocol is {{ protocol_status }} {{ ignore }}
  Description: {{ description }}

And then you would reference the template path using ttp_template:

>>> output = conn.send_command("show interfaces", use_ttp=True, ttp_template="templates/show_interfaces.ttp")
>>> pprint(output)
[[[{'description': 'CAM1',
    'interface': 'GigabitEthernet1/0/1',
    'link_status': 'up',
    'protocol_status': 'up'},
   {'description': 'CAM2',
    'interface': 'GigabitEthernet1/0/2',
    'link_status': 'up',
    'protocol_status': 'up'},
...

Genie

The last parser that Netmiko currently supports is Genie. Netmiko, however, does not install Genie nor its required library, PyATS, by default, so you will need to install them separately via pip install 'pyats[library]' or poetry add 'pyats[library]'. Once they are installed, it is again very similar to enable the Genie parsing:

>>> output = conn.send_command("show interfaces", use_genie=True)
>>> pprint(output)
{'GigabitEthernet1/0/1': {'arp_timeout': '04:00:00',
                          'arp_type': 'arpa',
                          'bandwidth': 100000,
                          'connected': True,
                          'counters': {'in_broadcast_pkts': 41240,
...

Note: Genie does not support custom templates.


Conclusion

As you can see, it doesn’t take much to get started with Netmiko. If you’d like to learn more advanced interactions with Netmiko, such as transferring files via SCP, connecting via SSH keys, or even handling commands that prompt for additional input, the best place to start would be the Netmiko Examples page in the GitHub repository. Another great resource is the #netmiko channel in the Network to Code Slack.

-Joe

New to Python libraries? NTC’s Training Academy is holding a 3-day course Automating Networks with Python I on September 26-28, 2022 with 50% labs to get you up to speed.
Visit our 2022 public course schedule to see our full list.



ntc img
ntc img

Contact Us to Learn More

Share details about yourself & someone from our team will reach out to you ASAP!

Getting Started with VS Code

Blog Detail

Visual Studio Code (VS Code) is a free cross-platform code editor that has risen in popularity in recent years. There is a large community surrounding VS Code and contributions to it continue to increase. In this blog post, I will showcase some of the tips, tricks, settings, and extensions that can enhance VS Code. These take VS Code beyond being just a simple code editor to being a very powerful automation tool. Whether you have never heard of VS Code or if you already use it on a regular basis, I am hoping you will take something away from this post to help you become a more efficient developer. All of the extensions that I highlight are free and all of the settings can be reverted, so there is no harm in giving them a try.

First Things First

Before we start modifying settings or installing extensions, we want to enable Settings Sync so that all of our changes are automatically backed up to the cloud. This also makes it easier to have the same experience across multiple development machines. The easiest way is to click on the silhouette icon in the bottom left and choose Turn on Settings Sync…. You will need a new or existing GitHub or Microsoft account to enable this option.

Auto Formatters

Many projects these days have formatting requirements for contributing—and rightly so. When developers are first starting out, they can get frustrated when they open that first pull request only for the CI/CD pipeline to flag their code because it doesn’t meet the formatting guidelines. This situation can lead to numerous small commits to try to squash every warning or error that pops up. Keep your sanity intact by using these auto formatting tools to have VS Code format your code every time you save your file!

Go to Settings > Text Editor > Formatting and enable Format On Save. Each time you save your file it will attempt to run the code formatter for the language you are writing in. You can go one step further and have your files save automatically when you change focus or window. To enable this, go to Settings > Text Editor > Files and change Auto Save from off to the desired version. VS Code has a few default formatters, but you can install extensions for missing languages or to use a more powerful formatter.

Python Extension

ms-python.python

As soon as you open any Python file in VS Code, it will suggest installing the Python extension. If you do not have the Python extension installed already, you will need to install it in order to enable auto formatting for Python files. Once you have the Python extension installed, it will attempt to format your code with autopep8 by default. If you want to use a different formatter you can select a different one by changing Settings > Extensions > Python > Formatting: Provider.

Prettier Extension

esbenp.prettier-vscode

Prettier is—by their own admission—opinionated, therefore after installing this extension it will enable code formatting for many languages. I only really use Prettier specifically for JSON and YAML files, as I do not really want it mucking with my Markdown files. Luckily, it is easy to disable languages that you don’t want Prettier to change. You can control these via the settings at Settings > Extensions > Prettier > Disable Languages.

Remote Development

Typically when you want to work on code, you would clone down a repository to a local directory, make and commit changes, and push those changes up. The reality is that there are many reasons why you can’t always develop locally, such as:

  • You need to test your code on a different operating system
  • Your company policy does not allow certain applications to be installed locally (e.g., Docker)
  • Your local machine does not have access to a private repository
  • The code you want to test needs access to a private API or lab device

With VS Code’s suite of Remote Development extensions you can overcome most of these challenges. We have already covered how you can use the Remote – Containers Extension previously, but here we will cover two additional Remote Development extensions briefly.

Remote – WSL Extension

ms-vscode-remote.remote-wsl

If you are on a Windows machine that is running the Windows Subsystem for Linux, and just need to change your development environment from Windows to Linux, this extension is for you. After I installed it, the extension was able to quickly find my WSL installation and let me browse to a folder to start working.

Remote – SSH Extension

ms-vscode-remote.remote-ssh

Do you have a remote machine that has all the access and dependencies you need, but all you have is SSH access? This extension is for you. This extension creates an SSH tunnel between your VS Code and your remote environment, so you can have your cake and eat it too. If you already have an SSH config file configured in the default location on your system, this extension will automatically detect that file and use it to present you a list of hosts you can connect to:

ssh-config
ssh-targets

Note: Using this extension requires OpenSSH

Keyboard Shortcuts and Settings

Next I want to detail a few keyboard shortcuts, as well as some settings to change, that will hopefully help you feel more like a power user.

Intelligent Word and Line Operations

I have used a few IDEs and text editors, and these operations are not exclusive to VS Code, but I thought I would list a few things that you can do with VS Code that you may not have been aware of before:

  • You can surround any text with parentheses, brackets, quotes, backticks, etc. by just selecting the text you want to surround, and then typing the opening character (e.g., ( { [ " ')
  • You can indent multiple lines at a time with the Tab key (or outdent with Shift+Tab)
  • You can comment out one or more lines with Cmd+/ (Ctrl+/ on Windows), and it will use the correct comment style based on the file type
  • You can position a cursor at the end of multiple lines at once with Opt+Shift+I (Shift+Alt+I on Windows)
  • You can select all instances of selected characters in a file with Cmd+Shift+L (Ctrl+Shift+L on Windows)

Speaking of selecting words…

Word Separators

Coding languages, unlike typical spoken and written languages, use certain characters as notation, operators, or replacements. For example, you can’t have a space in a variable or function name, so you typically use underscores or camelcase names in place of a space. In networking, we use the period in IPv4 addresses; in Python a period is used to signify a method on a class; and periods are used to separate domain names into Top Level Domains (TLDs) and sub-domains. In order to be efficient at navigating my way around code, I tend to double-click to select names and words. So, I have found it is helpful to change the default word separator list in VS Code to remove the period and the hyphen. You can see the current list of characters and change it with Settings > Text Editor > Word Separators.

Window Title

I don’t know about you, but I tend to keep way too many VS Code windows open with all of the separate projects I’ve worked on recently. When I wanted to switch windows, I either constantly cycled through the open windows with Cmd+` (Alt+Esc on Windows) or I went to the Window menu and looked for the project I wanted to switch to. By default VS Code titles the windows filename - project_name, which causes the list of open windows to reorder themselves constantly as I open different files in each project. Finding the right window was like finding a needle in a haystack sometimes. Thankfully, this issue can be solved as well in the settings. I went to Settings > Window > Title and changed it from ${activeEditorShort}${separator}${rootName} to ${rootName}${separator}${activeEditorShort}. There are plenty of additional variables to choose from if you want to customize the titles even further.

BeforeAfter
Screenshot Showing Default Open File Ordering in VS CodeScreenshot Showing Custom Open File Ordering in VS Code

Enhancement Extensions

Next I want to show you some extensions that augment and enhance coding with certain file types. These can help you find and deal with syntax issues.

Python

I already covered the standard Python extension above, but there are additional extensions that go even further to help you with coding in Python.

Visual Studio IntelliCode

VisualStudioExptTeam.vscodeintellicode

This extension uses machine learning to add context-sensitive suggestions to your code as you type. For example, it will detect that you are typing a variable that was just defined, and suggest it for auto completion when you start typing it again. It can also detect when you use the period to signify a class method, and bring up the methods for that class (as long as it can find the reference). It can also display context-sensitive docstring information as you are adding parameters into a call.

Pylance

ms-python.vscode-pylance

This extension adds a few helpful items like syntax highlighting, type checking, and parameter suggestions, to name a few. It is also compatible with the previously mentioned IntelliCode.

Python Docstring Generator

njpwerner.autodocstring

This handy extension takes your parameters and the return value from your code, and creates a template for you automatically after you type the triple quote to start your docstring. Once the template is created, the first item to edit will be selected and you can start typing. After that, you can use the Tab and Shift+Tab keys to navigate around the remaining sections to edit.

Docker

ms-azuretools.vscode-docker

This is another extension that will be automatically suggested by VS Code to install if you open either a Dockerfile or a docker-compose.yml file. It has many features for interacting with Docker and containers, but I definitely recommend this extension for the autocomplete and context-sensitive help for both file types at the very least.

GitLens

eamodio.gitlens

VS Code already works with git repositories by default, but this extension adds additional visual information. It can help show you the details of when a line was last changed—like git blame in-line or more details via a “hover” popup—as well as let you walk backwards through commits of a file. I found that using it with the Remote – SSH extension above caused the information pop up to lag a bit, but thankfully this extension has integrated many of its options into the VS Code settings interface, so they were easy to disable.

VS Code Visuals

This last section will be dedicated to extensions that help the entity between the keyboard and the chair (hint, that’s you!). Now that you have become an expert at VS Code, you are starting to realize how much everything starts running together, and many things look the same. These extensions hopefully can help you visually find errors in your code before you make them.

Code Spell Checker

streetsidesoftware.code-spell-checker

I can’t live without spell check on text, emails, or even blog posts, so why would I live without it when I code? For anything that is not auto-corrected or auto-completed by an extension you’ve installed, it can be handy to get a visual indication that a word might be misspelled. Words can be easily added to the dictionary via the right-click context menu, as well as removed via the settings. This extension keeps two separate dictionaries for new words—one for your user settings and another just for the current workspace—so you can choose the extent certain words are ignored.

Rainbow Brackets

2gua.rainbow-brackets

This is just a simple extension that color coordinates your parentheses and brackets for a visual indication of which opening bracket matches up with which closing bracket.

Peacock

johnpapa.vscode-peacock

In addition to changing the Window Title as I showed you before, this extension changes the colors on the left and bottom edge of your window to help you quickly identify which window you are looking at. After installing the extension, you can bring up the menu with Cmd+Shift+P (Ctrl+Shift+P on Windows) and searching for “peacock”. From there, you can either select the option to choose a color or just have it randomly pick a color. Once you find a color you like, you can lighten or darken it depending on your visual style, as well as save it to your favorites to use again in the future.


Conclusion

I hope I’ve given you some helpful tools to get you started with using and extending VS Code. I feel like I have barely scratched the surface, and there are definitely a lot more extensions you can explore and install. Is there an extension that you just can’t live without and tell everyone they have to try? If so, let us know in the comments.

-Joe



ntc img
ntc img

Contact Us to Learn More

Share details about yourself & someone from our team will reach out to you ASAP!