How to run commands on remote hosts using paramiko

We provide a guide on how to use Paramiko, a Python library for SSH2, to run commands on remote hosts. Learn how to establish a connection, execute commands, and handle outputs.


  • Ensure Python is installed on your local machine.
  • The Paramiko library is installed on your machine (you can install it using the pip command: pip install paramiko).
  • Access to a remote server with SSH enabled.
See also  How to automate file transfers with paramiko and SFTP

Establishing a Connection

import paramiko

ssh = paramiko.SSHClient()
ssh.connect(hostname='hostname', username='username', password='password')

Running Commands

Once connected, you can execute commands on the remote host:

stdin, stdout, stderr = ssh.exec_command('your_command_here')

Replace 'your_command_here' with the actual command you want to run on the remote server.

Closing the Connection

It’s important to close the connection to the remote host after you have finished executing your commands: