gpt4 book ai didi

Python 使用 Fabric 模块向组运行 SSH 命令

转载 作者:太空宇宙 更新时间:2023-11-04 04:37:56 24 4
gpt4 key购买 nike

我正在尝试使用 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']

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

最佳答案

这里有很多东西。要使用 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 的起点。

关于Python 使用 Fabric 模块向组运行 SSH 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51252054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com