Skip to content

Instantly share code, notes, and snippets.

@pixelvm
Last active October 23, 2023 02:57
Show Gist options
  • Select an option

  • Save pixelvm/e3a281f46bd6f69a49b8286109dbcb3a to your computer and use it in GitHub Desktop.

Select an option

Save pixelvm/e3a281f46bd6f69a49b8286109dbcb3a to your computer and use it in GitHub Desktop.
Scan ports with python
# -*- 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