简体   繁体   English

在不使用SCP的情况下在远程计算机上运行python脚本

[英]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.

相关问题 在没有scp的情况下在远程计算机上执行包括本地功能在内的本地python脚本 - Execute local python script including local functions on a remote machine without scp 如何在多台远程机器上运行python脚本? 不安装脚本? - How to run python script on multiple remote machine? without installing the script? 有没有办法在远程机器上运行python脚本而不发送它? - Is there any way to run a python script on remote machine without sending it? 在没有sudo特权的情况下在远程计算机上运行python脚本 - Run a python script on a remote machine without sudo previlage 如何在远程机器上运行本地python脚本 - how to run local python script on remote machine 在远程机器上运行python脚本功能 - Run python script function in remote machine 如何使用 ssh 在远程机器上运行 python 脚本 - How run python script in remote machine with ssh 使用python脚本连接到远程计算机 - Connection to Remote Machine using python script Python脚本来SSH远程计算机并在cron作业中运行此脚本 - Python script to ssh remote machine and run this script in cron job 如何使用python中的第三台本地计算机使用scp在两个远程服务器之间传输文件 - How to transfer a file between two remote servers using scp from a third, local machine in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM