在python中使用os.popen转换linux命令



如何制作此命令:

ACTIVE_MGMT_1=`ssh -n ${MGMT_IP_1}  ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " A " |awk '/TRAF/{print $1}' |cut -d "." -f2`;

运行python?

我试着做:

active_mgmgt_1 = os.popen("""ssh -n MGMT_IP_1  ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " A " |awk '/TRAF/{print $1}' |cut -d "." -f2""")
SITE_NAME = site_name.read().replace('n', '') 

但它不起作用。

subprocess.check_outputshell=True:一起使用

cmd = r'''ssh -n ${MGMT_IP_1} ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " A " |awk '/TRAF/{print $1}' |cut -d "." -f2'''
output = subprocess.check_output(cmd, shell=True)

更新

在Python 2.7中添加了CCD_ 3。如果使用旧版本的Python,请改用subprocess.Popen.communicate

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, errmsg = proc.communicate()

最新更新