重复使用结构中的连接对象

时间:2019-02-26 21:52:50

标签: python fabric

是否可以使用Fabric创建与堡垒服务器的连接对象,并使用该对象与堡垒后面的VM进行交互?

1 个答案:

答案 0 :(得分:0)

使用ParallelSSH解决此问题。

from pssh.clients import ParallelSSHClient
from pssh.utils import load_private_key

PRIVATE_KEY = "pvt.key"
USER = "username"
BASTION = "proxy_host"

hosts = ["h1", "h2"]
client = ParallelSSHClient(hosts, user=USER,
                           proxy_host=BASTION, proxy_user=USER,
                           proxy_port=2222,
                           proxy_pkey=load_private_key(PRIVATE_KEY))

out = client.run_command("ls -la")


for host in hosts:
    print(host+"\n********************\n")

    for line in out[host].stdout:
        print(line)