2

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
  • @ŁukaszR. that's awesome! thanks Commented Sep 22, 2015 at 19:28

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.