简体   繁体   English

执行 kubectl 命令时弹出/子进程出错

[英]Error popen/subprocess with exec kubectl command

I have this python codes:我有这个 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')

I have this code that when executed it returns this error at step "proces =...":我有这段代码,在执行时它在步骤“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

can someone help me?有人能帮我吗?

Plain split doesn't keep quoted strings together.普通split不会将引用的字符串保持在一起。 shlex.split() does; shlex.split()确实; but why not just split it yourself?但为什么不自己拆分呢?

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

Notice also the switch to subprocess.run ;还要注意切换到subprocess.run you should avoid Popen always when you can.你应该尽可能避免使用Popen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM