Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3.  
  4. import re
  5.  
  6. def is_a(domain):
  7.     """递归直到获取到A记录IP"""
  8.     if a_dict.has_key(domain):
  9.         return a_dict[domain]
  10.     else:
  11.         return is_a(cname_dict[domain])
  12.  
  13. def handle(records):
  14.     '''A记录和CNAME记录分为2个字典'''
  15.     for line in records:
  16.         record = re.split('\s',line.strip())
  17.         if record[1].lower() == 'a':
  18.             a_dict[record[0].lower()] = a_dict.get(record[0].lower(),[])
  19.             a_dict[record[0].lower()].append(record[2])
  20.             domain_list.append(record[0].lower())
  21.         elif record[1].lower() == 'cname':
  22.             cname_dict[record[0].lower()] = record[2].lower()
  23.             domain_list.append(record[0].lower())
  24.             domain_list.append(record[0].lower())
  25.  
  26.     domain_set = set(domain_list)  #建立一个唯一域名集合(去重)
  27.     for i in domain_set:
  28.         domain_dict[i] = domain_dict.get(i,is_a(i))
  29.     print domain_dict
  30.  
  31.  
  32.  
  33. a_dict = {}
  34. cname_dict = {}
  35. domain_dict = {}
  36. domain_list = []
  37. records = open('record.txt','r')
  38. handle(records)
  39. records.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement