连接到字符串的主机的结构组列表

2024-05-17 12:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Fabric2,您可以创建一个组,以串行或线程方式执行命令。你知道吗

g = ThreadingGroup('192.168.101.5', '192.168.101.10', user='username', port=22, connect_kwargs={'password': 'password'})    
g.run('uptime')

# Returns:
# {<Connection host=192.168.101.10 user=esp>: <Result cmd='uptime' exited=0>, <Connection host=192.168.101.5 user=esp>: <Result cmd='uptime' exited=0>}

如果我传入一个列表,我会收到一个rsplit错误,因为需要一组字符串。然后将列表转换为字符串,然后再次运行:

s = str(servers).strip('[]')
g = ThreadingGroup(s, user='username', port=22, connect_kwargs={'password': 'password'})
g.run('uptime')

# fabric.exceptions.GroupException: {<Connection host='192.168.101.5', '192.168.101.10' user=username>: gaierror(-2, 'Name or service not known')}

在后一个示例中,“连接主机”被合并,而不是像前一个示例中那样被分离。我该如何协调这一点?你知道吗


Tags: runcmdhostportconnectusernamepasswordresult
1条回答
网友
1楼 · 发布于 2024-05-17 12:47:23

不能将列表作为第一个参数传递,这样做行不通。您需要打开列表:

g = ThreadingGroup(*s, user='username', port=22, connect_kwargs={'password': 'password'})

相关问题 更多 >