python2 subprocess.Popen()获取命令返回结果

#! /usr/bin/python

import subprocess
import re
import sys

ip = sys.argv[1]

result = subprocess.Popen("fping -C 2 {} | sed -n '$!d'".format(ip), shell=True,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)

result = result.communicate()[1]
s = re.match(r'\n.* : (.*) (.*)', result, re.M|re.I)

print max(s.groups()) if max(s.groups()) != '-' else 0

你可能感兴趣的:(python)