简体   繁体   中英

Get all FQDN for an IP address in python

I've the following problem : I'm actually making a script for an ovirt server to automatically delete virtual machine wich include unregister them from the DNS. But for some very specific virtual machine there is multiple FQDN for an IP address exemple :

myfirstfqdn.com IN A 10.10.10.10
mysecondfqdn.com IN A 10.10.10.10

I've tried to do it with socket in python but it return only one answer, I've also tried python with dnspython but I failed. the goal is to count the number of type A record on the dns server Anyone have an idea to do stuff like this ?

That's outright impossible. If I am in the right mood, I could add an entry to my DNS server pointing to your IP address. Generally, you cannot find it out (except for some hints in some protocols like http(s)).

Given a zone file in the above format, you could do something like...

from collections import defaultdict

zone_file = """myfirstfqdn.com IN A 10.10.10.10
mysecondfqdn.com IN A 10.10.10.10"""

# Build mapping for lookups
ip_fqdn_mapping = defaultdict(list)

for record in zone_file.split("\n"):
    fqdn, record_class, record_type, ip_address = record.split()
    ip_fqdn_mapping[ip_address].append(fqdn)

# Lookup
ip_address_to_lookup = "10.10.10.10"
fqdns = ip_fqdn_mapping[ip_address_to_lookup]

print(fqdns)

Note: Using socket can be done like so - Python lookup hostname from IP with 1 second timeout

However this does require that DNS server that you are querying has correctly configured PTR reverse records.

https://www.cloudns.net/wiki/article/40/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM