Shell script remote execution using python

Is there a way I can use Python for Windows to execute shell scripts that reside on a remote Unix machine?

PS: Sorry for the late editing. I know Paramiko, but I wanted to know if there is a way to do this without him. For starters, can this be done using subprocess ()?

+6
python unix shell
source share
7 answers

In python there is no module with batteries enabled for remote shell execution. I suggest looking into Fabric , which provides a really good interface for working through SSH on remote machines, perhaps a little nicer than paramiko. You can even install Fabric on windows ...

+3
source share

You will need ssh on the remote computer, and if you have the appropriate credentials, you can invoke shell scripts.

To use ssh, you can easily use the paramiko module, which 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 the command "df -h" with a shell script.

+13
source share

I have a multiprocessor and subprocess that I have not tested, but should work based on documents ...

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']) 
+2
source share

Of course, usually through the ssh protocol (for the "secure shell"), as supported by Python, for example. via paramiko third-party extension.

+1
source share

I would do it with Pexpect and Plink .

+1
source share

You need to either start some server on a remote computer, or run ssh and do it yourself. It's easy to use one of the many pre-written Python servers to listen on the client and run the shell script.

Authentication may or may not be a problem for you; keep in mind that anyone else can follow the same steps as you and possibly get the same result. You do not want any of the intarwubs to run your scripts!

0
source share

If you do not want to use paramiko, try telnetlib. You can use it to execute remote commands.

0
source share

All Articles