Execute SSH commands with Python

By Charles LAZIOSI
Published on
your dApp on the lightning network
  1. Procedure
  2. Full Source Code
  3. With a SSH Key instead of a username/password

Procedure

To execute a command over SSH in Python, you can use the paramiko library. Here's an example of how to do it:

  1. Install paramiko library if you haven't already:
pip install paramiko
  1. Import the necessary modules:
import paramiko
  1. Create an SSH client and connect to the remote server:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', port=22, username='username', password='password')

Replace 'hostname', 'username', and 'password' with the appropriate values for your remote server.

  1. Execute the desired command on the remote server:
stdin, stdout, stderr = client.exec_command('command')

Replace 'command' with the command you want to execute on the remote server.

  1. Read and process the output of the command:
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')

print('Output:', output)
print('Error:', error)
  1. Close the SSH connection:
client.close()

That's it! You have now executed a command over SSH in Python using paramiko.

Full Source Code

Here's the full source code in Python to execute a bash script over SSH using paramiko:

import paramiko

# Create an SSH client and connect to the remote server
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', port=22, username='username', password='password')

# Execute the bash script on the remote server
stdin, stdout, stderr = client.exec_command('bash script.sh')

# Read and process the output of the command
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')

print('Output:', output)
print('Error:', error)

# Close the SSH connection
client.close()

Replace 'hostname', 'username', and 'password' with the appropriate values for your remote server. And make sure to replace 'script.sh' with the actual path of your bash script file.

With a SSH Key instead of a username/password

If you have an SSH public key, you can use it to authenticate the SSH connection instead of providing a password. Here's the modified source code to execute a command over SSH using an SSH public key:

import paramiko

# Create an SSH client and connect to the remote server
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', port=22, username='username', key_filename='/path/to/public/key')

# Execute the desired command on the remote server
stdin, stdout, stderr = client.exec_command('command')

# Read and process the output of the command
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')

print('Output:', output)
print('Error:', error)

# Close the SSH connection
client.close()

Replace 'hostname', 'username', and '/path/to/public/key' with the appropriate values for your remote server. And replace 'command' with the command you want to execute on the remote server.

Make sure to have your SSH public key file (id_rsa.pub or similar) available and provide its correct path in key_filename='/path/to/public/key'.