在没有命令行工具的情况下使用Python Fabric(fab)

fab*_*osa 19 python fabric

Altough Fabric文档是指使用库进行SSH访问而不需要fab命令行工具和/或任务的方法,我似乎无法管理这样做的方法.

我想通过执行' python example.py ' 来运行这个文件(example.py):

env.hosts = [ "example.com" ]
def ps():
    run("ps")
ps()
Run Code Online (Sandbox Code Playgroud)

谢谢.

dan*_*ast 16

我最终这样做了:

from fabric.api import env
from fabric.api import run

class FabricSupport:
    def __init__ (self):
        pass

    def run(self, host, port, command):
        env.host_string = "%s:%s" % (host, port)
        run(command)

myfab = FabricSupport()

myfab.run('example.com', 22, 'uname')
Run Code Online (Sandbox Code Playgroud)

哪个产生:

[example.com:22] run: uname
[example.com:22] out: Linux
Run Code Online (Sandbox Code Playgroud)


fab*_*osa 3

找到了我的修复方法。我需要提供自己的 *env.host_string* 因为更改 env.user/env.keyfile/etc 不会自动更新此字段。

  • 您能否发布对您有用的完整代码?我似乎无法从你的回答中得到正确的答案。 (3认同)