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:
- Import the required libraries, Paramiko and
json
:import json from paramiko.client import SSHClient
- 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 } ]
- 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)
- 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'])
- 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.