Remotely run python script consisting of multiple files via ssh












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?










share|improve this question























  • 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















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?










share|improve this question























  • 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












0








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?










share|improve this question














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?







ssh python remote






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 21 '17 at 14:37









Christian WaidnerChristian Waidner

12519




12519













  • 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



















  • 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

















do you expect test.zip to be on the local machine or on the remote machine?

– Yaron
Feb 21 '17 at 15:13





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





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





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










1 Answer
1






active

oldest

votes


















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





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1181203%2fremotely-run-python-script-consisting-of-multiple-files-via-ssh%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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





    share|improve this answer




























      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





      share|improve this answer


























        0












        0








        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 21 '17 at 16:19









        YaronYaron

        3842313




        3842313






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1181203%2fremotely-run-python-script-consisting-of-multiple-files-via-ssh%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

            How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...