-
Notifications
You must be signed in to change notification settings - Fork 0
/
端口扫描
65 lines (55 loc) · 1.88 KB
/
端口扫描
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import socket
import re
from datetime import datetime
import threading
threads = []
#判断输入是ip/域名,为域名则解析成ip地址
def get_ip(server_ip):
if re.match('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$',server_ip):
ip = server_ip
elif re.match('^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$',server_ip):
ip = socket.gethostbyname(server_ip)
else:
print("您输入的地址有误,请重输入:")
return ip
#socke连接
def PortScan(ip,port):
t1=datetime.now()
try:
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res=s.connect_ex((ip,port))
#connect连接返回值:0为成功连接,其他值为连接失败
if res==0:
print("port %d is open"%(port))
except socket.gaierror:
print('Hostname could not be resolved.Exiting')
except socket.error:
print ("Could't connect to the server")
s.close()
def main():
banner = '''
_
_ __ ___ _ __| |_ ___ ___ __ _ _ __
| '_ \ / _ \| '__| __/ __|/ __/ _` | '_ \
| |_) | (_) | | | |_\__ \ (_| (_| | | | |
| .__/ \___/|_| \__|___/\___\__,_|_| |_|
|_|
'''
print(banner)
server_ip=input("请输入ip:")
ip=get_ip(server_ip)
print(ip)
t1 = datetime.now()
#使用嵌套循环,使用多线程扫描全端口
for n in range(1,74):
for port in range((n-1)*880,n*880):
t = threading.Thread(target=PortScan,args=(ip,port))
threads.append(t)
t.start()
for t in threads:
t.join()
t2 = datetime.now()
print(t2 - t1)
#启动多线程调用ip函数
if __name__=="__main__":
main()