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

发布于 2024-11-25 08:31:51 字数 248 浏览 1 评论 0原文

尽管 Fabric 文档提到了一种使用该库进行 SSH 访问而不需要 fab 命令行工具和/或任务的方法,但我似乎无法管理一种方法来做到这一点。

我想通过仅执行“python example.py”来运行此文件(example.py):

env.hosts = [ "example.com" ]
def ps():
    run("ps")
ps()

谢谢。

Altough Fabric documentations refers to a way of using the library for SSH access without requiring the fab command-line tool and/or tasks, I can't seem to manage a way to do it.

I want to run this file (example.py) by only executing 'python example.py':

env.hosts = [ "example.com" ]
def ps():
    run("ps")
ps()

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原谅我要高飞 2024-12-02 08:31:51

我最终这样做了:

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

产生:

[example.com:22] run: uname
[example.com:22] out: Linux

I ended up doing this:

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

Which produces:

[example.com:22] run: uname
[example.com:22] out: Linux
所有深爱都是秘密 2024-12-02 08:31:51
#!/usr/bin/env python
from fabric.api import hosts, run, task
from fabric.tasks import execute

@task
@hosts(['user@host:port'])
def test():
    run('hostname -f')

if __name__ == '__main__':
   execute(test)

更多信息:http://docs.fabfile.org/en/latest/usage/库.html

#!/usr/bin/env python
from fabric.api import hosts, run, task
from fabric.tasks import execute

@task
@hosts(['user@host:port'])
def test():
    run('hostname -f')

if __name__ == '__main__':
   execute(test)

More information: http://docs.fabfile.org/en/latest/usage/library.html

紙鸢 2024-12-02 08:31:51

以下是三种不同的方法,均使用 execute 方法。

from fabric.api import env,run,execute,hosts

# 1 - Set the (global) host_string
env.host_string = "[email protected]"
def foo():
  run("ps")
execute(foo)

# 2 - Set host string using execute's host param
execute(foo, hosts=['[email protected]'])

# 3 - Annotate the function and call it using execute
@hosts('[email protected]')
def bar():
  run("ps -ef")
execute(bar)

要使用密钥文件,您需要设置 env.keyenv.key_filename,如下:

env.key_filename = 'path/to/my/id_rsa'
# Now calls with execute will use this keyfile
execute(foo, hosts=['[email protected]'])

您还可以提供多个密钥文件,并且将使用您登录到该主机的任何一个

Here are three different approaches all using the execute method

from fabric.api import env,run,execute,hosts

# 1 - Set the (global) host_string
env.host_string = "[email protected]"
def foo():
  run("ps")
execute(foo)

# 2 - Set host string using execute's host param
execute(foo, hosts=['[email protected]'])

# 3 - Annotate the function and call it using execute
@hosts('[email protected]')
def bar():
  run("ps -ef")
execute(bar)

For using keyfiles, you'll need to set either env.key or env.key_filename, as so:

env.key_filename = 'path/to/my/id_rsa'
# Now calls with execute will use this keyfile
execute(foo, hosts=['[email protected]'])

You can also supply multiple keyfiles and whichever one logs you into that host will be used

心碎无痕… 2024-12-02 08:31:51

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

Found my fix. I needed to provided my own *env.host_string* because changing env.user/env.keyfile/etc doesn't automatically updates this field.

天涯沦落人 2024-12-02 08:31:51

这是需要做的:

在 example.py 中,

from fabric.api import settings, run

def ps():
  with settings(host_string='example.com'):
    run("ps")
ps()

请参阅使用 Fabric 作为库的文档:
http://docs.fabfile.org/en/1.8/usage /env.html#host-string

This is what needs to be done:

in example.py

from fabric.api import settings, run

def ps():
  with settings(host_string='example.com'):
    run("ps")
ps()

see docs for using fabric as a library:
http://docs.fabfile.org/en/1.8/usage/env.html#host-string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文