0

I have the output below. I want to get only IP Address line. I wrote a for loop for it but it doesnt work.

__GENUS          : 2
__CLASS          : Win32_NetworkAdapterConfiguration
__SUPERCLASS     : 
__DYNASTY        : 
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
IPAddress        : {192.168.1.156, fe80::801:8189:f074:bcda}
PSComputerName   : 

Here is my code:

sonuc = session.run_ps('Get-WmiObject -Query "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration where ipEnabled=TRUE"') 
for line in sonuc.std_out:
        if "IPAddress" in line:
            print(line)

How can I reach the IP Address line?

6
  • how did you get that output in the first place and what is sonuc.std_out? Commented Jul 22, 2022 at 12:23
  • sonuc = session.run_ps('Get-WmiObject -Query "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration where ipEnabled=TRUE"') it is the first part of my code. I got sonuc from there Commented Jul 22, 2022 at 12:25
  • for getting the output I wrote sonuc.std_out Commented Jul 22, 2022 at 12:26
  • that should be included in the question not comments Commented Jul 22, 2022 at 12:26
  • And sonuc.std_out is a function or the output? Commented Jul 22, 2022 at 12:27

1 Answer 1

2

You can use this:

import sys
import io

old_stdout = sys.stdout
sys.stdout = buffer = io.StringIO()

print("""__GENUS          : 2
__CLASS          : Win32_NetworkAdapterConfiguration
__SUPERCLASS     : 
__DYNASTY        : 
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
IPAddress        : {192.168.1.156, fe80::801:8189:f074:bcda}
PSComputerName   : """)

sys.stdout = old_stdout

printed = buffer.getvalue()

for line in printed.splitlines():
    if 'IPAddress' in line:
        print(line)

Output:

IPAddress        : {192.168.1.156, fe80::801:8189:f074:bcda}

(nothing else is printed)

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

3 Comments

Is there any way to do this without writing print("""__GENUS : 2 __CLASS : Win32_NetworkAdapterConfiguration __SUPERCLASS ....... etc )
Replace the print(...) with the code that prints __GENUS : .... I didn't have that code so I had to use a print statement
__GENUS ---> ı got this with writing sonuc=result2.std_out. In the code when i replace print(sonuc) i got the same output with stripting \\ like b'\r\n\r\n__GENUS : 2\r\n__CLASS : Win32_NetworkAdapterConfiguration\r\n__SUPERCLASS ......

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.