(python)域名查询服务(whois)程序+检查5位以内域名到期时间邮件报警抢注域名
来源:互联网 发布:facebook服务器域名 编辑:程序博客网 时间:2024/05/03 09:20
前言:这个程序写了挺长时间,主要的加‘-d’选项+域名(只支持.com域名)查询特定域名,不带任何参数后台运行(可以设定cron)脚本检查域名到期时间,发现域名还有5天以内到期报警提示,方便抢注呵呵!
缺点:1 有些域名是没有域名信息的,比如a.com,p.com 所有就过滤掉了
2 程序执行时间不会对负载有影响,但是可能出现socket超时,我设置了socket连接三次,但是可能会出现一些未知错误break,大家发现BUG留言我再修改
#!/bin/env pythonimport sysimport stringimport socketimport optparseimport smtplibimport reimport timeimport itertoolsreload(sys)sys.setdefaultencoding('utf8') class Host(object): INTERHOST ="whois.internic.net" NICHOST = "whois.crsnic.net" INICHOST = "whois.networksolutions.com" ip_whois = [ INTERHOST, NICHOST ,INICHOST ]class Whois(object) : WHOIS_SERVER_ID = "Whois Server:" WHOIS_ORG_SERVER_ID = "Registrant Street1:Whois Server:" WHOIS_RECURSE = 0x01 WHOIS_QUICK = 0x02 def whois(self, query, hostname, flags): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fails =0 while True: try: if fails <=3: s.connect((hostname, 43)) except socket.error: fails +=1 else: break print 'Socket Connection timed out' queryBytes = None queryBytes = (query + "\r\n").encode() s.send(queryBytes) response = '' while True: d = s.recv(4096) response += d if not d: break s.close() nhost = None nhost = self.findwhois_server(response.decode(), hostname) if (nhost != None): response += self.whois(query, nhost, 0) return response.decode() def findwhois_server(self, buf, hostname): nhost = None parts_index = 1 start = buf.find(Whois.WHOIS_SERVER_ID) if (start == -1): start = buf.find(Whois.WHOIS_ORG_SERVER_ID) parts_index = 2 if (start > -1): end = buf[start:].find('\n') whois_line = buf[start:end+start] whois_parts = whois_line.split(':') nhost = whois_parts[parts_index].strip() else: for nichost in Host.ip_whois: if (buf.find(nichost) != -1): nhost = nichost break return nhost def whois_lookup(self, options, query_arg, flags): nichost = None if (options == None): options = {} options['whoishost'] = Host.NICHOST if ( not (flags & Whois.WHOIS_QUICK)): flags |= Whois.WHOIS_RECURSE return self.whois(query_arg, options['whoishost'], flags) class WhoisEntry(object): _regex = { 'domain_name': 'Domain Name:\s?(.+)', 'registrar': 'Registrar:\s?(.+)', 'whois_server': 'Whois Server:\s?(.+)', 'referral_url': 'Referral URL:\s?(.+)', 'updated_date': 'Updated Date:\s?(.+)', 'creation_date': 'Creation Date:\s?(.+)', 'expiration_date': 'Expiration Date:\s?(.+)', 'name_servers': 'Name Server:\s?(.+)', 'status': 'Status:\s?(.+)', 'emails': '[\w.-]+@[\w.-]+\.[\w]{2,4}', } def __init__(self, domain, text, regex=None): self.domain = domain self.text = text if regex is not None: self._regex = regex def __getattr__(self, attr): whois_regex = self._regex.get(attr) if whois_regex: setattr(self, attr, re.findall(whois_regex, self.text)) return getattr(self, attr) else: raise KeyError('Unknown attribute: %s' % attr) def __str__(self): return '\n'.join('%s: %s' % (attr, str(getattr(self, attr))) for attr in self.attrs()) def attrs(self): return sorted(self._regex.keys()) @staticmethod def load(domain, text): if text.strip() == 'No whois server is known for this kind of object.': pass return WhoisCom(domain, text)class WhoisCom(WhoisEntry): def __init__(self, domain, text): if 'No match for "' in text: pass else: WhoisEntry.__init__(self, domain, text)class Email(object): def __init__(self,body): mail_server = 'mail.dongwm.com' mail_server_port = 25 from_addr='whois_check@dongwm.com' to_addr ='dongwm@dongwm.com' from_header = 'From: %s\r\n' % from_addr# to_header = 'To: %s\r\n\r\n' % to_addr to_header = 'To: %s\r\n\r\n' % to_addr subject_header = 'Subject: Yuming check' email_message = '%s\n%s\n%s\n\n%s' % (from_header, to_header, subject_header, body) s = smtplib.SMTP(mail_server, mail_server_port) s.sendmail(from_addr, to_addr, email_message) s.quit()def parse_command_line(argv): usage = "usage: %prog [options] name" parser = optparse.OptionParser(add_help_option=False, usage=usage) parser.add_option("-d", "--domain", action="store_const", const=Host.NICHOST, dest="whoishost", help="Query for a specific domain name, please use: XXX.com format") parser.add_option("-h", "--help", action="help") return parser.parse_args(argv)if __name__ == "__main__": whoisclient = Whois() (options, args) = parse_command_line(sys.argv) if len(sys.argv)==3 and re.sub('^.*://', '', args[1]).split('/')[0].lower().split('.')[1] in 'com': text = whoisclient.whois_lookup(options.__dict__,args[1], 0) print WhoisEntry.load(args[1], text) elif len(sys.argv)==1: #for i in range(97,123): # print chr(i) len = 1 while (len<6): for i in list(itertools.permutations(string.lowercase,len)): if whoisclient.whois_lookup(options.__dict__,''.join(i)+'.com', 0).encode('utf8').find('Expiration Date: ') != -1: sa = whoisclient.whois_lookup(options.__dict__,''.join(i)+'.com', 0).encode('utf8').split('Expiration Date: ')[1].split(r'>>> Last')[0][:-2] wtime = time.strftime('%d-%b-%Y',time.localtime(time.time())) if wtime.lower()[3:6] == sa[3:6] and wtime[7:11] == sa[7:11] and int(sa[0:2]) - int(wtime[0:2]) <=5 and int(sa[0:2]) - int(wtime[0:2]) >=0: tt = '%s is %s' % (''.join(i)+'.com',sa) Email(tt) time.sleep(1) len +=1 else: print "Please use the '-h' or '--help' parameter"
0 0
- (python)域名查询服务(whois)程序+检查5位以内域名到期时间邮件报警抢注域名
- 查询域名是否注册(WHOIS)
- 域名抢注,域名交易平台(国内国外)
- C#域名whois查询
- 域名WHOIS查询
- whois域名查询
- 利用Whois服务查询域名注册信息
- 使用python-whois模块查询域名信息
- 域名抢注
- 域名抢注
- java实现whois域名查询
- 域名whois查询接口代码
- whois域名查询代码分享
- 我的Python爬虫(一):记录5位以内未被注册的域名
- 完美域名软件,域名扫描,域名抢注!
- python3实现域名查询和whois查询
- python3实现域名查询和whois查询
- python3实现域名查询和whois查询
- 2014-6-10任务完成情况
- Java中的"goto"实现
- c中的union的用法和作用
- 《深入浅出 Java Concurrency》——原子操作
- 【Linux】Linux下Android开发环境搭建
- (python)域名查询服务(whois)程序+检查5位以内域名到期时间邮件报警抢注域名
- 关于iOS强制横屏
- Erlang/Otp gen_server terminate
- Ubuntu13.10安装ssh
- 黑马程序员-单例模式(Singleton pattern)
- 戴维营教育-Swift语言中文教程和配套视频教程更新中
- 使用JDK的javaagent机制时候,遇到的蛋疼无比的ClassNotFoundException
- [转]为什么你应该(从现在开始就)写博客
- linux clone