I wish to run python script that I have locally on disk on remote machine. I used to run bash scripts like this:

cat script.sh | ssh user@machine

but I do not know how to do same for Python script.

  • 5
    cat script.py | ssh user@machine python
    – jordanm
    Aug 1 '16 at 16:45
  • 2
    Or see ansible for a somewhat more complicated take on the run-code-via-SSH thing.
    – thrig
    Aug 1 '16 at 17:04
  • 1
    Copy the script to the server and run it.
    – Jakuje
    Aug 1 '16 at 19:55
  • @jordanm How do I pass parameters to script? Aug 5 '16 at 18:26

As others have said, pipe it into ssh. But what you will want to do is give the proper arguments. You will want to add -u to get the output back from ssh properly. And want to add - to handle the output and later arguments.

ssh user@host python -u - < script.py

If you want to give command line arguments, add them after the -.

ssh user@host python -u - --opt arg1 arg2 < script.py

Use the remrunner package for python. It copies local scripts to a remote machine and then executes them.

pip install remrunner

>> from rumrunner import runner

>> rval, stdout, stderr = r.run('/path/to/local/script.py')
>> if rval:
    print stderr
    print stdout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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