How do you run linux commands on a remote linux server from a windows host using a python script. I am using Python 2.7.10 and I want to ssh into a remote linux machine and issue some basic commands all from the script which is executed on a windows host.
1 Answer
You can use paramiko. Example:
Install via pip:
pip install paramiko
Code:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("ip address", username="user_name", password="password", port=1234)
ssh.exec_command('your command')
This will allow you to establish an SSH connection between your two machines and execute commands. You can read the output like this:
stdin, stdout, stderr = ssh.exec_command(cmd)
lines = stdout.readlines()
errors = stderr.readlines()
for e in errors:
print 'error', e
for l in lines:
print 'line', l