user3013012
user3013012

Reputation: 29

Converting linux commands with os.popen in python

How i can make this command:

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`;

run through python?

I was trying to do:

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', '') 

But it doesn't work.

Upvotes: 3

Views: 382

Answers (1)

falsetru
falsetru

Reputation: 369474

Use subprocess.check_output with 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)

UPDATE

subprocess.check_output was added in Python 2.7. If you use older version of Python, Use subprocess.Popen.communicate instead.

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

Upvotes: 3

Related Questions