-
Notifications
You must be signed in to change notification settings - Fork 0
/
portscanner.py
41 lines (32 loc) · 961 Bytes
/
portscanner.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
import socket
from IPy import IP
class PortScan():
banners = []
open_ports = []
def __init__(self, target, port_num):
self.target = target
self.port_num = port_num
def scan(self):
for port in range(1,500):
self.scan_port(port)
def check_ip(self):
try:
IP(self.target)
return(self.target)
except ValueError:
return socket.gethostbyname(self.target)
def scan_port(self,port):
try:
converted_ip = self.check_ip()
sock = socket.socket()
sock.settimeout(0.5)
sock.connect((converted_ip, port))
self.open_ports.append(port)
try:
banner = sock.recv(1024).decode().strip('\n').strip('\r')
self.banners.append(banner)
except:
self.banners.append(' ')
sock.close()
except:
pass