How to connect SSH and run command in Python (using paramiko)
0
2551
To connect to remote system via SSH you need to use python module paramiko . Paramiko is a Python (2.7, 3.4+) implementation of the SSHv2 protocol.
Install paramiko as follows
$ pip install paramiko
The following code will connect SSH and run command date
import paramiko
ip = '10.176.225.132' # IP or hostname
port = 22 # SSH port on remote system
username = 'root' # Username
password = 'Pass1234' # Password
cmd = 'date' # Command to run on remote system
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
stdin, stdout, stderr = ssh.exec_command(cmd)
outlines = stdout.readlines()
resp = ''.join(outlines)
print(resp)
ssh.close()
Comment here