import commands,re command = 'curl xxx.xxx.xxx.xxx:9200/_cat/health' a,b=commands.getstatusoutput(command) if re.search('green',b): x=1 elif re.search('red',b): x=2 else: x=3 print x