结构:为显式主机调用run() [英] Fabric: call run() for an explicit host
问题描述
我想使用Fabric作为一种工具来收集所有服务器负载并随后处理值,我想到了这样的东西:
I'd like to use fabric as a tool to gather all server loads and process the values afterward, I thought of something like this:
from fabric.api import run
for servername in servernames:
load_str = run('cat /proc/loadavg | cut -d' ' -f1', host=servername)
但是结构不允许我这样指定主机名,我发现这种IMO丑陋的方式:
but fabric doesn't allow me to specify the hostname this way, I found this IMO ugly way:
from fabric.api import env, run
for servername in servernames:
env.host_string = servername
load_str = run('cat /proc/loadavg | cut -d' ' -f1')
还有更优雅的方式吗?
按照建议在此处直接使用paramiko
Using paramiko directly, as suggested here pushes me to write an own module that abstracts it - quoting from fabrics website, that's exactly what fabric should do for me:
除了通过傻瓜使用外,Fabric的组件还可以导入到其他Python代码中,从而为SSH协议套件提供比Python所提供的更高级别的Pythonic接口. Paramiko(Fabric本身利用的.)
In addition to use via the fab fool, Fabric’s components may be imported into other Python code, providing a Pythonic interface to the SSH protocol suite at a higher level than that provided by e.g. Paramiko (which Fabric itself leverages.)
推荐答案
此问题提供了一个解决方案:
This question offers a solution:
这篇关于结构:为显式主机调用run()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!