使用 nslookup 查找域名且仅查找域名
Using nslookup to find domain name and only the domain name
目前我有一个包含多个 IP 的文本文件我目前正尝试从使用 nslookup(下面的代码)给出的信息集中仅提取域名
with open('test.txt','r') as f:
for line in f:
print os.system('nslookup' + " " + line)
到目前为止,它可以从第一个 IP 中提取所有信息。我无法让它通过第一个 IP,但我目前正在尝试清理仅接收到 IP 域名的信息。有什么办法可以做到这一点,或者我需要使用不同的模块
import socket
name = socket.gethostbyaddr(‘127.0.0.1’)
print(name) #to get the triple
print(name[0]) #to just get the hostname
喜欢, I wouldn't make a system call to use nslookup
; I would also use socket
. However, the answer shared by 提供主机名。请求者请求 域名 。见下文:
import socket
with open('test.txt', 'r') as f:
for ip in f:
fqdn = socket.gethostbyaddr(ip) # Generates a tuple in the form of: ('server.example.com', [], ['127.0.0.1'])
domain = '.'.join(fqdn[0].split('.')[1:])
print(domain)
假设 test.txt
包含以下行,解析为 server.example.com
的 FQDN:
127.0.0.1
这将生成以下输出:
example.com
这就是(我相信)OP 想要的。
目前我有一个包含多个 IP 的文本文件我目前正尝试从使用 nslookup(下面的代码)给出的信息集中仅提取域名
with open('test.txt','r') as f:
for line in f:
print os.system('nslookup' + " " + line)
到目前为止,它可以从第一个 IP 中提取所有信息。我无法让它通过第一个 IP,但我目前正在尝试清理仅接收到 IP 域名的信息。有什么办法可以做到这一点,或者我需要使用不同的模块
import socket
name = socket.gethostbyaddr(‘127.0.0.1’)
print(name) #to get the triple
print(name[0]) #to just get the hostname
喜欢nslookup
; I would also use socket
. However, the answer shared by
import socket
with open('test.txt', 'r') as f:
for ip in f:
fqdn = socket.gethostbyaddr(ip) # Generates a tuple in the form of: ('server.example.com', [], ['127.0.0.1'])
domain = '.'.join(fqdn[0].split('.')[1:])
print(domain)
假设 test.txt
包含以下行,解析为 server.example.com
的 FQDN:
127.0.0.1
这将生成以下输出:
example.com
这就是(我相信)OP 想要的。