Created
July 26, 2020 16:11
-
-
Save atdt/0a79c38b153cd8bada01496cb900d354 to your computer and use it in GitHub Desktop.
Port-knocking client (e.g. for knockd) in Python
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
#!/usr/bin/env python3 | |
# Port-knocking client | |
# Usage: knock HOST PORT [PORT...] | |
import socket | |
import sys | |
try: | |
host = sys.argv[1] | |
ports = [int(p) for p in sys.argv[2:]] | |
ports[0] | |
except (ValueError, IndexError): | |
print('Usage: %s HOST PORT [PORT...]' % sys.argv[0], file=sys.stderr) | |
sys.exit(1) | |
TIMEOUT_SECS = 0.2 | |
# Get IP address and address family | |
family, _, _, _, (ip, _) = socket.getaddrinfo( | |
host, port=None, flags=socket.AI_ADDRCONFIG)[0] | |
for port in ports: | |
s = socket.socket(family, socket.SOCK_STREAM) | |
s.settimeout(TIMEOUT_SECS) | |
s.connect_ex((ip, port)) | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment