0

I want to run a python script remotely via ssh. For a single script this would be something like this:

ssh user@machine python test.py

If my python program consists of multiple files, I'm out of luck with this. As Python can execute zip-files, I created one and it runs perfectly on my local system:

python test.zip

Over ssh:

ssh user@machine python < test.zip

I got the error message "SyntaxError: Non-UTF-8 code starting with...". Both files in the archive start with "-- coding: utf-8 --".

What do I have to do to make this work?

  • do you expect test.zip to be on the local machine or on the remote machine? – Yaron Feb 21 '17 at 15:13
  • test.zip is on the local machine. I don't want to modify the remote machine by copying the .zip first but execute it directly. – Christian Waidner Feb 21 '17 at 15:15
  • I found this question on stackoverflow which is basically the same problem - but has no solution: stackoverflow.com/questions/20276105/… – Christian Waidner Feb 21 '17 at 16:09
0

The basic problem with

ssh user@machine python < test.zip

is that test.zip is sent to ssh instead of python.

The solution in stack-overflow might work, if you'll implement the python-script mentioned there (copied below):

#!/usr/bin/python 

import sys
import os
import zipfile
import StringIO
import zipimport
import time

sys.path.append('/tmp')

class SinEater(object):
    def __init__(self):
        tmp = str(int(time.time()*100)) + '.zip'
        f = open(tmp, 'w')
        f.write(sys.stdin.read(1024*64)) # 64kb limit
        f.close()
        try:
            z = zipimport.zipimporter(tmp)
            z.load_module('foo')

        except:
            pass

if __name__ == '__main__':
    print 'herp derp'
    s = SinEater()

Save it as zip_parse_script.py on the remote machine and will execute your command using

test.zip | ssh user@machine python /path_to_python_script/zip_parse_script.py

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.