0

A very convenient way to execute a Python script on a remote server is to pipe it to ssh:

cat script.py | ssh user@address.com python -

where the - seems to be optional.

How can I execute other bash commands before running the Python script in this way?

This does not work:

cat script.py | ssh user@address.com "cd ..; python -" # WRONG!

Interestingly, this sends a non-deterministically corrupted version of the Python script, which gives a syntax error in a different place every time you run it!

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200

1 Answers1

3

You can create a sub-shell:

cat script.py | ssh user@address.com "(cd ..; python -)"

Or a temporary file:

cat script.py | ssh user@address.com "tee >/tmp/tmp.py; cd ..; python /tmp/tmp.py; rm /tmp/tmp.py"
Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53