[英]Run python script in remote machine without using SCP
There is any possibility to run the python script in remote machine with out transfer files using SCP or better method? 是否有可能在不使用SCP或更好方法的传输文件的情况下在远程计算机上运行python脚本? I have search a lot of libraries for solve this issue.
我搜索了很多图书馆来解决此问题。 But did not find the best solution for it.
但是并没有找到最好的解决方案。
I have found some libraries which perform SSH and SCP to remote machine using python script. 我已经发现了一些图书馆使用python脚本,执行SSH和SCP到远程计算机。 In their approach first copy files to remote system using SCP and execute command over SSH.
在他们的方法中,首先使用SCP将文件复制到远程系统,然后通过SSH执行命令。
Thanking you 感谢您
You could use fabric . 您可以使用fabric 。
Obviously it does depend on what exactly you want your remote python script to do, but it has a lot of helper functions for interacting with the OS including file upload & download but it's written all in Python. 显然,这确实取决于您希望远程python脚本执行的操作,但是它具有许多与OS交互的辅助功能,包括文件上载和下载,但都是用Python编写的。
You can invoke python
with a flag to read the script from its standard input, and then feed the script to the python instance through the ssh connection, for example: 您可以使用标记调用
python
以从其标准输入中读取脚本,然后通过ssh连接将该脚本提供给python实例,例如:
cat /some/script.py | ssh user@host 'python -'
Running python -
will cause the python interpreter to read the script from the process's standard input. 运行
python -
将使python解释器从进程的标准输入中读取脚本。 In this case, the standard input of the ssh
process is passed to the remote system as the standard input of the python instance. 在这种情况下,
ssh
进程的标准输入将作为python实例的标准输入传递到远程系统。
You can supply additional command-line arguments to the python script, if desired: 如果需要,您可以向python脚本提供其他命令行参数:
cat /some/script.py | ssh user@host 'python - arg1 arg2...'
Note that any import
statements in the python script will be resolved on the remote system, so the script has to limit itself to modules which are available to the remote python interpreter. 请注意,python脚本中的所有
import
语句都将在远程系统上解析,因此该脚本必须将其自身限制为远程python解释器可用的模块。
Sure. 当然。
ssh remotehost python -c "'print(5)'"
A script needs not to be copied to execute it. 无需复制脚本即可执行脚本。 The Python interpreter can run a script which is given as argument.
Python解释器可以运行作为参数给出的脚本。
Double quoting is necessary (and will quickly become a hassle) because one level of quoting is removed by the ssh
call and another is still necessary to group the script into one argument for the python
call. 双引号是必需的(并且很快就会变得麻烦),因为一个级别的引号已被
ssh
调用删除,而仍需要另一个级别来将脚本分组为python
调用的一个参数。
Multiline is also no problem: 多行也没问题:
ssh remotehost python -c "'
import random
print(random.random())'"
will print something like 将打印类似
0.998373816572
Just be aware of the quoting stuff when you use '
or "
in your Python script as well. 只是知道的东西,引用当您使用
'
或"
你的Python脚本,以及。
Another option is to have the script in a variable and then use printf
to quote the contents properly: 另一个选择是将脚本包含在变量中,然后使用
printf
正确引用内容:
a='
import random
print(random.random())'
# or use something like a=$(cat myscript.py)
ssh ttiqadm@ttiq-hv02 python -c "$(printf "%q" "$a")"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.