473,738 Members | 5,934 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 1298
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
2086
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 data-structure and then the function is called again and the new data is stored in the cache. (There is no...
32
3198
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
1363
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
1635
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 people already feel about Thread.Abort(), so I was wondering what I need to do to "clean up" when...
3
1485
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
4567
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 solution with
12
2323
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 1.1 I used .Suspend and .Resume to handle these pauses but in .NET 2.0 these methods are no longer...
2
4406
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
1206
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
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.