使用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')}
在后一个示例中,“连接主机”正在合并,并且不像前一个示例中那样分开。我该如何调和?
您不能将列表作为第一个参数传递,这将不起作用。您需要打开列表包装:
g = ThreadingGroup(*s, user='username', port=22, connect_kwargs={'password': 'password'})