I essentially want to get some values from a command like 'ipconfig', but when I print the output of the command I get alot of newline characters and spaces.
Code I've Tried:
>>> import subprocess
>>> output = subprocess.getstatusoutput("ipconfig")
>>> print(output)
(0, '\x0c\nWindows IP Configuration\n\n\nEthernet adapter Ethernet 2:\n\n Media State . . . . . . . . . . . : Media disconnected\n Connection-specific DNS Suffix . : \n\nEthernet adapter Npcap Loopback Adapter:\n\n
Connection-specific DNS Suffix . : \n Link-local IPv6 Address . . . . . : ~e~~::7dab:~~7f:e56f:1131%9\n Autoconfiguration IPv4 Address. . : 169.~~4.1~.49\n
Subnet Mask . . . . . . . . . . . : 255.255.0.0\n Default Gateway . . . . . . . . . : \n\nEthernet adapter VirtualBox Host-Only Network:\n\n Connection-specific DNS Suffix . : \n
Link-local IPv6 Address . . . . . : fe80::7~~c:69aa:~~aa:~~14~10\n IPv4 Address. . . . . . . . . . . : 192.168.~~.~\n Subnet Mask . . . . . . . . . . . : 255.~~~.255.0\n Default Gateway . . . . : etc...
I'm not sure of the best way to parse this data into some sort of table with keys and values
And when trying to use code from this answer to this question here, all I could get was this error:
>>> import subprocess
>>> output = subprocess.check_output("ipconfig", shell=True)
>>> result = {}
>>> for row in output.split('\n'):
... if ': ' in row:
... key, value = row.split(': ')
... result[key.strip(' .')] = value.strip()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> print(result)
{}
>>> print(result['A (Host) Record'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'A (Host) Record'
Example of what I'm looking for:
"Link-local IPv6 Address" : "my ipv6 addr"
"Subnet Mask" : "my subnet mask"
(Using python 3)