【问题标题】:Error popen/subprocess with exec kubectl command使用 exec kubectl 命令弹出/子进程错误
【发布时间】:2021-07-28 01:58:47
【问题描述】:

我有这个 python 代码:

command = "kubectl exec -it " + jmeter_pod + " -n " + namespace + " -- bash -c \"echo "+ ext_ip_lfe +">nelshost\" "
process = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE)
output = process.communicate()[0].decode('utf-8')

我有这段代码在执行时会在“proces = ...”步骤返回此错误:

10.117.142.201>nelshost": -c: line 0: unexpected EOF while looking for matching `"'
10.117.142.201>nelshost": -c: line 1: syntax error: unexpected end of file
command terminated with exit code 1

有人可以帮我吗?

【问题讨论】:

    标签: python docker kubernetes kubectl popen


    【解决方案1】:

    普通的split 不会将带引号的字符串放在一起。 shlex.split() 确实;但是为什么不自己拆分呢?

    output = subprocess.run(
        [ "kubectl", "exec", "-it", jmeter_pod,
          "-n", namespace, "--",
          "bash", "-c", "echo "+ ext_ip_lfe +">nelshost\" "],
        text=True, check=True,
        capture_output=True).stdout
    

    还要注意切换到subprocess.run;你应该尽可能避免Popen

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2014-10-20
      • 2019-08-31
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      相关资源
      最近更新 更多