Skip to content

Instantly share code, notes, and snippets.

@lixingcong
Last active February 22, 2020 11:56
Show Gist options
  • Save lixingcong/c6b5e831fa250a77219befa1261b7ca3 to your computer and use it in GitHub Desktop.
Save lixingcong/c6b5e831fa250a77219befa1261b7ca3 to your computer and use it in GitHub Desktop.
[deprecated in 2016] a script to generate chinadns_blacklist.txt
google.com
twitter.com
tumblr.com
facebook.com
youtube.com
telegram.org
instagram.com
fbcdn.net
google.de
google.co.jp
blogspot.com
google.co.kr
nytimes.com
vimeo.com
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python Network Programming Cookbook -- Chapter - 1
# this file could generate chinadns_iplist_black.txt
# put 'google.com' into read_filename.txt
# https://github.com/rthalley/dnspython
import dns.resolver
import time
# please make sure that read_filename.txt should EOF with '\n'
read_filename = '/tmp/domain.txt'
save_filename = '/tmp/dns_results.txt'
# query times for each domain, and sleep time between queries
loop_times = 20
sleep_time = 0.1
# 'timeout': time to wait for any given server's response
# 'lifetime': maximum time to wait to get an answer
# 'lifetime' should be a bit longer than 'timeout'
my_resolver = dns.resolver.Resolver()
my_resolver.timeout = 0.15
my_resolver.lifetime = 0.2
# the first server is prefered for sending query
my_resolver.nameservers = ['221.131.143.69', '114.114.114.114']
with open(read_filename, 'r') as f:
with open(save_filename, 'w+') as f1:
for line in f:
# tailing '\n' was ignored
print line[:-1]
f1.write('# '+ line[:-1])
tmp_res = []
is_last_fail = False
for t in xrange(loop_times):
try:
answer = my_resolver.query(line[:-1])
for i in answer:
if str(i) not in tmp_res:
print ' %s'%str(i)
tmp_res.append(str(i))
f1.write('\n')
f1.write(str(i))
is_last_fail = False
time.sleep(sleep_time)
except Exception, e:
print e
if not is_last_fail:
f1.write('\n')
f1.write('# dns query error')
is_last_fail = True
f1.write('\n\n')
print "these results were saved to %s"%save_filename
@lixingcong
Copy link
Author

lixingcong commented Jul 21, 2016

Remove '# xxx.com' lines and sort unique ip results:

sed 's/#.*//' dns_results.txt | awk 'NF' | sort -u > dns_results_sorted.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment