I want to run and capture the IP address & mask from the output of the following command from python3;
"ip addr show eth0 | grep inet"
I've attempted it, and it works, with this code snippet:
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])
Is there a better/ more pythonic way of doing this?
Thank you,
PK