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

This post is the fifth in a series exploring Python libraries that are commonly used for interacting with network devices. The previous entries in this series covered libraries for directly interacting with network devices. Libraries such as NetmikoNAPALMScrapli, and Nornir greatly ease the process of connecting to, and configuring, a network device. This blog will examine Netutils, a Python library that is a collection of objects for common network automation tasks.

What Is Netutils?

Netutils is a library created by Network to Code that has a series of functions that are aggregated in a single location. The intention is to find things that network engineers commonly have to do in their job and provide them in one place. The high-level buckets include:

  • Bandwidth
  • Banner
  • BGP ASN
  • Configuration
  • DNS
  • Interface
  • IP Address
  • Library Mapper
  • MAC Address
  • Password
  • Ping
  • Protocol Mapper
  • Route
  • Time
  • VLANs

While initially explored here when introduced, this article will deep dive into parts not already covered.

Installation

You can install Netutils via pip:

pip install netutils

Or, if you are using Poetry for dependency management:

poetry add netutils

Bandwidth

This is a new addition since Netutils was introduced. The functions provide the ability to swap between names to bits/bytes floats. This is especially helpful when dealing with systems that are better suited for human reading (such as interface stats) versus machine read comparison.

>>> from netutils.bandwidth import name_to_bits
>>> name_to_bits("10Gbps")
10000000000
>>> name_to_bits("33.6Kbps")
33600
>>> name_to_bits("2.5Gbps")
2500000000
>>> 
>>> 
>>> from netutils.bandwidth import name_to_bytes
>>> name_to_bytes("10Gbps")
1250000000.0
>>> name_to_bytes("100Mbps")
12500000.0
>>> name_to_bytes("100GBps")
100000000000.0
>>> 
>>> 
>>> from netutils.bandwidth import bits_to_name
>>> bits_to_name(125000)
'125Kbps'
>>> bits_to_name(1000000000)
'1Gbps'
>>> 
>>> 
>>> from netutils.bandwidth import bytes_to_name
>>> bytes_to_name(10000.0)
'10.0KBps'
>>> bytes_to_name(10000000.0)
'10.0MBps'

Protocol Mapper

When initially released, Netutils had the ability to map IP protocols, but it has since been extended to map TCP, UDP, DCCP, and SCTP. This is rather helpful when dealing with firewall rules, when you may be given number and need name or vice versa.

>>> from netutils.protocol_mapper import TCP_NAME_TO_NUM, TCP_NUM_TO_NAME, UDP_NAME_TO_NUM, UDP_NUM_TO_NAME
>>> TCP_NUM_TO_NAME[22]
'SSH'
>>> TCP_NAME_TO_NUM['SSH']
22
>>> UDP_NUM_TO_NAME[49]
'TACACS'
>>> UDP_NAME_TO_NUM["TACACS"]
49
>>>

BGP ASN

BGP configurations sometimes use the integer value, which is the same with 2-byte ASNs, but different when using 4-byte ASNs. This can show up in Cisco configurations in both manners, depending on the bgp asnotation configuration setting.

>>> from netutils.asn import asn_to_int
>>> asn_to_int("65000")
65000
>>> asn_to_int("65000.111")
4259840111
>>>

Ping

A pure Python ping can be helpful. One strategy used to eliminate connectivity check issues with high-level libraries like Netmiko is to test that there is basic connectivity beforehand.

>>> from netutils.ping import tcp_ping
>>> tcp_ping("1.1.1.1", 443)
True
>>> tcp_ping("1.0.100.0", 27)
False
>>>

MAC Address

MAC addresses can be stored in a variety of ways, but the configuration expected is generally uniform. These functions help verify that the MAC addresses are valid and transpose them.

>>> from netutils.mac import mac_to_format
>>> mac_to_format("aa.bb.cc.dd.ee.ff", "MAC_DASH_FOUR")
'aabb-ccdd-eeff'
>>>
>>> mac_to_format("aa.bb.cc.dd.ee.ff", "MAC_DOT_FOUR")
'aabb.ccdd.eeff'
>>> mac_to_format("aa.bb.cc.dd.ee.ff", "MAC_DOT_TWO")
'aa.bb.cc.dd.ee.ff'
>>>

IP Address

There is a whole series of options for manipulating and gathering information about IP addresses.

Adding and subtracting IP addresses can be used when you want to “plan” your IP address configuration. Such as, always use the first IP address as the HSRP, the second on the primary device’s interface, and the third on the backup device’s interface.

>>> from netutils.ip import ip_addition
>>> ip_addition("10.100.100.100", 200)
'10.100.101.44'
>>>
>>> from netutils.ip import ip_subtract
>>> ip_subtract("10.100.100.100", 200)
'10.100.99.156'
>>>

Many systems store in one format (between cidr/netmask), but the configuration systems require differences. For example, Cisco IOS uses netmask, but Arista uses cidr.

>>> from netutils.ip import netmask_to_cidr
>>> netmask_to_cidr("255.255.255.0")
24
>>> netmask_to_cidr("255.255.254.0")
23
>>>
>>> from netutils.ip import cidr_to_netmask
>>> cidr_to_netmask(24)
'255.255.255.0'
>>> cidr_to_netmask(17)
'255.255.128.0'

Getting a peer IP address can be a bit more complicated, as you need to consider /30 & /31’s and understand which would give you the opposite sides IP address. It is helpful to have such a solution centralized and well tested.

>>> from netutils.ip import get_peer_ip
>>> get_peer_ip('10.0.0.1/255.255.255.252')
'10.0.0.2'
>>> get_peer_ip('10.0.0.2/30')
'10.0.0.1'
>>> get_peer_ip('10.0.0.1/255.255.255.254')
'10.0.0.0'
>>> get_peer_ip('10.0.0.0/31')
'10.0.0.1'
>>>

Providing an interface range is helpful to communicate with those less familiar with IPs and networking in general.

>>> from netutils.ip import get_usable_range
>>> get_usable_range("10.100.100.0/29")
'10.100.100.1 - 10.100.100.6'
>>>

Time

Many Network Operating Systems (NOSs) use human-readable time formats, which are not great for automation. The functions provided help unify them to integers.

>>> from netutils.time import uptime_seconds_to_string
>>> uptime_seconds_to_string(7250)
'2 hours, 50 seconds'
>>> from netutils.time import uptime_string_to_seconds
>>> uptime_string_to_seconds("58 minutes")
3480
>>> from netutils.time import uptime_string_to_seconds
>>> uptime_string_to_seconds("4m15s")
255

Other Updates

There have been many updates since the initial release:

  • Bandwidth conversion added
  • Protocol mapper updates for tcp/tdp/dccp/sccp
  • Parsers added for
    • Cisco ASA
    • Fortinet FortiOS
    • Nokia SR OS
  • Interface Range
  • Interface Sorting
  • Added a Jinja2 convenience function
  • Added Uptime Conversion

What’s Next?

The Netutils repo is alive and well; and I, for one, believe it hasn’t reached its full potential. There are many ideas that you can track in issues, but I wanted to call out a few ideas that I would love to get completed.


Conclusion

Netutils is simple in nature, generally only functions, which makes it great for newer developers to contribute. Its mission is simple and light, but it solves real-world problems. Any help from the community is welcome, and don’t forget to meet us at the #networktocode channel on Slack.

-Ken

New to Python libraries? NTC’s Training Academy is holding a three-day course Automating Networks with Python I September 26-28, 2022, half of which is labs so you can get 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!

Author