Python: DNS Servers

Hi..
in a python script how can i get all the ip address of  the DNS servers on the network.

Thank you for your help.
NetRock6Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
You can just script a command line utility like dig.

And to get all IPs on a network... You must have a starting point, like an IP range.

Then you must determine what constitutes an IP.

By your question, seems like you may be looking for certain services active on certain IPs.

For this you'd use nmap or netcat with the -z option.

Best to describe what you mean by an IP, then someone can assist.

And... highly unlikely you will do this in Python. Rather you'll run some command line tool which does the work for you, so this code will be independent of Python.
nociSoftware EngineerCommented:
You would need the DNS resolve library: (and possibly more)

Described here
Source:
http://www.dnspython.org/
Examples:
http://www.dnspython.org/examples.html
https://www.adampalmer.me/iodigitalsec/2014/11/21/performing-dns-queries-python/


To find the nameservers for a domain you need to ask the NS records.
Or use some AXFR zone transfer fr a known domain.

If you mean to find listeners on port UDP:53.
then you need to do queries to all IP addresses port 53.. Which is a bit more involved. Like enumeratings over all addresses and ask for correct info.
NetRock6Author Commented:
Thank You for your great help.
i saw the link noci has posted this morning. It seems very complicated and I am certainly running out of time.... Therefore, I found another way but i need help to finish it off here it is the link

Thank you again.
nociSoftware EngineerCommented:
It is not more complicated than:
(install the dnspython library, probably available  in the repository of  your Distro:
Otherwise: pip install dnspython

import dns.resolver

n=0
myRes = dns.resolver.Resolver()
nsAnsw = myRes.query("","NS")
for rdata in nsAnsw:
  nsRec = rdata.to_text()
  ip4=myRes.query(nsRec,"A")
  for ip in ip4:
    n=n+1
    print "dns{} {}".format(n,ip)
  ip6=myRes.query(nsRec,"AAAA")
  for ip in ip6:
    n=n+1
    print "dns{} {}".format(n,ip)

Open in new window


This will parse yout complete resolver config (/etc/nsswitch, /etc/resolv.conf...)
it will look ofr the empty name (augmented with the domainname / search strings from resolv.conf)
and then query the NS records (finding all NS (even the ones that are not in resolv.conf) for the named domain.

and then print the ns records bot IPv4 & IPv6.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NetRock6Author Commented:
Great Thanks noci.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.