扫描网站服务器真实IP的小脚本

 1 #!/usr/bin/env python
 2 # -*- coding: gbk -*-
 3 # -*- coding: utf_8 -*-
 4 # Date: 2015年9月11日
 5 # Author:蔚蓝行
 6 # 博客 http://www.cnblogs.com/duanv/
 7 
 8 import requests
 9 import threading
10 import Levenshtein
11 import re
12 
13 def scan(original_r,cip,ip_begin,original_match,header):
14     ip=cip+str(ip_begin)
15     try:
16         r=requests.get('http://'+ip,headers=header,timeout=1)
17     except Exception:
18         pass
19     else:
20         if(r.status_code==original_r.status_code):
21             if r.content==original_r.content:
22                 print '---everything is match!---\n'+ip+'\n--------------------------\n\n\n',
23             else:
24                 if Levenshtein.ratio(r.text,original_r.text)>0.8: 
25                     match=re.search(r"<title>(.*?)</title>",r.content)
26                     try:
27                         if match==original_match or match.group()==original_match.group():
28                             print '--matches>0.8-same title--\n'+ip+'\n--------------------------\n\n\n',
29                         else:
30                             print '--matches>0.8-diff title--\n'+ip+'\n--------------------------\n\n\n',
31                     except Exception:
32                         if match==None:
33                             #扫描网页无标题
34                             print '-matches>0.8-none title-s-\n'+ip+'\n--------------------------\n\n\n',
35                         else:
36                             #原始网页无标题
37                             print '-matches>0.8-none title-o-\n'+ip+'\n--------------------------\n\n\n',
38 
39 def loop(original_r,cip,original_match,header):
40     global ip_begin,ip_end,mutex
41     while 1:
42         mutex.acquire()
43         if ip_begin > ip_end:
44             mutex.release()
45             break
46         ip=ip_begin
47         ip_begin += 1
48         mutex.release()
49         scan(original_r,cip,ip,original_match,header)
50 
51 def start():
52     global ip_begin,ip_end,mutex
53 
54     ip_begin=1
55     ip_end=254
56     mutex=threading.Lock()
57 
58     cip='180.97.33.'
59     address='www.baidu.com'
60     
61     #cip='220.181.136.'
62     #address='www.219.me'
63     
64     header={"host":address,"Accept-Encoding":"identity","User-Agent":""}
65     r=requests.get('http://'+address,headers=header)
66     
67     original_match=re.search(r"<title>(.*?)</title>",r.content)
68     
69     threads=[]
70     for i in range(254):
71         threads.append(threading.Thread(target=loop,args=(r,cip,original_match,header)))
72     for t in threads:
73         t.start()
74 
75 if __name__ == '__main__':
76     start()

 1,由于是多线程,输出时如果用print xxx会出现因线程抢占而造成的输出乱序,改用print xxx+‘\n’,可以不用线程锁并解决这个问题

 2,扫描网段时请求头加上host:domain_name,可以解决单一IP对应多域名的问题

 3,发送请求时将UA头置空,可以防止某些网站服务器返回内容过大和不完全相同的问题,提高效率。比如请求百度,有UA头情况下返回的页面内容相当大,而且不同IP返回内容有细微差别,脚本就会进行相似度比较,耗时巨大,如果置空UA头,请求响应内容就比较小,而且不同IP返回内容相同

posted @ 2015-09-11 15:31  荆书凝  阅读(1515)  评论(0编辑  收藏  举报