简体   繁体   中英

how to run local python script on remote machine

我在我的本地机器上有一个python脚本。有没有办法在远程机器上运行这个脚本。我的意思是python脚本应该在本地机器上,但执行应该在远程机器上进行,并将输出返回到本地机器。

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

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 - 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM