SSH与子进程

2024-05-29 06:41:10 发布

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

我正在尝试制作一个程序,自动将代理和白名单添加到Nutanix中的我的设置中,到目前为止,我能够获得要添加的代理,但无法获得要添加的白名单。 白名单ip在一行中运行。“\n”对它们不起作用。下面是我的代码:

import subprocess
import sys
add_whitelists = "ncli http-proxy add-to-whitelist target-type=IPV4_ADDRESS target="
whitelist_Ip = ["192.168.1.22", "192.168.1.23"]
login = "nutanix@192.168.1.50"
add_pxy = "ncli http-proxy add name=NPX5-proxy address=proxy.npx5.local port=8080 proxy-types=HTTPS"
list_proxy = "ncli http-proxy get-whitelist"
nl = '\n'
#ssh connection
ssh = subprocess.Popen(["ssh", "-i .ssh/id_rsa", login],
                        stdin =subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        universal_newlines=True,
                        bufsize=0)

# Send ssh commands to stdin
ssh.stdin.write(add_pxy + "\n")
for ip in whitelist_Ip:
    ssh.stdin.write(add_whitelists + ip + "\n")
ssh.stdin.close()
# Fetch output
for line in ssh.stdout:
    if line == "END\n":
        break
    print(line,end="")

谢谢


Tags: toimportipaddhttp代理stdinline

热门问题