0

I run the code:

from subprocess import Popen, PIPE

p1 = Popen(['ip', 'addr'], stdout=PIPE)
p2 = Popen(['grep', 'enp'], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
output=p2.communicate()[0]
print(output)

and I get the output:

b'7: enp0s20f0u1c4i2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000\n    inet 172.20.10.2/28 brd 172.20.10.15 scope global dynamic enp0s20f0u1c4i2\n'

How to extract the characters enp0s20f0u1c4i2 from the string of this output?

2
  • 3
    output.split(':')[1].strip() Commented Jan 3, 2018 at 6:10
  • 3
    output.split()[-1].strip() Commented Jan 3, 2018 at 6:10

1 Answer 1

1

If you are sure that the string output always has that same format, you could use the split function to split in :, take the first element and then clean the string to remove the spaces and \n. Similarly you could split by space and get the last element of the split (it seems the string you are looking for appears twice?)

So output.split(':')[1].strip() or output.split(' ')[-1].strip().

Sign up to request clarification or add additional context in comments.

Comments

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.