使用python實現IP反查網域名稱

來源:互聯網
上載者:User

0x00

趁著暑假的功夫,把python初略的學習一遍,最大的感受就是這個語言是我見過的最最好用的語言。尤其是其強大的類庫以及簡單的文法。

閑來無事,今天我就記錄一下我用python寫的一個小的IP反查網域名稱的工具。

IP反查就是通過IP地址反向查詢出綁定在這個IP上的所有的網域名稱資訊(一台伺服器可以有多個虛擬機器主機)。

0x01

思路就是使用網路上的IP反查網域名稱的網站,這裡我用的是http://dns.aizhan.com/。通過爬蟲將查詢結果從網頁上提取出來,最後輸出到一個html檔案中。

使用也很方便,比如你要查詢的IP地址是10.10.10.10,那麼就提交:http://dns.aizhan.com/?q=10.10.10.10即可。

但是,查詢結果不是直接返回,而是使用Ajax動態擷取的。這時用firebug找到對應的介面就行了。


第一個url是拿到一個總的查詢結果(json),其他的url都是擷取網域名稱的title。這樣,所要做的只有兩個事兒:

第一是從json中提取出本頁所有的url

第二是根據url的列表找出每個url對應的title



上圖就是一個網域名稱對應一個title,只需將這兩個資訊作為資料元即可。

0x03

有了以上的分析基礎,就可以直接看My Code了。

#coding=utf-8'''IP反查小工具http://dns.aizhan.com/index.php?r=index/domains&ip=202.203.208.8&page=1&_=1408264478284'''import requests,json,urllib,sys,osfrom bs4 import BeautifulSoup#擷取頁面內容def getPage(ip,page):    r = requests.get("http://dns.aizhan.com/index.php?r=index/domains&ip=%s&page=%d" % (ip,page))    return r#擷取最大的頁數def getMaxPage(ip):    r = getPage(ip,1)    json_data = {}    json_data = r.json()    maxcount = json_data[u'conut']    maxpage = int(int(maxcount)/20) + 1        return maxpage#擷取網域名稱列表def getDomainsList(ip):    maxpage = getMaxPage(ip)    result = []    for x in xrange(1,maxpage+1):        r = getPage(ip,x)        result.append(r.json()[u"domains"])    return result#擷取最終結果,形式:{url  title}  並寫入檔案中def getResultWithTitle(filepath,domain_list):    f = open(filepath,"a")    res_dict = {'domain':'','title':''}    res_list = []    f.write('<html>')    for x in domain_list:        for i in xrange(0,len(x)):            title = urllib.urlopen("http://dns.aizhan.com/index.php?r=index/title&id=%d&url=%s" % (i,x[i])).read()            soup = BeautifulSoup(title)            res_dict['domain'] = x[i]            res_dict['title'] = soup.contents[0].encode('utf-8')            f.write('<a href='+str(res_dict['domain'])+'>'+str(res_dict['domain'])+'</a>\t\t'+str(res_dict['title'])+'<br/>')            res_list.append(res_dict)    f.write('</html>')    f.close()    return res_list                if __name__ == "__main__":    if len(sys.argv) < 3:        print "Usage:reverseIP targetIP Outfile"    else:        ip = str(sys.argv[1])        outfile = str(sys.argv[2])        if not str(os.path.basename(outfile)).split('.')[-1] == 'html':            print "The outfile must end with '.html' "        else:            print "The target IP is :%s" % ip            print "Starting, please wait..."            domainList = getDomainsList(ip)            getResultWithTitle(outfile,domainList)            print "Success! The path of result file is %s" % outfile            


0x04

以上代碼我已經執行通過,效果如下:



看來好好利用 python,確實可以輕易解決不少問題。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.