gpt4 book ai didi

python - 如何使用很多参数执行 python subprocess.Popen?

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:58 35 4
gpt4 key购买 nike

我需要在本地和远程服务器上执行相同的命令。所以我正在使用 subprocess.Popen 来执行,并且本地命令按预期工作,但是当我在远程执行时它给了我一些错误,比如找不到命令。感谢您的支持,因为我是新手。

本地执行函数

    def topic_Offset_lz(self):
CMD = "/dsapps/admin/edp/scripts/edp-admin.sh kafka-topic offset %s -e %s | grep -v Getting |grep -v Verifying | egrep -v '^[[:space:]]*$|^#' | awk -F\: '{print $3}'|sed '%sq;d'" % (self.topic,self.envr,self.partition)
t_out_lz, t_error_lz = subprocess.Popen(CMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
return t_out_lz

远程服务器执行

    def topic_offset_sl(self):
CMD = "/dsapps/admin/edp/scripts/edp-admin.sh kafka-topic offset %s -e %s | grep -v Getting |grep -v Verifying | egrep -v '^[[:space:]]*$|^#' | awk -F\: '{print $3}'|sed '%sq;d'" % (self.topic, self.envr, self.partition)
t_out_sl, t_error_sl = subprocess.Popen(["ssh", "-q", CMD], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
return t_error_sl

远程执行时出错

Landing Zone Offset: 0

SoftLayer Zone Offset: /bin/sh: ^# |sed 1: command not found/bin/sh: d: command not found

最佳答案

我提出了以下解决方案,可能会有比这个更简单的方法。

 def topic_offset_sl(self):
CMD_SL1 = "ssh -q %s '/dsapps/admin/edp/scripts/edp-admin.sh kafka-topic offset %s -e %s'" % (KEY_SERVER,self.topic, self.envr)
CMD_SL2 = "| grep -v Getting |grep -v Verifying | egrep -v '^[[:space:]]*$|^#' | awk -F\: '{print $3}'|sed '%sq;d'" % (self.partition)
t_out_sl, t_error_sl = subprocess.Popen(CMD_SL1 + CMD_SL2 , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
return t_out_sl

关于python - 如何使用很多参数执行 python subprocess.Popen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901815/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com