2018-12-28 08:49:56 +08:00
|
|
|
import socket
|
2019-01-11 10:09:47 +08:00
|
|
|
import threading
|
2018-12-28 08:49:56 +08:00
|
|
|
|
|
|
|
|
2019-01-01 10:38:32 +08:00
|
|
|
class PortScan:
|
2019-01-11 10:09:47 +08:00
|
|
|
|
2018-12-28 08:49:56 +08:00
|
|
|
def __init__(self, host, ports):
|
2019-02-04 21:18:00 +08:00
|
|
|
self.threads = 25
|
2018-12-28 08:49:56 +08:00
|
|
|
self.host = host
|
|
|
|
self.ports = ports
|
|
|
|
self.lock = threading.BoundedSemaphore(value=self.threads)
|
|
|
|
|
|
|
|
def port_scanner(self, host, ports):
|
|
|
|
openports = []
|
|
|
|
self.lock.acquire()
|
|
|
|
for port in ports:
|
|
|
|
try:
|
2019-01-01 09:00:10 +08:00
|
|
|
connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
connect.settimeout(2)
|
|
|
|
result = connect.connect_ex((host, int(port)))
|
2018-12-28 08:49:56 +08:00
|
|
|
if result == 0:
|
|
|
|
openports.append(port)
|
2019-01-01 09:00:10 +08:00
|
|
|
connect.close()
|
2018-12-28 08:49:56 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
pass
|
|
|
|
self.lock.release()
|
|
|
|
return openports
|
|
|
|
|
|
|
|
def process(self):
|
2019-01-01 09:00:10 +08:00
|
|
|
ports = self.port_scanner(self.host, self.ports)
|
2018-12-28 08:49:56 +08:00
|
|
|
return ports
|