Exercise 2 – generating a routing table for your device – The Simplest Ways to Start Using DevOps in Python Immediately

Exercise 2 – generating a routing table for your device

Routing tables define the routes that certain web traffic takes within your devices. These tables exist in practically every device, and they define the routes by which those devices access computer networks. You can use the netifaces Python library to generate a routing table showing all the available routes and destinations that your device contains. The netifaces library in this case is used to collect the network interfaces (hence the name netifaces) of your operating system. You will then parse this information and display it in a tabular form. You can once again use Google Colab for this, though for more interesting results, you could try running the code locally.

  1. Let’s begin the steps to generate a routing table for your device. If you’ve been following along so far, you know the first step is installing the library:

!pip install netifaces

2. Next, write code to generate the routing table:

#import library
import netifaces
#begin function
def generate_routing_table():
routing_table = []
#Loop through network interfaces
for interface in netifaces.interfaces():
     #initialize current address of interface
Interface_addresses =netifaces.ifaddresses(interface)
#Check for, then loop through the addresses
if netifaces.AF_INET in addresses:
for entry in        interface_addresses[netifaces.AF_INET]:
              #Create routing entry wherefound
if ‘netmask’ in entry and ‘addr’ in            entry:
routing_entry = {
‘interface’: interface,
‘destination’: entry[‘addr’],
‘netmask’: entry[‘netmask’]
}
#Append route to routing table
routing_table.append(routing_entry)
return routing_table
#Call function
routing_table = generate_routing_table()
#Display routing table
for entry in routing_table:
print(f”Interface: {entry[‘interface’]}”)
print(f”Destination: {entry[‘destination’]}”)
print(f”Netmask: {entry[‘netmask’]}”)
print(“-” * 30)

It’s a lot of code, but fairly easy to make sense of. It also provides you with detailed information about where the network traffic goes from your network interfaces. If you tried it on Colab as I suggested, you’d get something like this:

Figure 3.17 – Route table on Colab

And if you’ve done it on your personal computer, you might get something like this:

Figure 3.18 – Route table on a personal computer

A bit more baggage was added there.

But that is the gist of it, and these are just a couple of ways you can use Python to facilitate the networking aspect of DevOps.

Summary

In this chapter, you learned a thing or two about the hands-on part of this book. You’ve learned about APIs and computer networks, which practically means you’re halfway there as far as the Python DevOps implementation goes.

In this chapter, not only did you learn about these important DevOps concepts, but you also learned how you can implement them in your DevOps process. You can take this code and implement it directly in your DevOps workload right now if that benefits you.

These fundamentals that you have learned will help you enhance, monitor, and diagnose problems on practically any DevOps workload that you may encounter. In the next chapter, we will discuss the creation of resources in a DevOps workload and how and where Python can be of assistance in the process.

Leave a Reply

Your email address will not be published. Required fields are marked *