Python 运行 使用 Fabric 模块的 SSH 命令
Python running an SSH command to a group using Fabric module
我正在尝试使用 Fabric
模块向两个 raspberry Pi 作为一个组发送 运行 命令。我正在尝试学习如何使用 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']
我想我犯了一个简单的错误,非常感谢任何有关分组连接的指导!谢谢!
这里有很多东西。要通过 fab
使用 fabfile.py
和 运行 命令,您必须确保使用 @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
的起点。
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
我正在尝试使用 Fabric
模块向两个 raspberry Pi 作为一个组发送 运行 命令。我正在尝试学习如何使用 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']
我想我犯了一个简单的错误,非常感谢任何有关分组连接的指导!谢谢!
这里有很多东西。要通过 fab
使用 fabfile.py
和 运行 命令,您必须确保使用 @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
的起点。
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