Book Image

Python Network Programming Techniques

By : Marcel Neidinger
Book Image

Python Network Programming Techniques

By: Marcel Neidinger

Overview of this book

Network automation offers a powerful new way of changing your infrastructure network. Gone are the days of manually logging on to different devices to type the same configuration commands over and over again. With this book, you'll find out how you can automate your network infrastructure using Python. You'll get started on your network automation journey with a hands-on introduction to the network programming basics to complement your infrastructure knowledge. You'll learn how to tackle different aspects of network automation using Python programming and a variety of open source libraries. In the book, you'll learn everything from templating, testing, and deploying your configuration on a device-by-device basis to using high-level REST APIs to manage your cloud-based infrastructure. Finally, you'll see how to automate network security with Cisco’s Firepower APIs. By the end of this Python network programming book, you'll have not only gained a holistic overview of the different methods to automate the configuration and maintenance of network devices, but also learned how to automate simple to complex networking tasks and overcome common network programming challenges.
Table of Contents (14 chapters)

Executing the same command against multiple devices

In the previous recipes, we have always only dealt with a single device. Quite often we have a fleet of similar devices that we want to configure in unison.

In this recipe, you will see how to programmatically open an SSH connection to multiple devices, issue the same command to all of them, and then save the output. We will again use this example to back up the running configuration of multiple devices.

Getting ready

Open your code editor and start by creating a file called exec_multiple.py. Next, navigate your terminal to the same directory that you just created the exec_multiple.py file in. Additionally, create a file called credentials.json. We will use this file to retrieve credentials such as the username and password of our devices.

How to do it...

Let's start by creating our credentials file. We will then read that file from our Python script, create clients for each of these devices, and finally execute a command while also saving the output back to our file:

  1. Import the required libraries, Paramiko and json:
    import json
    from paramiko.client import SSHClient
  2. Open up the credentials.json file and provide the credentials to your device(s) in the format shown in the following code. You can specify as many devices as you want:
    [
     {
       "name": "<insert a unique name of your device>",
       "host": "<insert the host of your device>",
       "username": "<insert the username>",
       "password": "<insert the password",
       "port": 22
     },
     {
       "name": "<insert a unique name of your device>",
       "host": "<insert the host of your device>",
       "username": "<insert the username>",
       "password": "<insert the password",
       "port": 22
     }
    ]
  3. Go back to your exec_multiple.py file. We will now open the JSON file in our Python script:
    credentials = {}
    with open("credentials.json") as fh:
         json.load(fh)
  4. Create a variable holding the command you want to execute. We will then loop over all the devices specified in our credentials.json file and create an SSH client object. Additionally, we will create an individual output file for each of our devices based on the name we specified in the JSON file:
    CMD = "show running-config"
    for cred in credentials:
         out_file_name = str(cred['name']) + ".txt"
         client = SSHClient()
         client.load_system_host_keys()
         client.connect(SSH_HOST, port=cred['port'],
                                  username=cred['username'],
                                  password=cred['password'])
         stdin, stdout, stderr = client.exec_command(CMD)
         out_file = open(out_file_name, "w")
         output = stdout.readlines()
         for line in output:
              out_file.write(line)
         out_file.close()
         client.close()
         print("Executed command on " + cred['name'])
  5. To run this script, go to your terminal and execute it with this:
    python3 exec_multiple.py

How it works...

In this example, we first create a JSON file containing the credentials and connection details for all of our devices. We can view this file as a type of inventory. In general, it is good practice to keep this information separate from the Python code that is acting upon it.

We then use the built-in json module to read the credentials file into a list of dictionaries that we can loop over.

Based on the credentials specified in the credentials file, we then open up a new Paramiko connection, execute the command, and write the output to a new log file, that is, by including the name we set for our device in the JSON file, which is unique for each of the devices.

With this, we can back up the running configuration of an entire fleet of devices from one simple script.