I have a python script on my local machine.Is there any way to run this script on remote machine.I mean python script should on the local machine but execution should happen on remote machine and get the output back to the local machine.
-
1Please add some details regarding the operating systems that you are using. Are you familiar with SSH? – reto Mar 11 '14 at 09:02
-
check the [fabric](http://docs.fabfile.org/en/1.8/) and [this](http://stackoverflow.com/questions/15297189/better-way-of-running-a-python-script-remotely) – MaNKuR Mar 11 '14 at 09:22
-
possible duplicate of [Run local python script on remote server](http://stackoverflow.com/questions/20499074/run-local-python-script-on-remote-server) – Uyghur Lives Matter Mar 07 '15 at 15:57
3 Answers
The pathos
package has tools that make it easy to interact with remote machines, all directly from python… and you can also easily capture stdout
or other piped responses and return them to your calling script.
So, let's say you have a local script hello.py
that looks like this:
# 'hello.py'
import os
print os.system('hostname')
They you can push the script and execute it like this:
>>> import pathos
>>> c = pathos.core.copy('hello.py', destination='guido.remote.com:~/hello.py')
>>> s = pathos.core.execute('python hello.py', host='guido.remote.com')
>>> print s.response()
guido
0
>>> s.pid()
37429
There's also ssh-tunneling, setting up daemon processes, remote port pickling, and dealing with killing remote processes… if you need that stuff.
pathos
provides an abstraction on remote commands, the default being ssh
and scp
… but it's easy to see what it's doing.
>>> s.message
'ssh -q guido.remote.com "python hello.py"'
>>> c.message
'scp -q -r hello.py guido.remote.com:~/hello.py'
Get pathos
here: https://github.com/uqfoundation

- 33,715
- 8
- 119
- 139
You can find a solution for a similar question in here
For my specific problem, which you may also face, I had this local script that I wanted to run at a remote machine using one of its virtual enviroments. I added the following to my bashrc:
rpython (){
cat $1 | ssh user@remote source /home/user/venv/bin/activate \; python -
}
This way I can run rpython local_script.py
. So here is how it works: cat $1 |
pipes the first argument and the ssh user@remote source /home/user/venv/bin/activate \; python -
part copies through ssh what was piped, launches the remote machines's virtual enviroment venv
and runs local_script.py
. Note that everything that local_script.py
may eventually print goes back to the local stdout.
The TL;DR here is everything that you write after ssh user@remote
runs in the remote. If you want to pass several shell instructions, separate them with \;
to not confuse your local shell.
There are other possible solutions involving python modules like Fabric or Plumbum that you may find useful.
If you want to execute a python script on a remote machine, the script must reside on the remote machine.
If you have SSH access to the remote machine you need to first copy the file from your local machine to the remote machine:
scp -r /this/file/location/my_script.py user@server:/home/newlocation
Then simply SSH into the remote machine and run the script.
ssh user@server
cd /home/newlocation
python my_script.py

- 2,488
- 5
- 26
- 39
-
do you have any suggestions for ssh server for windows for which this would work. I have tried mobassh and openSSH, and while I can execute normal windows commands and powershell commands, I am unable to run `python my_script.py`. It always errors out: https://serverfault.com/questions/901041/how-to-run-a-python-script-remotely-via-ssh-on-a-windows-machine. Perhaps the problem is with the server I am using.. – alpha_989 Mar 11 '18 at 01:12