Python实现bgp.he.net网站接口查询
来源:互联网 发布:java架构师岗位职责 编辑:程序博客网 时间:2024/05/29 16:44
http://bgp.he.net/这个网站可以查询域名的归属或一些组织的IP段,也可以查询某个IP上的同站域名,为了方便快速查询用Python实现了一个小工具。
使用这个小工具需要先安装"PyQuery"这个Python库,至于怎么下载和安装网上已有很多介绍。
使用示例:
源码:
#!/usr/bin/python# coding=UTF-8# Code by Anleimport osimport sysimport urllib import urllib2from pyquery import PyQuerydef _usage(): print 'Usage:\r\n BGP.py [Host]|IP-IP|Host.txt <-dns>\r\n'def QueryRequest(szKeyword,iType=0): reCount = 0 QueryUrl = 'http://bgp.he.net/search?search%5Bsearch%5D=' + urllib.quote(szKeyword) + '&commit=Search' headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537'} while reCount < 3: try: req = urllib2.Request(QueryUrl,headers=headers) Resp = urllib2.urlopen(req) the_page = Resp.read() DOM = PyQuery(the_page) resList=[] if iType == 1: dns = DOM('#dns')("a") if dns: for i in range(len(dns)): resList.append(dns.eq(i).text()) if len(resList) > 0: print '[ %s : %d ]' % (szKeyword,len(resList)) for i in range(len(resList)): print resList[i] return len(resList) trs1 = DOM('#search')("tbody")("tr") trs2 = DOM('#ipinfo')("tbody")("tr") if trs1: for i in range(len(trs1)): p1 = trs1.eq(i)("td").children("a").text() if p1=='' or p1.find('AS')>=0: continue p2 = trs1.eq(i)("td").next().text() p3 = trs1.eq(i)("td").next().children("div").children("img").attr("title") resList.append('{0:22}{1}\t{2}'.format(p1,p2,p3)) elif trs2: for i in range(len(trs2)): p1 = trs2.eq(i)("td").eq(1).text() p2 = trs2.eq(i)("td").eq(2).text() resList.append('{0:22}{1}'.format(p1,p2)) else: resList = None if resList==None: print '[-] %s - Not Result.' % szKeyword return 0 print '\n[ %s : %d ]' % (szKeyword,len(resList)) for i in resList: print i return len(resList) except KeyboardInterrupt: print '\n[!] User interrupt operation.\n' sys.exit(0) except: print '[-] Connect to server failed.' reCount = reCount + 1 print ' Retrying %d\n' % reCountdef _int2ip(num): strs = '' tt = [0,0,0,0] tt[0] = (num >> 24) tt[1] = ((num << 8) >> 24) % 256 tt[2] = ((num << 16) >> 24) % 256 tt[3] = ((num << 24) >> 24) % 256 strs = '{0}.{1}.{2}.{3}'.format(tt[0],tt[1],tt[2],tt[3]) return strsdef _ip2int(ip): num = 0 ip = ip.split('.') num = int(ip[0]) * 256 ** 3 + int(ip[1]) * 256 ** 2 + int(ip[2]) * 256 + int(ip[3]) return numdef BuildRange(strHost): dwStartIP = 0 dwEndIP = 0 if strHost.find('-')>0: ips = strHost.split('-') dwStartIP = _ip2int(ips[0]) dwEndIP = _ip2int(ips[1]) if strHost.find('/')>0: hosts = strHost.split('/') mask = int(hosts[1]) dwStartIP = (_ip2int(hosts[0]) & (0xffffffff << (32-mask))) + 1 dwEndIP = dwStartIP + (0xffffffff >> mask) - 2 else: dwStartIP = dwEndIP = _ip2int(strHost) return [dwStartIP,dwEndIP] def BuildHostRange(strHost): slash=[] startIpStr="" endIpStr="" ranges=0 submask=0 realStartIP=0 realEndIP=0 if strHost.find('-')>0: slash = strHost.split('-')startIpStr=slash[0]endIpStr=slash[1] else:startIpStr=strHost try: startIpStr=socket.gethostbyname(startIpStr) if strHost.find('-')>0: realStartIP = socket.ntohl(struct.unpack('I',socket.inet_aton(startIpStr))[0]) realEndIP = socket.ntohl(struct.unpack('I',socket.inet_aton(endIpStr))[0]) else: realStartIP=realEndIP=socket.ntohl(struct.unpack('I',socket.inet_aton(startIpStr))[0]) except: return [0,0] return [realStartIP,realEndIP]def ParseResult(argKeyword,iType=0): info = [] if os.path.exists(argKeyword): try: fs = open(argKeyword,'r') for line in fs: line = line.strip() QueryRequest(line,iType) fs.close() except: print 'Open file "%s" failed!' % argKeyword elif argKeyword.find('-') > 0 or argKeyword.find('/') > 0: IpRange=BuildRange(argKeyword) for index in range(IpRange[0],IpRange[1]+1): curIP=_int2ip(index) QueryRequest(curIP,iType) else: QueryRequest(argKeyword,iType) if __name__ == '__main__': argLen = len(sys.argv) if argLen < 2: _usage() sys.exit(0) arg=sys.argv iType = 0 if argLen >= 3: if arg[2]=='-dns': iType = 1 ParseResult(arg[1],iType)
0 0
- Python实现bgp.he.net网站接口查询
- VB.NET版ssl链接(工行查询接口的实现)
- 网站安全检测接口网站健康状况查询
- ASP.NET网站查询优化
- python批量查询网站名称
- .net附件接口实现
- he
- he
- he
- [转]Python实现接口
- pagingandsortingrepository接口实现分页查询
- Flask实现成绩查询接口
- BGP
- bgp
- bgp
- BGP
- BGP
- BGP
- 基于Thinkphp很不错的一个响应式blog系统
- SSH:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 错误处理方法
- js实现动态控件提示信息模版
- Linux内核——第十五章:页高速缓存
- 不在同一个内网的本机通过SecureCrt配置,访问服务器的应用(应用设置仅允许内网之间能访问)
- Python实现bgp.he.net网站接口查询
- js判断开始时间不能小于结束时间
- 利用SimpleJdbcTemplate进行增删改查
- HihoCoder#1082 : 然而沼跃鱼早就看穿了一切
- apache htdocs 的路径
- 神器3.0时代,几乎所有的 LBS 社交应用
- Android TouchEvent事件传递机制
- 单一职责原则
- .net项目中使用log4net记录日志