2017-08-15 60 views
0

目前,我有多發性IP的我目前正在嘗試使用nslookup從給出的一組信息僅拉出域名(下面的代碼)使用nslookup查找域名,只有域名

with open('test.txt','r') as f: 
    for line in f: 
     print os.system('nslookup' + " " + line) 
一個文本文件

到目前爲止,它可以從第一個IP中提取所有信息。我無法讓它通過第一個IP,但我目前正在嘗試清除僅收錄到IP的域名的信息。有沒有辦法做到這一點還是需要使用不同勢模塊

回答

0

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的願望。

0
import socket 
name = socket.gethostbyaddr(‘127.0.0.1’) 
print(name) #to get the triple 
print(name[0]) #to just get the hostname