【问题标题】:Python running an SSH command to a group using Fabric modulePython 使用 Fabric 模块向组运行 SSH 命令
【发布时间】:2018-07-09 18:37:40
【问题描述】:

我正在尝试使用Fabric 模块对两个树莓派作为一个组运行命令。我正在尝试学习如何使用 Group 方法,但我认为我做错了什么,因为当我运行以下代码时:...

import fabric

b = fabric.connection.Connection("192.168.3.151", port=22, user="pi", \
    connect_kwargs={"password" : "Raspberry"})
c = fabric.connection.Connection("192.168.3.123", port=22, user="pi", \
    connect_kwargs={"password" : "Raspberry"})
pool = fabric.group.SerialGroup(b, c)
pool.run("touch /home/pi/Desktop/new_file65.txt")
pool.close()
print("hi")

我收到以下错误:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/invoke/config.py", line 98, in __getattr__
    return self._get(key)
  File "/home/pi/.local/lib/python3.5/site-packages/invoke/config.py", line 165, in _get
    value = self._config[key]
  File "/home/pi/.local/lib/python3.5/site-packages/invoke/config.py", line 154, in __getitem__
    return self._get(key)
  File "/home/pi/.local/lib/python3.5/site-packages/invoke/config.py", line 165, in _get
    value = self._config[key]
KeyError: 'rsplit'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    pool = fabric.group.ThreadingGroup(b, c)
  File "/home/pi/.local/lib/python3.5/site-packages/fabric/group.py", line 70, in __init__
    self.extend(map(Connection, hosts))
  File "/home/pi/.local/lib/python3.5/site-packages/fabric/connection.py", line 246, in __init__
    shorthand = self.derive_shorthand(host)
  File "/home/pi/.local/lib/python3.5/site-packages/fabric/connection.py", line 416, in derive_shorthand
    user_hostport = host_string.rsplit("@", 1)
  File "/home/pi/.local/lib/python3.5/site-packages/invoke/config.py", line 110, in __getattr__
    raise AttributeError(err)
AttributeError: No attribute or config key found for 'rsplit'

Valid keys: ['connect_kwargs', 'forward_agent', 'gateway', 'load_ssh_configs', 'port', 'run', 'runners', 'ssh_config_path', 'sudo', 'tasks', 'timeouts', 'user']

Valid real attributes: ['cd', 'clear', 'client', 'close', 'config', 'connect_kwargs', 'connect_timeout', 'create_session', 'cwd', 'derive_shorthand', 'forward_agent', 'forward_local', 'forward_remote', 'from_data', 'gateway', 'get', 'host', 'is_connected', 'local', 'open', 'open_gateway', 'original_host', 'pop', 'popitem', 'port', 'prefix', 'put', 'resolve_connect_kwargs', 'run', 'setdefault', 'sftp', 'ssh_config', 'sudo', 'transport', 'update', 'user']

我认为我犯了一个简单的错误,如果有任何关于分组连接的指导,我将不胜感激!谢谢!

【问题讨论】:

  • 你使用的是fabric还是fabric3
  • 另外,您没有使用 fabric env 有什么特别的原因吗?
  • @2ps 我使用的是fabric,它是 2.1.3 版。无论出于何种原因,fabric env 都不适合我。每当我在命令提示符下调用fab 命令时,它都找不到该命令。但是该模块可以很好地导入 python 使用

标签: python networking raspberry-pi paramiko fabric


【解决方案1】:

这里有很多东西。要使用fabfile.py 并使用fab 运行命令,您必须确保使用@task 装饰您的命令。这是一个例子:

--- 在 fabfile.py 中---

from fabric.decorators import task
@task
def greet():
    print('Hello, Matt!')

当您想在 fabfile.py 中更改结构环境时(即在 python 代码的运行时),您必须使用执行模式

from fabric.decorator import task
from fabric.operations import run
from fabric.context_managers import env
def touch_file():
    run("touch /home/pi/Desktop/new_file65.txt")

@task 
def manage_pis():
    env.hosts = [ ... ]
    execute(touch_file)

这应该足以作为使用env 的起点。

【讨论】:

  • 如果我不想将它用作 fabfile.py 而是想将其嵌入到另一个 python 脚本中以自动运行,该怎么办
【解决方案2】:

SerialGroup 期望 string 不是 Connection 所以你需要

import fabric

pool = fabric.group.SerialGroup("192.168.3.151", "192.168.3.123", user="pi", 
                                  port=22, connect_kwargs={"password": "Raspberry"})

pool.run("touch /home/pi/Desktop/new_file65.txt")
pool.close()
print("hi")

如果您需要不同的用户,也可以使用login@host。但是你不能使用不同的密码。

pool = fabric.group.SerialGroup("pi@192.168.3.151", "pi@192.168.3.123", 
                                  port=22, connect_kwargs={"password": "Raspberry"})

文档:http://docs.fabfile.org/en/2.4/api/group.html#fabric.group.SerialGroup

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2020-05-05
    • 1970-01-01
    • 2023-03-23
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多