这段python代码调用windows下的ping命令,通过subprocess在其子进程里面实现,由于windows下的ping和linux下的ping返回的消息不太一样,所以这段python代码要想在linux下运行,需要修改一下正则匹配
import subprocess import re p = subprocess.Popen(["ping.exe", 'google.com'], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) out = p.stdout.read() regex = re.compile("Minimum = (\d+)ms, Maximum = (\d+)ms, Average = (\d+)ms", re.IGNORECASE) print regex.findall(out)