简体   繁体   English

在python中获取IP地址的所有FQDN

[英]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. 我有以下问题:我实际上是在为ovirt服务器制作一个脚本,以自动删除包含从DNS注销的虚拟机。 But for some very specific virtual machine there is multiple FQDN for an IP address exemple : 但是对于某些非常特定的虚拟机,一个IP地址示例有多个FQDN:

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. 我试过用python中的套接字来做,但是它只返回一个答案,我也试过用dnspython进行python,但是失败了。 the goal is to count the number of type A record on the dns server Anyone have an idea to do stuff like this ? 目标是计算dns服务器上A型记录的数量。有人有想法做这样的事情吗?

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. 如果心情合适,我可以在DNS服务器中添加指向您IP地址的条目。 Generally, you cannot find it out (except for some hints in some protocols like http(s)). 通常,您无法找到它(某些协议(例如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 注意:可以像这样使用套接字- 通过IP进行Python查找主机名,超时时间为1秒

However this does require that DNS server that you are querying has correctly configured PTR reverse records. 但是,这确实要求您查询的DNS服务器已正确配置了PTR反向记录。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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