7

Is there a way that I can use Python on Windows to execute shell scripts which are located on a remote Unix machine?

P.S: Sorry about the late edit. I do know of Paramiko, but I wanted to know if there is way of doing it without it. For starters, could it be done with subprocess()?

5
  • possible duplicate of Remote server command execute Commented Sep 23, 2010 at 17:46
  • 1
    Nothing in the standard Python library will allow you to do remote execution on a Unix machine. Either you must use SSH or write your own server (don't). If you'd rather avoid using Paramiko, Twisted offers an alternative SSH implementation. Either way though, you'll need to use a third party package. Commented Sep 23, 2010 at 18:38
  • You can avoid Paramiko. As suggested by Rakis, you could explore ssh support in Twisted. You could add certificates so that you can avoid password challenge and in subprocess, you can run usual command ssh user@servername and then the subsequent commands would be executed on the server. Commented Sep 23, 2010 at 21:56
  • @S. Lott - are you the author of "Building skills in Python" ? Commented Sep 24, 2010 at 9:12
  • @Rakis - Have you tried subprocess package in Python? Commented Oct 29, 2010 at 7:45

7 Answers 7

15

You will need to ssh into the remote machine and if you have appropriate credentials, you can invoke the shell scripts.

For using ssh, you can easily use paramiko module that provides ssh automation

A typical example:

import paramiko
import sys
import os
import os.path
passwd = ""
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('servername', username, password=passwd)
stdin, stdout, stderr = ssh.exec_command('df -h')
x = stdout.readlines()
print x
for line in x:
    print line
ssh.close()

Replace "df -h" command with the your shell script.

Sign up to request clarification or add additional context in comments.

Comments

3

There is not any 'batteries included' module for remote shell execution in python. I'd suggest looking into Fabric , which provides a really nice interface for working through SSH on remote machines, probably a bit nicer than paramiko. You can even install Fabric on windows...

Comments

2

I've got one with multiprocessing and subprocess that i haven't tested but should work based on docs...

Server:

import subprocess
from multiprocessing.managers import BaseManager
def get_subprocess_module():
    return subprocess
class MyManager(BaseManager):
    pass
MyManager.register( 'subprocess', get_subprocess_module )
MyManager(address=('', 50000), authkey='makecrazy').get_server().serve_forever()

Remote Client:

from multiprocessing.managers import BaseManager
class MyManager(BaseManager):
    pass
MyManager.register('subprocess')
manager = MyManager(address=('dns.of.remote.server',50000),authkey='makecrazy')
manager.connect()
remoteSubprocess = manager.subprocess()
rc = remoteSubprocess.call(['ls', '-aplh'])

Comments

1

Sure, typically via the ssh protocol (for "secure shell") as supported for Python e.g. by the paramiko third-party extension.

Comments

1

I'd do this with Pexpect and Plink.

Comments

0

You will either need to run some sort of server on the remote machine, or ssh in and do it yourself. It would not be difficult to use one of the many pre-written Python servers to listen for a client and kick off a shell script.

Authentication may or may not be a problem for you; be aware that anyone else can follow the same steps you do and possibly get the same result. You don't want to allow anyone on the intarwubs to start your scripts!

Comments

0

If you don't want to use paramiko, then try telnetlib. You can use it to execute remote commands.

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.