1
My Python program needs the following machine information to work:
DNS Primario.
DNS Secundário.
How can I get this information using Python?
1
My Python program needs the following machine information to work:
DNS Primario.
DNS Secundário.
How can I get this information using Python?
0
I don’t know if it’s on the ground windows
or linux
, but already done using Windows, you can run a ipconfig /all
using python
and grab all the settings 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']
Run in Windows10 PT-BR
if Win is in English it is very likely that you will have to exchange the IF "Servers" for "Server", I am using Python version 33, if it is on Linux or MAC it will not work ...
Dear Ederwander, your code looks really good, I just don’t understand why you did it b' : '
instead of ' : '
(the same goes for b"DNS"
and b"Servidores"
), there are no characters to escape from, agree that it is redundancy? I know that it does not affect anything, but it also does not seem to be necessary. Rest for Windows Server (or even desktop) in Portuguese this very good.
the b
is necessary because when you perform ipconfig = subprocess.check_output(["ipconfig", "/all"])
the return produces an instance for the byte type, so I’m comparing the strings using the same type, notice that at the end I convert the string to utf8 using a decode('UTF-8')
You’re absolutely right dear It was my mistake!
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
You can use wmi to retrieve this information.
– Tuxpilgrim
How do I get DNS with this wmi?
– user110265
Take a look at the correct answer to this thread: https://stackoverflow.com/questions/4904047/how-can-i-get-the-default-gateway-ip-with-python
– Tuxpilgrim
Buddy, you can’t even find the word DNS in that answer you recommended.
– user110265
@thegrapevine , nor search what query WMI provides to search for DNS?
– Jefferson Quesado