Last active
June 29, 2023 10:53
-
-
Save AlgorithmAlchemy/2bd4b3e545938cb344f8eb98b250c5cc to your computer and use it in GitHub Desktop.
Port Scanner python
This file contains hidden or 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
# war 1 | |
import socket | |
def scan_port(ip,port): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.settimeout(0.5) | |
try: | |
connect = sock.connect((ip,port)) | |
print('Port :',port,' its open.') | |
sock.close() | |
except: | |
pass | |
ip = '212.32.245.163' | |
for i in range(1000): | |
print(i) | |
scan_port(ip,i) | |
# war 2 | |
import socket # for connecting | |
from colorama import init, Fore | |
# some colors | |
init() | |
GREEN = Fore.GREEN | |
RESET = Fore.RESET | |
GRAY = Fore.LIGHTBLACK_EX | |
def is_port_open(host, port): | |
""" | |
determine whether `host` has the `port` open | |
""" | |
# создает новый сокет | |
s = socket.socket() | |
try: | |
# пытается подключиться к хосту через этот порт | |
s.connect((host, port)) | |
# сделайте таймаут, если хотите немного быстрее (с меньшей точностью) | |
# s.settimeout(0.2) | |
except: | |
# не могу подключиться, порт закрыт | |
# return false | |
print("close ", port) | |
else: | |
# соединение установлено, порт открыт! | |
print("open ", port) | |
# получаем хост от пользователя | |
host = input("Enter the host: ") | |
# перебирать порты от 1 до 1024 | |
for port in range(1, 65535): | |
if is_port_open(host, port): | |
print(f"{GREEN}[+] {host}:{port} is open {RESET}") | |
else: | |
print(f"{GRAY}[!] {host}:{port} is closed {RESET}", end="\r") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment