一个扫描网站内容,及端口的扫描器。

生活中的懒人

在测试网站运行状态的时候,为了免去一些重复的不必要操作,所以想着能不能用Python脚本来代劳,答案当然是肯定的,好了来说下主要思路吧!

Github

用到的Python库

requestsulib32

Run Enveronment

python 36

模块

scan_port


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# coding: utf-8
import socket
from multiprocessing.dummy import Pool as ThreadPool
def main():
global host_ip
host = input("Enter a host to scan:")
exit_port = int(input("Enter exit_port:"))
processes_num = int(input("Enter processes num:"))
host_ip = socket.gethostbyname(host)
ports = []
print ('-' * 60)
print ('Please wait, scanning host ', host_ip)
print ('-' * 60)
socket.setdefaulttimeout(0.5)
for i in range(1,exit_port):
ports.append(i)
pool = ThreadPool(processes = processes_num)
results = pool.map(scan_port,ports)
pool.close()
pool.join()
print ('port scan end')
def scan_port(port):
try:
s = socket.socket(2,1)
res = s.connect_ex((host_ip,port))
if res == 0:
print ('Port {}: OPEN'.format(port))
s.close()
except Exception as e:
print (str(e.message))
if __name__ == '__main__':
main()

scan_content


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import requests
from multiprocessing.dummy import Pool as ThreadPool
def scan_content(url):
r = requests.get(url)
print(str(r.status_code)+' '+url+'\n')
def read_file(file,url):
global content
content = []
for line in file:
tl = line.rstrip('\n')
content.append(url+tl)
del content[-0]
return content
def main():
url = input("Please enter url:")
print ('-' * 60)
try:
file = open ('..//map//func//dir.txt','r',encoding='utf-8')
#file = open ('dir.txt',encoding='utf-8')
except IOError:
print ('open file(dir.txt) fail!')
else:
if (url.strip()==''):
url = 'https://www.baidu.com'
read_file(file,url)
pool = ThreadPool(2)
pool.map(scan_content,content)
pool.close()
pool.join()
#scan_content(content)
else:
read_file(file,url)
pool = ThreadPool(2)
pool.map(scan_content,content)
pool.close()
pool.join()
#scan_content(content)
print('scan end')
if __name__ == '__main__':
main()

main()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from func import scan_content
from func import scan_ports
if __name__ == '__main__':
print ('welcome to the scanner of xu')
print ('*' * 60+'\n1.scan for content')
print ('2.scan for ports')
while(1):
temp = input('*' * 60+'\nenter the function num: ')
print ('*' * 60)
if (temp=='1'):
scan_content.main()
continue
elif (temp=='2'):
scan_ports.main()
print('ports')
continue
elif (temp=='exit'):
exit()
else:
print('Enter Error!')
continue

测试效果

哈哈,这下省事多了,还是python大法好啊!这里以百度为栗子:

ScanResultExam

Comments

2017-10-26

⬆︎TOP