473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Almost Have Threads Working for HTTP Scan

This is almost working. I've read up on queues and threads and I've
learned a lot, still not enough to fully understand what I'm doing
though, but I'm getting there. Much of this script was copied straight
from this example:

http://aspn.activestate.com/ASPN/Coo.../Recipe/284631

My goal is to scan a big private network (65,000 hosts) for HTTP
servers. Make a list of IPs that are running the servers and a list of
those that are not. I need to use threads to speed up the process. I can
do it sequentially, but it takes 2 days!!!

Everything in the script works except that it just hangs and never
produces the two results lists. Could a threading guru please offer some
advice?

Thanks... Bart

import urllib
import socket
import time
import Queue
import threading

######################
# Network Section #
######################

urls = []
x = 0
while x < 255:
x = x + 1
urls.append('http://192.168.1.' + str(x))

######################
# Queue Section #
######################

url_queue = Queue.Queue(65536)
for url in urls:
url_queue.put(url)

######################
# Thread Section #
######################

def test_http(url_queue, result_queue):

def sub_thread_proc(url, result):
try:
data = urllib.urlopen(url).read()
except Exception:
result.append(-1)
else:
result.append(1)

while 1:
try:
url = url_queue.get()
size = url_queue.qsize()
print size
except Queue.Empty:
return
print "Finished"
result = []
sub_thread = threading.Thread(target=sub_thread_proc,
args=(url,result))
sub_thread.setDaemon(True)
sub_thread.start()
sub_thread.join(HTTP_TIMEOUT)
if [] == result:
result_queue.put((url, "TIMEOUT"))
elif -1 == result[0]:
result_queue.put((url, "FAILED"))
else:
result_queue.put((url, result[0]))

HTTP_TIMEOUT = 20
workers = []
result_queue = Queue.Queue()

for thread_num in range(0, 64):
workers.append(threading.Thread(target=test_http, args=(url_queue,
result_queue)))
workers[-1].start()
for w in workers:
w.join()

web_servers = []
failures = []

while not result_queue.empty():
url, result = result_queue.get(0)
if isinstance(result, str):
failures.append((result, url))
else:
web_servers.append((result,url))

web_servers.sort()
failures.sort()

for result, url in web_servers:
print "%7d %s" % (result, url)
for result, url in failures:
print"%7s %s" % (result, url)

#############
# END #
#############
Jul 18 '05 #1
3 1287
Some observations:

Since every worker thread is creating another thread for each host you will
end up with 65,535 threads all active, that seem overkill to me.

On closer inspection it's going to be massively skewed towards thread 1, since
it could simply empty the entire url_queue before the others get started.

I presume the network section isn't finished since it's only actually scanning
255 addresses.

Wouldn't it be enough to just try and connect to ports 80,8080, (etc.), using
just a socket?

Why not use seperate queues for failures and successes (not sure what the
failures gives you anyway?)

As for it hanging, I'm guessing most of the threads are sitting on
url = url_queue.get()
the exception will never happen on a blocking get().

How about a simpler approach of creating 256 threads (one per subnet) each of
which scans its own subnet sequentially.

def test_http (subnet):
for i in range(256):
ip='192.168.%d.%d'%(subnet,i)
x = probe (ip) ; returns one of 'timeout','ok','fail'
Qs[x].put(ip)

Qs = {'timeout':Queue.Queue(),'ok':Queue.Queue(),'fail' :Queue.Queue()}
for subnet in range(256):
workers.append (.. target=test_http, args=(subnet,) )
Jul 18 '05 #2
Eddie Corns wrote:
Some observations:

Since every worker thread is creating another thread for each host you will
end up with 65,535 threads all active, that seem overkill to me.

On closer inspection it's going to be massively skewed towards thread 1, since
it could simply empty the entire url_queue before the others get started.

I presume the network section isn't finished since it's only actually scanning
255 addresses.
Yes, I'm only testing one subnet as it's quicker. It takes about an hour
to do 65,536 urls.

Wouldn't it be enough to just try and connect to ports 80,8080, (etc.), using
just a socket?
I guess it would, but isn't that the same as urllib trying to read a url???

Why not use seperate queues for failures and successes (not sure what the
failures gives you anyway?)
This is a good point, I could probably just have a queue for urls that
urllib can read.
How about a simpler approach of creating 256 threads (one per subnet) each of
which scans its own subnet sequentially.

def test_http (subnet):
for i in range(256):
ip='192.168.%d.%d'%(subnet,i)
x = probe (ip) ; returns one of 'timeout','ok','fail'
Qs[x].put(ip)

Qs = {'timeout':Queue.Queue(),'ok':Queue.Queue(),'fail' :Queue.Queue()}
for subnet in range(256):
workers.append (.. target=test_http, args=(subnet,) )


I like the idea of having one thred per subnet, but I don't really
understand your example. Sorry, I'm having trouble with threads. Can't
get my head around them just yet. I'll probably stick with my earlier
script (I can use netstat to watch it make the syn requests) so I know
that it's working. Just can't figure out how to make it write out the
results.

Thanks for the tips,

Bart
Jul 18 '05 #3

Sometimes doing it yourself is nice, but I'd suggest you use nmap which
is a very powerful port scanner. It is designed to do such jobs very, very
fast. All you have to do then is parse it's output.

get it from http://www.insecure.org

My goal is to scan a big private network (65,000 hosts) for HTTP
servers. Make a list of IPs that are running the servers and a list of
those that are not. I need to use threads to speed up the process. I can
do it sequentially, but it takes 2 days!!!

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ralph Sluiters | last post by:
Hi, i've got a small problem with my python-script. It is a cgi-script, which is called regulary (e.g. every 5 minutes) and returns a xml-data-structure. This script calls a very slow function,...
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
14
by: ed | last post by:
this script should create individual threads to scan a range of IP addresses, but it doesnt, it simple ... does nothing. it doesnt hang over anything, the thread is not being executed, any ideas...
21
by: Doug Thews | last post by:
I was noticing that when I run a multi-threaded app from within the IDE and then I close the main form (which spun off all of the threads), that the Stop/Pause VCR buttons in the debugger are still...
3
by: EAI | last post by:
Hello All, How to abort or make sure the child threads are aborted before aborting the parent thread? Thanks
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
12
by: Dave | last post by:
Hi all, I have an application which has some worker threads which often have to stop and wait for some further information from other threads. These pauses will often take a long time (a couple...
2
oll3i
by: oll3i | last post by:
package zad31; import java.io.FileDescriptor; import java.io.FileInputStream; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import...
0
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.