473,574 Members | 2,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('ht tp://192.168.1.' + str(x))

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

url_queue = Queue.Queue(655 36)
for url in urls:
url_queue.put(u rl)

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

def test_http(url_q ueue, 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.Threa d(target=sub_th read_proc,
args=(url,resul t))
sub_thread.setD aemon(True)
sub_thread.star t()
sub_thread.join (HTTP_TIMEOUT)
if [] == result:
result_queue.pu t((url, "TIMEOUT"))
elif -1 == result[0]:
result_queue.pu t((url, "FAILED"))
else:
result_queue.pu t((url, result[0]))

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

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

web_servers = []
failures = []

while not result_queue.em pty():
url, result = result_queue.ge t(0)
if isinstance(resu lt, str):
failures.append ((result, url))
else:
web_servers.app end((result,url ))

web_servers.sor t()
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 1291
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':Queu e.Queue(),'ok': Queue.Queue(),' fail':Queue.Que ue()}
for subnet in range(256):
workers.append (.. target=test_htt p, 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':Queu e.Queue(),'ok': Queue.Queue(),' fail':Queue.Que ue()}
for subnet in range(256):
workers.append (.. target=test_htt p, 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
2076
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, with a duration of 10-40 seconds. To avoid delays, i inserted a cache for the data. So, if the script is called, it returns the last caculated...
32
3158
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
1351
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 anyone? ---------- import socket import threading import traceback
21
1612
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 available & working. It's like closing the form did not actually cancel the threads. From my earlier discussions on the newsgroup, I know how...
3
1474
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
4534
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 nothing to control Timeout Connection... and I wasn't interested to wait 25-30sec for each ip. After some search, it's look like Socket was the...
12
2305
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 of minutes) and I want the thread to be able to begin work ASAP once the new information arrives so Thread.Sleep isn't the best solution. In .NET...
2
4400
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 java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
0
1193
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 IPScanner() { } private void connect(object data)
0
7801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8044
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7808
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5299
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1055
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.