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

时间:2018-07-09 18:37:40

标签: python networking raspberry-pi paramiko fabric

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

我想我犯了一个简单的错误,感谢您对分组连接的任何指导!谢谢!

2 个答案:

答案 0 :(得分: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就足够了。

答案 1 :(得分:0)

--- packages: - <name_of_your_electron_package> 期望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

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