我想运行并捕获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