[Tutor] run local script on a remote machine

Alex Kleider akleider at sonic.net
Wed Oct 26 13:44:58 EDT 2016


I've got three files as follows:

1:
#!/usr/bin/env python3
#
# file: experiment.py
#
# A simple python program that takes parameters.

import sys
info = sys.argv[1:]
print(info)
with open("/home/alex/junk.txt", 'w') as file_object:
     for item in info:
         file_object.write(''.join((item,'\n')))

2:
#!/bin/bash
#
# file: call.sh

# Demonstrates running a local python script on another host
# with command line arguments specified locally.

ssh -p22 alex at 10.10.10.10 python3 -u - one two three < 
/home/alex/Py/BackUp/Sandbox/Scripted/experiment.py

3:
#!/usr/bin/env python3
#
# file: call.py

import os
import shlex
import subprocess

script = "/home/alex/Py/BackUp/Sandbox/Scripted/experiment.py"
if os.path.isfile(script):
     print("File exists on local machine.")
else:
     print("No such file.")

command = (
"ssh -p22 alex at 10.10.10.10 python3 -u - one two three < {}"
     .format(script))

ret = subprocess.call(shlex.split(command))

if ret:
     print("Command failed!!")
else:
     print("All's well.")


Running the shell script (2) executes a single shell command and leaves 
the junk.txt file at 10.10.10.10 as desired.
Calling the same shell command using the subprocess module from with in 
a python script (3) does not work:
alex at X301n3:~/Py/BackUp/Sandbox/Scripted$ ./call.py
File exists on local machine.
bash: /home/alex/Py/BackUp/Sandbox/Scripted/experiment.py: No such file 
or directory
Command failed!!

I'm doing this using Ubuntu14.04LTS.

Thanks in advance for any suggestions as to how to proceed.

Alex


More information about the Tutor mailing list