How to get the Primary and Secondary DNS of Windows using Python?

1

My Python program to run needs the following machine information:

DNS Primario.
DNS Secundário.

How can I get this information using Python?

    
asked by anonymous 24.07.2018 / 02:37

1 answer

0

I do not know if it's on windows or linux solo, but I've already done it using Windows, you can run ipconfig /all using python and get all configuration information from your network interface:

import subprocess

ipconfig = subprocess.check_output(["ipconfig", "/all"])

DNS_ip = []


InDns=False

for line in ipconfig.split(b'\n'):
    if b"DNS" in line and b"Servidores" in line:
        DNS_ip.append(line.split(b' : ')[1].strip().decode('UTF-8'))
        InDns=True
    if b":" not in line and InDns:
        InDns=False
        DNS_ip.append(line.strip().decode('UTF-8'))



print(DNS_ip)

The above example returns my primary and secondary DNS servers:

C:\Python33>python.exe achaDNS.py
['8.8.8.8.', '4.4.4.4']
If the Win is in English it is very likely that you will have to change the IF "Servers" to "Server", I am using Python version 33, If it is in Linux or MAC this will not work. ..

    
24.07.2018 / 17:06