Loading local SSH configuration
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.
Getting ready
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>
How to do it...
Let's start by importing the required libraries and defining the path to our SSH configuration:
- Import the Paramiko library:
from paramiko.client import SSHClient from paramiko import SSHConfig
- Specify the path to your SSH config file and the name of your host as it appears in your SSH configuration (example in this snippet). We will populate all the other variables from the configuration we are reading:
SSH_CONFIG = "<insert path to ssh config here>" SSH_HOST = "example"
- Create an
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)
- Next, we need to tell the
SSHConfig
object to load and parse the configuration file:config.parse(config_file)
- With the config parsed, we can now do a lookup on this configuration object to extract all information stored in the configuration itself. The lookup function will return a dictionary:
dev_config = config.lookup(SSH_HOST)
- With our device configuration extracted from the SSH config we can go ahead and fill our connection details with what we have extracted from the SSH configuration file:
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'])
- With the connection established, we can do all the different things we discovered in previous recipes before finally closing the connection:
client.close()
- To run this script, go to your terminal and execute it with this:
python3 parse_config.py
How it works...
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.