我如何执行此命令: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_mgm...

我如何执行此命令:
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_output与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)
更新
subprocess.check_output已在Python 2.7中添加.如果您使用旧版本的Python,请改用subprocess.Popen.communicate.
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, errmsg = proc.communicate()
沃梦达教程
本文标题为:在python中使用os.popen转换linux命令


基础教程推荐
猜你喜欢
- 利用Python实现自定义连点器 2022-09-03
- python之Linux基础(三) 2023-09-04
- python中playwright结合pytest执行用例的实现 2023-08-04
- python多进程、多线程 2023-11-11
- Shell命令从python失败,从shell正常 2023-11-12
- Python运算符之Inplace运算符的使用教程 2022-10-20
- 人脸识别实战之Opencv+SVM实现人脸识别 2023-08-04
- 在Linux中达到一定大小后,停止将Python脚本写入文件 2023-11-12
- Python 多进程multiprocessing 2023-09-04
- python-从本地linux文件夹移动到使用cifs挂载的Windows共享 2023-11-12