-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_scanner.py
More file actions
59 lines (51 loc) · 1.36 KB
/
port_scanner.py
File metadata and controls
59 lines (51 loc) · 1.36 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
import sys
import socket
from IPy import IP
import pyfiglet
from printy import printy
ascii_banner = pyfiglet.figlet_format("port_scanner")
printy(ascii_banner,'n')
ports = {21, 22, 23, 25, 53, 80, 135, 443, 445, 8000, 8080}
info = "This a basic port scanner which scan very few but popular ports and also my computer is slow to scan all 65535 ports"
usage = "Usage: python3 port_scan.py <target>,<target>,<target>.."
print(info)
if len(sys.argv) > 1:
targets = sys.argv[1]
else:
targets = False
if len(sys.argv) < 1 or targets == False:
print(usage)
elif len(sys.argv) > 1 or targets != False:
print("Port Scanner is running.Sit back, relax and wait for results..")
def scan(target):
converted_ip = get_ip(target)
print("\n[-]Scanning target", str(target))
for i in ports:
scan_port(converted_ip, i)
def get_banner(sock):
return sock.recv(1024)
def get_ip(ip):
try:
IP(ip)
return(ip)
except ValueError:
return socket.gethostbyname(ip)
def scan_port(ipaddress, port):
try:
s = socket.socket()
s.settimeout(0.5)
s.connect((ipaddress, port))
try:
banner = get_banner(s)
print("[+] Open Port",str(port),":",str(banner))
except:
print("[+] Open Port",str(port))
except:
pass
if len(sys.argv) > 1:
targets = sys.argv[1]
if "," in targets:
for ip in targets.split(","):
scan(ip.strip())
else:
scan(targets)