-
-
Save lasteminista/bd0b78ea9088f3411ae2885f070ec81d to your computer and use it in GitHub Desktop.
Port Scanner Code Snippet 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from datetime import datetime | |
import threading | |
def get_target(): | |
hostname = input("Enter your target hostname (or IP address) : ") | |
target = socket.gethostbyname(hostname) | |
print(f'Scan Target > {target}') | |
return target | |
def get_port_list(): | |
print(f'Ports Range > [1 - 1024]') | |
return range(1, 1024) | |
def scan_port(target, port): | |
# Create a socket object | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
# Test connection | |
test = s.connect_ex((target, port)) | |
if test == 0: | |
print(f'Port {port} is [open]') | |
def port_scanner(): | |
try: | |
target = get_target() | |
port_list = get_port_list() | |
thread_list = list() | |
start_time = datetime.now() | |
for port in port_list: | |
scan = threading.Thread(target=scan_port, args=(target, port)) | |
thread_list.append(scan) | |
scan.daemon = True | |
scan.start() | |
for scan in thread_list: | |
scan.join() | |
except: | |
print("Something went wrong !") | |
else: | |
end_time = datetime.now() | |
print("Scanning completed in", end_time - start_time) | |
if __name__ == '__main__': | |
port_scanner() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment