一段github上的一段py 使用iplist.net去反查域名,看看到底有多少个域名指向了一个IP

使用iplist.net去反查域名,看看到底有多少个域名指向了一个IP

#################################################################################
#       iplist.py - Lookup IPs -> Domain and Domain -> IPs from iplist.net      #
#       Copyrighted:  Primal Security Podcast - www.primalsecurity.net          #
#                                                                               #
#       This program is free software: you can redistribute it and/or modify    #
#       it under the terms of the GNU General Public License as published by    #
#       the Free Software Foundation, either version 3 of the License, or       #
#       (at your option) any later version.                                     #
#                                                                               #
#       This program is distributed in the hope that it will be useful,         #
#       but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#       GNU General Public License for more details.                            #
#                                                                               #
#       You should have received a copy of the GNU General Public License       #
#       along with this program.  If not, see <http://www.gnu.org/licenses/>.   #
#################################################################################


#!/usr/bin/env python

####################################
# Curent URL format:               #
# http://iplist.net/74.125.228.73/ #
####################################


import os, urllib, sys, optparse, re

# Function to check file  
def checkFile(cfile):
      if not os.path.isfile(cfile):
            print '[-] ' + cfile + ' does not exist.'
            exit(0)

      if not os.access(cfile, os.R_OK):
            print '[-] ' + cfile + ' access denied.'
            exit(0)

      print '[+] Fetching URLs from  ' +cfile


# Function to perform the lookup 
def iplook(ips):
    if iplist != None:
          iFile = open(iplist, 'r')
          for ip in iFile:
            ei = ip.split()
            i = ei[0]
            httpR = urllib.urlopen("http://iplist.net/"+i+"/")
            f = httpR.readlines()
            for line in f:
                        if "<h2" in line:
                                if "</table" in line:
                                        # Formatting line for domain
                                        htm = line.split("<")
                                        html = htm[2]
                                        dom = html.split(">")
                                        domain = str(dom[1])
                                        # Formatting line for IP
                                        i = line.split("/")
                                        ip = str(i[4])
                                else:   
                                        print "##############################"
                                        htm = line.split("<")
                                        html = htm[1]
                                        dom = html.split(">")
                                        domain = str(dom[1])
                                        i = line.split("/")
                                        print i
                                        ip = str(i[3])
                                l = '%-25s --> %20s' % (domain,ip)
                                print l


    else:
        httpR = urllib.urlopen("http://iplist.net/"+ips+"/")
        f = httpR.readlines()
        for line in f:
            if "<h2" in line:
                if "</table" in line:
                    # Formatting line for domain
                    htm = line.split("<")
                    html = htm[2]
                    dom = html.split(">")
                    domain = str(dom[1])
                    # Formatting line for IP
                    i = line.split("/")
                    ip = str(i[4])
                else:
                    htm = line.split("<")
                    html = htm[1]
                    dom = html.split(">")
                    domain = str(dom[1])
                    i = line.split("/")
                    ip = str(i[3])
                l = '%-25s --> %20s' % (domain,ip)
                print l

def main():
    parser = optparse.OptionParser(sys.argv[0] +'-r <file_with_ips> || -i <ip_addr>')
    parser.add_option('-i', dest='ip', type='string',help ='specify a target IP')
    parser.add_option('-r', dest='ips', type='string',help='specify target file with IPs')
    (options, args) = parser.parse_args()
    global iplist
    global ip
    iplist = options.ips
    ip = options.ip

    if (iplist == None) and (ip == None):
        print parser.usage
        exit(0)
    
    if iplist != None:
        checkFile(iplist)
        iplook(iplist)

    else:
        iplook(ip)


if __name__ == "__main__":
      main()

这段代码的结构十分清晰,支持两种输入 一种是-i后面加ip,另一种是-r后面加存有ip的文件。如果是文件则从文件中依次读取出ip 然后通过urllib.urlopen进行访问,从得到html文件中 找出规律 从而 得出结果。

github:  https://github.com/primalsecn/python_code/blob/master/iplist.py#L49


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值