解析DNS日志并从日志文件返回顶级IP和域名

时间:2019-03-22 19:55:56

标签: python logging dns ip

我有一个DNS日志文件(txt),我需要解析该日志文件,删除不良条目和反向查找,并列出客户端IP地址和查询域

尝试了多种方法,我可以使用reg表达式解析文件并删除错误的条目,但无法对行进行切片以创建ip和域名的字典。

文件示例

07-Nov-2009 20:11:01.863 queries: info: client 7.204.241.161#52985: query: 181.190.75.3.in-addr.arpa IN PTR +
07-Nov-2009 20:11:01.893 queries: info: client 3.75.190.181#54339: query: jabber.usma.bluenet IN A +
07-Nov-2009 20:11:02.326 general: warning: checkhints: unable to find root NS 'cluster2.hq.simnet' in hints
07-Nov-2009 20:11:02.326 general: warning: checkhints: extra NS 'A.ROOT-SERVERS.NET' in hints
07-Nov-2009 20:11:02.326 general: warning: checkhints: extra NS 'B.ROOT-SERVERS.NET' in hints
pattern = re.compile(r".*queries: info:.*IN A")  # Compile a case-insensitive regex pattern.

cnt = 0
with open ('c:\python\dns_log_file.txt', 'rt') as dns_file: 
    for line in  dns_file:                            # Try to:
        if pattern.search(line) != None:          # If substring search finds a match,
            err_occur.append((line.rstrip('\n'))) # strip linebreaks, store line and line number in list as tuple.
    for linenum, line in enumerate(err_occur,start=1):        # Keep track of line numbers.  

文件拼接无法正常工作,无法将项目添加到字典中。

0 个答案:

没有答案