-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Python Network Programming Techniques
By :
When dealing with multiple different devices and connecting to them via SSH, it can be convenient to specify information such as the hostname, port, username, or identity file to use in a specific configuration file. The OpenSSH implementation stores this file in a file called config in the .ssh directory in your home directory (~/.ssh/config on macOS and Linux).
While we could copy and paste this information into our Python scripts or try to write a parsing function for the format ourselves, it is easier and more convenient to use Paramiko's SSH configuration parser.
In this recipe, you will see how to programmatically parse your SSHConfig file, extract the relevant information based on a host, and store it in a dictionary.
Open your code editor and start by creating a file called parse_config.py. Next, navigate your terminal to the same directory that you just created the parse_config.py file in.
You'll also need an SSH config file for the device you are trying to connect to. In this example, we will be using a config file that has the following content:
Host example Host <insert your host address here> User <insert your user here> Port <insert the port here> IdentityFile <insert the path to your private key here>
Let's start by importing the required libraries and defining the path to our SSH configuration:
from paramiko.client import SSHClient from paramiko import SSHConfig
SSH_CONFIG = "<insert path to ssh config here>" SSH_HOST = "example"
SSHConfig object, which we just imported from Paramiko, and create a local file object with the path to our SSH configuration: config = SSHConfig() config_file = open(SSH_CONFIG)
SSHConfig object to load and parse the configuration file:config.parse(config_file)
dev_config = config.lookup(SSH_HOST)
client.load_system_host_keys() HOST = dev_config['hostname'], client.connect(HOST, port=int(dev_config['port']), username=dev_config['user'], key_filename=dev_config['identityfile'])
client.close()
python3 parse_config.py
In this example, we use Paramiko's ability to parse an SSH configuration file to not define this information in multiple different locations.
We start by importing the SSHConfig class, in addition to the already established SSHClient class. Instead of manually specifying the host, username, and key file information, we now create a local file object that points to our SSH configuration.
With that file opened, we can now have Paramiko parse this configuration. The SSHConfig object now contains all the different information for each of the hosts. We can then do a lookup on our host – in this recipe, the host is called example – and extract all configuration variables that are known in the configuration file.
From that, we proceed to providing that information to the SSHClient. Instead of statically specifying it, we just access it from the dictionary that was returned by Paramiko when doing the lookup on the host.