[Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .

eryksun eryksun at gmail.com
Thu Sep 20 00:13:46 CEST 2012


On Wed, Sep 19, 2012 at 2:47 PM, ashish makani <ashish.makani at gmail.com> wrote:
>
> I tried this
>>>> import os
>>>> os.system ("ssh remoteuser at remote python remote.py arg1 arg2 arg3")
>
> This worked, but if the arguments i tried to pass, had spaces, i was not
> able to 'escape' the spaces.

Presuming "remote" has an SSH server running, and that a firewall
isn't blocking port 22, and that you've enabled logging in without a
password (i.e. ssh-keygen), try the following:

    import sys
    import subprocess

    user = remoteuser
    hostname = remote
    cmd = 'python remote.py "%s" "%s" "%s"' % (arg1, arg2, arg3)

    try:
        out = subprocess.check_output(['ssh', '%s@%s' % (user, hostname), cmd])
    except subprocess.CalledProcessError as e:
        print >>sys.stderr, str(e)   # handle/log the error, retry


More information about the Tutor mailing list