-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path简单的端口扫描.py
65 lines (62 loc) · 1.44 KB
/
简单的端口扫描.py
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 threading
import time
import datetime
alll=0
duankou=''
def changshi(ip,port): #端口扫描函数
socket.setdefaulttimeout(2)
so=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=ip
po=port
global alll
global duankou
try:
soo=so.connect((ip,po))
print(po,'ok')
alll+=1
duankou+=str(po)
duankou+=','
except Exception as err:
#print(po,str(err))
pass
ip=input('ip:')
stt=datetime.datetime.now()
st=time.time()
print(stt)
port1=0
port2=65535
def shuru(): #检查输入的端口是否合法
try:
port1=int(input('范围:'))
port2=int(input('{}--'.format(port1)))
return port1,port2
except ValueError as err:
print(err)
shuru()
return port1,port2
port1,port2=shuru()
if port2>port1:pass
else:port1,port2=port2,port1
print(port1,'--',port2)
threads=[]
a=input('go?:')
for i in range(port1,port2):
t=threading.Thread(target=changshi,args=(ip,i))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
et=time.time()
print('all:',alll)
print('time:',(et-st),'s')
sa=input('save?y/n:')
if sa=='y':
with open('scanjieguoOK.txt', 'a') as f: #保存扫描到的端口
f.write('all:')
f.write(str(alll))
f.write(' ')
f.write(duankou)
f.write('\n')