2011-07-15, 21:32
#1

import sys
import socket
import threading
import time
PORTSTATUS_OPEN = 1
PORTSTATUS_CLOSED = 0
class PortScan(threading.Thread):
def __init__(self, host, port):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.port_status = PORTSTATUS_CLOSED
def run(self):
# We have to use reliable data transfer (RDT) which exists in SOCK_STREAM (TCP)
# so we can be notified if connection failed.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# The socket has 10 seconds to figure out if port is open or not.
# Port is open if a connection could be established.
s.settimeout(10)
try:
t = s.connect((self.host, self.port))
except:
pass
else:
self.port_status = PORTSTATUS_OPEN
s.close()
host = "www.aftonbladet.se"
start_port = 25
end_port = 5500
threads = []
max_thread_count = 300
for port in xrange(start_port, end_port + 1):
# Never have more than 300 threads active at same time...
while threading.activeCount() > max_thread_count:
sys.stdout.write("Too many threads active. Waiting for threads to terminate... ")
sys.stdout.flush()
for t in threads:
t.join()
sys.stdout.write("[done]\n")
sys.stdout.flush()
portscan_thread = PortScan(host, port)
threads.append(portscan_thread)
try:
# thread.start() calls the run() method in PortScan instance
portscan_thread.start()
except:
# Should not happen.... If this error occurs try lowering max_thread_count
print "Unexpected thread error: start(). [port ", port, "]"
# For every 1000th port we have processed, print the port number
# in order to overview progress
if port % 1000 == 0:
print port
# Make sure all threads have been terminated
# (complete with checking port status)
for t in threads:
t.join()
# Open a file in which we will write all open ports
# Let's also make the terminal happy by printing them
output = open("test", "w")
output.write("Host: " + str(host) + "\n")
for t in threads:
if t.port_status == PORTSTATUS_OPEN:
print "Port", t.port , " is open"
output.write(str(t.port) + "\n")
output.close()

import sys
import socket
import threading
import time
PORTSTATUS_OPEN = 1
PORTSTATUS_CLOSED = 0
class PortScan(threading.Thread):
def __init__(self, host, port):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.port_status = PORTSTATUS_CLOSED
def run(self):
# We have to use reliable data transfer (RDT) which exists in SOCK_STREAM (TCP)
# so we can be notified if connection failed.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# The socket has 10 seconds to figure out if port is open or not.
# Port is open if a connection could be established.
s.settimeout(10)
try:
t = s.connect((self.host, self.port))
except:
pass
else:
self.port_status = PORTSTATUS_OPEN
s.close()
host = "www.aftonbladet.se"
start_port = 25
end_port = 5500
threads = []
max_thread_count = 300
for port in xrange(start_port, end_port + 1):
# Never have more than 300 threads active at same time...
while threading.activeCount() > max_thread_count:
sys.stdout.write("Too many threads active. Waiting for threads to terminate... ")
sys.stdout.flush()
for t in threads:
t.join()
sys.stdout.write("[done]\n")
sys.stdout.flush()
portscan_thread = PortScan(host, port)
threads.append(portscan_thread)
try:
# thread.start() calls the run() method in PortScan instance
portscan_thread.start()
except:
# Should not happen.... If this error occurs try lowering max_thread_count
print "Unexpected thread error: start(). [port ", port, "]"
# For every 1000th port we have processed, print the port number
# in order to overview progress
if port % 1000 == 0:
print port
# Make sure all threads have been terminated
# (complete with checking port status)
for t in threads:
t.join()
# Open a file in which we will write all open ports
# Let's also make the terminal happy by printing them
output = open("test", "w")
output.write("Host: " + str(host) + "\n")
for t in threads:
if t.port_status == PORTSTATUS_OPEN:
print "Port", t.port , " is open"
output.write(str(t.port) + "\n")
output.close()
Du måste vara medlem för att kunna kommentera