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

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.