-
Notifications
You must be signed in to change notification settings - Fork 54
/
nmap_py.py
52 lines (44 loc) · 1.31 KB
/
nmap_py.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
# 模拟nmap扫描端口
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__version__ = '1.1.1'
import socket
import threading
import time
import argparse
import ipaddress
import sys
openPortNum = 0
socket.setdefaulttimeout(3)
def socket_port(ip, PORT):
global openPortNum
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex((ip, PORT))
if(result == 0):
print(ip, PORT,'is open')
openPortNum += 1
s.close()
def start_scan(IP):
for port in range(0, 65535+1):
threading.Thread(target=socket_port, args=(IP, int(port))).start()
time.sleep(0.006)
def args_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-V','--version', action='version', version='%(prog)s '+ __version__)
parser.add_argument('ip',metavar="IP",nargs='+',help="IP Address")
args = parser.parse_args()
return args.ip
if __name__ == '__main__':
try:
args = args_parser()
t = time.time()
for i in args:
try:
ipaddress.ip_address(i)
start_scan(i)
print()
except ValueError as e:
print(e,'\n')
print('total open port is %s, scan used time is: %f' %(openPortNum, time.time()-t))
except KeyboardInterrupt:
sys.exit('Ctrl-c')