使用python爬取互聯網設備信息

擴大搜索範圍html

DNS服務器搭建好之後,須要尋找可修改的路由器目標,目標不能太多也不能太少,就從我路由器c段入手吧,65536個IP地址,使用nmap掃描,將全部開放80端口的主機找出來,而後分析結果:python

因爲某些主機不響應ping包,因此加上-Pn選項,防止漏報.--host-timeout指定鏈接超時,不然nmap會將速度減到很慢,浪費大量時間.通過一段時間後,結果出來了web

能夠看到輸出的結果裏包含有timeout的項目,須要將這些項目去掉,利用Notepad++的正則表達式功能刪除這些不須要的項目.正則表達式

點擊所有替換,便可去掉超時的部分.9817個匹配項被替換:瀏覽器

剩下的結果爲須要的部分,能夠看到每一個IP都會出現兩次,須要單獨將他們提取出來,手工剔除效率很是低,浪費時間.爲了提升效率,寫了個提取工具用來自動化這個過程.服務器

腳本代碼以下:app

@echo off setlocal EnableDelayedExpansion color 0a set num = 1 , str = 0 ,total = 0
echo. set /p p=input file name : for /f "eol=# tokens=2" %%i in ( %p% ) do ( set /a str = num%%2 set /a num = num + 1
if !str! equ 0 (echo %%i >> %p:~0,-4%_ok.txt set /a total = total + 1) else ( echo %%i ) ) echo. echo total !total! IP echo any key to close ... pause > nul

通過批處理,開放80端口的主機ip被單獨取出來了.共有1749個開放80端口的主機.ide

它們都是什麼設備?得須要一個一個在瀏覽器中查看,但是這樣太費時間了,並且有時候只須要看網頁的title信息便可大概知道設備的類型是什麼樣的.基於此,我用python寫了一個爬蟲,來獲取這些IP地址的title信息,這樣能夠大幅減小工做量:工具

下面是爬蟲代碼單線程版本:url

 1 # !/usr/bin/python
 2 # -*- coding:utf-8 -*-
 3 #code by skq9@qq.com
 4 
 5 import requests,sys,time,datetime
 6 from lxml.html import fromstring
 7 
 8 data = {
 9 
10     
11     'User-Agent': 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)',
12     
13 }
14 
15 reload(sys)
16 sys.setdefaultencoding("utf-8")
17 
18 try:
19     sys.argv[1]
20 except:
21     print "Usage: get_title.py [filename]"
22     sys.exit()
23 
24 ip_file = sys.argv[1]
25 num = len(open(ip_file,'r').readlines())
26 count = 0
27 starttime = datetime.datetime.now()
28 
29 with open (ip_file,'r') as f :
30     for line in f.readlines():
31         ip = line.strip()
32         count += 1
33         print '[%d/%d] ' % (count,num) + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' -->' , ip + ' -->' ,
34         try:
35 
36             url = 'http://' + ip + '/'
37             r = requests.get(url=url,headers=data,timeout=(4,6))
38             r.encoding = r.apparent_encoding
39             tree = fromstring(r.text)
40             title = tree.findtext('.//title')
41             print '\'' + title + '\'' ,
42             
43             with open ('./title_info.txt','a') as f :
44                 f.write(url)
45                 f.write('\t')
46                 if title == '' :
47                     x = r.text.find('/doc/page/login')
48                     if x != -1 :
49                         f.write('-- May be HIKVISON --')
50                     else :
51                         f.write('-- Null string --')
52                 else:
53                     f.write(title.strip())
54                 f.write('\n')
55                 print 'ok!'
56 
57         except requests.exceptions.ConnectTimeout as e:
58             print 'ConnectTimeout'
59             info = url + '\tERROR:' + 'ConnectTimeout' + '\n'
60             with open ('./title_info.txt','a') as f :
61                 f.write(info)
62         except requests.exceptions.ConnectionError as e:
63             print 'ConnectionError'
64             info = url + '\tERROR:' + 'ConnectionError' + '\n'
65             with open ('./title_info.txt','a') as f :
66                 f.write(info)
67         except requests.exceptions.ReadTimeout as e:
68             print 'ReadTimeout'
69             info = url + '\tERROR:' + 'ReadTimeout' + '\n'
70             with open ('./title_info.txt','a') as f :
71                 f.write(info)
72         except Exception as e:
73             print 'Error:%s' % e
74             info = url + '\tERROR:' + str(e) + '\n'
75             with open ('./title_info.txt','a') as f :
76                 f.write(info)
77                 
78         finally :
79             pass
80 
81 endtime = datetime.datetime.now()
82 
83 print '--> Done at' , time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) , 'in' , (endtime - starttime).seconds , 'seconds'

運行結果以下:

結果顯示,返回403錯誤的ip最多,多是tp-link系的路由器,沒有開放外網web管理權限,其次是"互聯世界 物聯將來"爲標題的路由器.後期手工抽查檢查結果代表,這款路由器出廠默認密碼爲admin,並且絕大部分都保留了出廠默認.

接下來能夠針對這款路由器提取一些信息.

相關文章
相關標籤/搜索