- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to perform different commands over ssh with Python?
The simplest way to use SSH using python is to use paramiko. You can install it using −
$ pip install paramiko
To use paramiko, ensure that you have correctly set up SSH keys(https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html) on the host machine and when running the python script, these keys are accessible. Once that is done use the following code to connect to a remote server using ssh −
from paramiko import SSHClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('user@server:path') ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls') print(ssh_stdout) #print the output of ls command
You can use the exec_command function to run any command supported by the server you're connected to over ssh. Running the above code will give you the directory listing on the remote server.
- Related Articles
- How to use Python modules over Paramiko (SSH)?
- Common SSH Commands in Linux With Examples
- How to resume a partially transferred file over ssh on Linux?
- Copying SSH Keys to different Linux Machine
- Transfer Files Between Linux Machines Over SSH
- Replacing and then opening stdinstdout over ssh
- How to perform Calculations with Dictionaries in Python?
- How to perform different simple thresholding of an image using Python OpenCV?
- How to display different list commands in JShell in Java 9?
- Tkinter button commands with lambda in Python
- Why Should We Disable Root-login over SSH on Linux
- How to perform drag and drop operation in Selenium with python?
- How to perform mouse movement to an element in Selenium with python?
- Compute the tensor dot product for arrays with different dimensions over specific axes in Python
- How to perform double click on an element in Selenium with python?