-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Python Network Programming Techniques
By :
So far, we have always used a username/password combination to connect to our device. This is not the most secure way, however, and many security policies advocate using public-private key pairs instead of a static password.
In this recipe, you will see how to programmatically open an SSH connection using a password-protected private key.
Open your code editor and start by creating a file called key_file.py. Next, navigate your terminal to the same directory that you just created the key_file.py file in.
You'll also need a password-protected private key for the device/server you are trying to connect to and have the device/server configured to allow or require key-based logins.
Let's start by importing the required libraries, define our new connection details, and finally open up a connection using key-based authentication:
from paramiko.client import SSHClient
SSH_USER = "<Insert your ssh user here>" SSH_HOST = "<Insert the IP/host of your device/server here>" SSH_PORT = 22 # Change this if your SSH port is different SSH_KEY = "<Insert the name of your private key here>" SSH_KEY_PASSWORD = "<Insert the password here>"
SSHClient object, which we just imported from Paramiko: client = SSHClient()
connect method of the client object to do so. Before actually connecting, we will still need to make sure that our client knows the host keys: client.load_system_host_keys() client.connect(SSH_HOST, port=SSH_PORT, username=SSH_USER, look_for_keys=True, key_filename=SSH_KEY, passphrase=SSH_KEY_PASSWORD)
stdin, stdout, stderr = client.exec_command('<your command>')client.close()
python3 key_file.py
In this example, we use Paramiko's ability to load RSA keys for authentication to avoid using a username/password combination for authentication.
We need to import the same packages we have used before. The difference lies in the parameters that we pass to the connect function. Instead of specifying a password, we specify the name of our ssh key. The library will then, as indicated by setting the look_for_keys flag to true, search common places such as ~/.ssh for keys and match them with the name provided. The passphrase argument is used to provide the passphrase used to decode the private key. If your private key does not have a passphrase, you can omit this argument.
Once the connection is established, we can use the client in the same way as we did before, when dealing with username-password authentication.
In the preceding example, we relied on the key file being present in one of the known paths. Sometimes you might want to explicitly specify the path you are loading a key file from. You can do so by, instead of just specifying the filename in the key_filename attribute, specifying the entire path where Paramiko can find your private key.
For example, if your private key is in /home/user/my_keys/id_rsa, you could modify the preceding example like so:
SSH_KEY = "/home/user/my_keys/id_rsa"
If you want to connect to different devices and have multiple keys, one for each device, you can also pass a list of key names or paths to ssh keys to the key_filename attribute:
SSH_KEY = [ "/home/user/my_keys/device_1", "/home/user/my_keys/device_2", "/home/user/my_keys/device_3" ]
Paramiko will then try out all the keys in the provided list for each device you are connecting to.