python3子进程&正则表达式解析linux cli输出

时间:2016-10-27 18:49:13

标签: python linux

我想运行并捕获IP地址&从python3;

输出以下命令的掩码

“ip addr show eth0 | grep inet”

我尝试了它,并且它可以使用此代码段:

import subprocess
import re
Regex = re.compile(r'(\d+\.\d+\.\d+\.\d+.\d+)')                
p = subprocess.Popen('ip addr show eth0'.split(), stdout=subprocess.PIPE)
grep =subprocess.Popen(['grep','inet'],stdin=p.stdout,stdout=subprocess.PIPE)
output = grep.communicate()[0]
match = Regex.findall(output.decode('ascii'))
print(match[0])

有更好/更多的pythonic方法吗?

谢谢,

PK

0 个答案:

没有答案