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

在python中使用os.popen转换linux命令,python,linux,popen,Python,Linux,Popen,如何发出此命令: 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 T

如何发出此命令:

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', '') 
但是它不起作用。

shell=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)
更新

Python2.7中添加了
子流程.check_输出。如果使用较旧版本的Python,请改用


>>>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'>>输出=子进程。检查输出(cmd,shell=True)回溯(最近一次调用):文件“”,第1行,在AttributeError:“module”对象没有属性“检查输出”@user301312,你使用哪一版本的python?@user3013012,我用另一种解决方案更新了答案。过来看。
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, errmsg = proc.communicate()