個人想查看下哪些短的網域名稱還沒有被用來提供網路服務, 自己可從中選擇一些有趣的使用.
首先使用字典產生演算法, 產生一個關鍵字列表檔案. 如下
#!/usr/bin/env python#coding=utf-8"""filename: make_key.pyauthor: zz_ddate: 2013-05-25"""import sysimport getoptlen_ch_range = 0def mk_key(plen, cnt, pstr): if cnt < plen: for i in range(len_ch_range): mk_key(plen, cnt+1, pstr+ch_range[i]) else: for i in range(len_ch_range): print pstr+ch_range[i]def help(): print '%s -n len [-h]' % sys.argv[0]if __name__ == "__main__": plen = 0 try: opts, args = getopt.getopt(sys.argv[1:], "n:h") except getopt.GetoptError as err: print str(err) help() sys.exit(2) for o, a in opts: if o == '-n': if int(a) > 0: plen = int(a) elif o == '-h': help() sys.exit(0) if plen <= 0: help() sys.exit(0) ch_range = 'abcdefghijklmnopqrstuvwxyz0123456789' len_ch_range = len(ch_range) mk_key(plen, 1, '')
取得關鍵字 key, 分別檢查 www.key.com 和 www.key.net 是否存在, 用 domain_key 表示.
檢查網域名稱的步驟是:
1. 擷取網域名稱不存在時電信或網通的劫持IP;
2.如果 gethostname(domain_key) 出錯或者返回劫持的 IP, 則此 domain_key 未提供服務;
代碼如下:
#!/usr/bin/env python#coding=utf-8"""filename: check_domain.pyauthor: zz_ddate: 2013-05-25"""import sysimport timeimport threadingimport getoptimport socketbsema = None# 通過一個不存在的網域名稱, 來擷取網域名稱不存在時被 isp 劫持到的 ipno_exist_domain = 'www.djflskfjiosfhossdfdsfsfsdfdi.com'fake_ip = None def check_domain(key): ''' check if a domain is exist ''' try: domain = 'www.%s.com' % key ip = socket.gethostbyname(domain) if ip == fake_ip: print domain except socket.gaierror: print domain except socket.timeout: print domain except socket.error: print domain try: domain = 'www.%s.net' % key ip = socket.gethostbyname(domain) if ip == fake_ip: print domain except socket.gaierror: print domain except socket.timeout: print domain except socket.error: print domain bsema.release()def help(): print '%s -f keyfile [-t threads_num] [-h]' % sys.argv[0]if __name__ == "__main__": file_keys = None thread_num = 5 try: opts, args = getopt.getopt(sys.argv[1:], "f:t:h") except getopt.GetoptError as err: print str(err) help() sys.exit(2) for o, a in opts: if o == '-f': file_keys = a elif o == '-t' and int(a) > 0: thread_num = int(a) elif o == '-h': help() sys.exit(0) if not file_keys: help() sys.exit(-2) try: fake_ip = socket.gethostbyname(no_exist_domain) except socket.error: fake_ip = None cnt = 0 socket.setdefaulttimeout(10) bsema = threading.BoundedSemaphore(thread_num) fs = file(file_keys, 'r') key = fs.readline() while key: key = key.strip() if key: bsema.acquire() th = threading.Thread(target=check_domain, args=(key,)) th.start() #check_domain(key) cnt += 1 key = fs.readline() if cnt >= 10: cnt = 0 fake_ip = socket.gethostbyname(no_exist_domain)
手動檢查了幾個輸出的結果, 並無異常. 還是有好一些有趣的網域名稱沒有被註冊的.