Last active
October 23, 2023 02:57
-
-
Save pixelvm/e3a281f46bd6f69a49b8286109dbcb3a to your computer and use it in GitHub Desktop.
Scan ports with 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
| # -*- coding: UTF-8 -*- | |
| import time | |
| import socket | |
| import threading | |
| import ipaddress | |
| def create_thread(ipaddr, port): | |
| return threading.Thread(target = test_port, args = (ipaddr, port)) | |
| def test_port(ipaddr, port): | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| sock.settimeout(1) | |
| result = sock.connect_ex((ipaddr, port)) | |
| if result == 0: | |
| print("{}:{} Open".format(ipaddr, port)) | |
| sock.close() | |
| thread_count = 500 | |
| ipaddr = '192.168.20.0/24' | |
| net = ipaddress.ip_network(ipaddr) | |
| for ipaddr in net.hosts(): | |
| for port in range(1, 65536): | |
| thread = create_thread(str(ipaddr), port) | |
| while True: | |
| if threading.active_count() < thread_count: | |
| thread.start() | |
| break | |
| else: | |
| time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment