Skip to content

Instantly share code, notes, and snippets.

@NiceRath
Created September 4, 2024 10:49
Show Gist options
  • Save NiceRath/91174de30382cd125b18e9a44e03e72e to your computer and use it in GitHub Desktop.
Save NiceRath/91174de30382cd125b18e9a44e03e72e to your computer and use it in GitHub Desktop.
Python3 Port Check Script
#!/usr/bin/env python3
from sys import argv as sys_argv
from socket import socket, AF_INET, AF_INET6, SOCK_STREAM
if len(sys_argv) < 3:
raise ValueError("""
You need to provide two arguments:
1 > Target IP
2 > Target port (only TCP)
Example: 'python3 port_check.py 1.1.1.1 53'
""")
TARGET_IP = sys_argv[1]
TARGET_PORT = sys_argv[2]
PORT_TIMEOUT_SEC = 2
ip_proto = AF_INET if TARGET_IP.find(':') == -1 else AF_INET6
with socket(ip_proto, SOCK_STREAM) as s:
s.settimeout(PORT_TIMEOUT_SEC)
if s.connect_ex((TARGET_IP, int(TARGET_PORT))) == 0:
print('OK')
else:
print('FAILED:', ERROR_RESULT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment