在Fabric中重用连接对象

问题描述 投票:0回答:1

使用fabric我可以创建一个到堡垒服务器的连接对象,并使用该对象与堡垒后面的VM进行交互吗?

python fabric
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.