目前,我有一个带有多个IP的文本文件,我目前只试图从使用nslookup提供的一组信息中提取域名(下面的代码)
with open('test.txt','r') as f:
for line in f:
print os.system('nslookup' + " " + line)到目前为止,它可以从第一个IP中提取所有信息。我无法让它通过第一个IP,但我目前正在尝试清除只接收到IP域名的信息。有什么方法可以这样做吗?还是我需要使用一个分流模块?
发布于 2017-08-15 19:02:39
像IgorN一样,我不会进行系统调用来使用nslookup;我也会使用socket。但是,IgorN共享的答案提供了主机名。请求者请求域名。见下文:
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所希望的。
发布于 2017-08-15 12:42:35
import socket
name = socket.gethostbyaddr(‘127.0.0.1’)
print(name) #to get the triple
print(name[0]) #to just get the hostnamehttps://stackoverflow.com/questions/45692970
复制相似问题