0

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

Tullio_IRL
  • 99
  • 1
  • 2
  • 9
  • Have you taken a look to https://pypi.python.org/pypi/netifaces/ ? I find that more "user friendly" **:)** (and more portable) – Savir Oct 27 '16 at 18:51
  • Possible duplicate of [Get network address and network mask in Python](http://stackoverflow.com/questions/11453378/get-network-address-and-network-mask-in-python) – Alexandr T Oct 27 '16 at 18:54

0 Answers0