I am trying to write a Python script on Windows 7, which reads the output of command ipconfig /displaydns and try to get some values from the output.
import subprocess
output = subprocess.check_output("ipconfig /displaydns", shell=True)
result = {}
for row in output.split('\n'):
if ': ' in row:
key, value = row.split(': ')
result[key.strip(' .')] = value.strip()
print(result)
print(result['A (Host) Record'])
gives:
{'A (Host) Record': '127.0 .0.16', 'Data Length': '4', 'Section': 'Answer', 'Record Name': '9.a.c.e.x-0.19-430f5091.531.1518.1b8d.2f4a.210.0.k1m2t5a3245k242qmfp75spjkv.avts.', 'Time To Live': '289', 'Record Type': '1'}
127.0 .0.16
Another solution would be to: (when i thought of this in my head, i thought it would be more compact.. it wasn't but anyway, it's a different way of calling the external command where you get control of the errors and output (you can differntiate the two))
import subprocess
cmdpipe = subprocess.Popen("ipconfig /displaydns", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
result = {}
for row in cmdpipe.stdout.readline():
if ': ' in row:
key, value = row.split(': ')
result[key.strip(' .')] = value.strip()
print(result)
print(result['A (Host) Record'])