Skip to content

Commit b2d0dd5

Browse files
committed
Port-Scanner-Changes-Made
1 parent 66e46aa commit b2d0dd5

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

Port-Scanner/PortScanner.py

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,66 @@
44

55
init(autoreset=True)
66

7-
def scanAPort():
7+
def getTarget():
88
hostname = input("Enter the hostname: ")
9-
port = int(input("Enter the port you want to scan: "))
109
target = socket.gethostbyname(hostname)
10+
return target
1111

12+
def scan(target, port):
1213
try:
1314
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1415
socket.setdefaulttimeout(1)
15-
16+
print(Fore.CYAN + f"Scanning port {port}...")
1617
if s.connect_ex((target, port)) == 0:
17-
print(Fore.BLUE + f"Port {port} is open")
18+
print(Fore.BLUE + f"Port {port} is Open")
1819
else:
19-
print(Fore.BLUE + f"Port {port} is close")
20-
21-
20+
print(Fore.BLUE + f"Port {port} is Close")
2221
s.close()
23-
2422
except socket.gaierror:
2523
print(Fore.RED + "Check your hostname!")
2624
sys.exit()
2725
except KeyboardInterrupt:
2826
sys.exit()
29-
30-
def scanCommonPorts():
31-
commonPorts = [20,21,22,23,25,53,80,110,119,123,143,161,194,443,445,464,465,497,500,512,513,514,515,520,521,540,554,563,587,631,636,691,873,993,994,995,1080,1433,1434,1512,1701,1758,1759,1789,1812,1813,1985,2049,2053,2100,2401,2809,3130,3306,4321,4444,5002,5308,5549,6000,9876,10080,11371,11720,13720,13721,31337,33434]
32-
33-
hostname = input("Enter the hostname: ")
34-
target = socket.gethostbyname(hostname)
3527

36-
open_ports = []
37-
close_ports = []
28+
def reservedPorts():
29+
target = getTarget()
30+
for port in range(1024):
31+
scan(target, port)
3832

39-
for port in commonPorts:
40-
try:
41-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
42-
socket.setdefaulttimeout(1)
33+
def scanAllPorts():
34+
target = getTarget()
35+
for port in range(65536):
36+
scan(target, port)
4337

44-
if s.connect_ex((target, port)) == 0:
45-
print(Fore.CYAN + f"Scanning port {port}...")
46-
open_ports.append(port)
47-
else:
48-
print(Fore.CYAN + f"Scanning port {port}...")
49-
close_ports.append(port)
50-
51-
s.close()
52-
53-
except socket.gaierror:
54-
print(Fore.RED + "Check your hostname!")
55-
sys.exit()
56-
except KeyboardInterrupt:
57-
sys.exit()
58-
59-
if(open_ports):
60-
print(Fore.GREEN + "These ports are Open: ")
61-
print(open_ports)
62-
if(close_ports):
63-
print(Fore.GREEN + "These ports are Close: ")
64-
print(close_ports)
38+
def scanAPort():
39+
target = getTarget()
40+
port = int(input("Enter the port you want to scan: "))
41+
scan(target, port)
42+
43+
def criticalPorts():
44+
criticalPorts = [15, 20, 21, 22, 23, 25, 49, 50, 51, 53, 67, 68, 69, 79, 80, 88, 110, 111, 119, 123, 135, 137, 138, 139, 143, 161, 389, 443, 445, 500, 520, 546, 547, 636, 993, 995, 1512, 1701, 1720, 1723, 1812, 1813, 3306, 3389, 5004, 5005, 5060, 5061, 5900, 8080]
45+
target = getTarget()
46+
for port in criticalPorts:
47+
scan(target, port)
6548

6649
while True:
6750
print()
6851
print("-" * 22, "MENU", "-" * 22)
69-
print("1. Look for a specific port in a server")
70-
print("2. Scan for some common ports in a server")
71-
print("3. Quit")
52+
print("Press 1 for Reserved Ports")
53+
print("Press 2 to scan all the ports")
54+
print("Press 3 for Critical Ports")
55+
print("Press 4 to scan a particular port")
7256
print("-" * 50)
7357

7458
choice = int(input(Fore.GREEN + "Enter the menu option: "))
7559

7660
if choice == 1:
77-
scanAPort()
61+
reservedPorts()
7862
elif choice == 2:
79-
scanCommonPorts()
63+
scanAllPorts()
8064
elif choice == 3:
81-
break
65+
criticalPorts()
66+
elif choice == 4:
67+
scanAPort()
8268
else:
8369
print("\nInvalid Choice. Enter a valid option!")

Port-Scanner/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Download this folder in your system.
66
* `cd` into the folder.
7+
* Install dependencies by running `pip install -r requirements.txt`
78
* Run `python3 PortScanner.py`
89

910
## Output

Port-Scanner/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama==0.4.3

0 commit comments

Comments
 (0)