Question liée au multiprocessing
Bonjour,
Je tente de faire un programme ayant pour but de scan une ip sur une tranche de port indiqué . Le but étant de faire en sorte que le scan utilise plusieurs processus (dans un but de comprehension du principe).
Donc voici la "fonction" dans le processus :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| def proc1() :
name = multiprocessing.current_process().name
for i in range(1,10):
print "Creating workerThread: %d"%i
worker = WorkerThread(queue, i)
worker.setDaemon(True)
worker.start()
threads.append(worker)
print "WorkerThread %d Created"%i
for j in range (1,1000) :
queue.put(j)
queue.join()
for item in threads :
item.join()
print "Scan Done! Proc1" |
Je termine le programe avec :
Code:
1 2 3 4 5 6 7
| queue = Queue.Queue()
threads = []
proc_1 = multiprocessing.Process(name='proc1', target = proc1)
proc_2 = multiprocessing.Process(name='proc2', target = proc2)
proc_1.start()
proc_2.start()
print "Terminer ici !" |
Le second proc ne possède rien du tout je vais simplement link le programe que j'ai écris pour le moment en entier :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #!usr/bin/env python
# -*- coding:utf-8 -*-
import threading
import Queue
from scapy.all import *
import multiprocessing
import time
class WorkerThread(threading.Thread) :
def __init__(self,Queue, tid) :
threading.Thread.__init__(self)
self.queue = queue
self.tid = tid
print "Worker %d Reporting for Service Sir!"%self.tid
def run(self):
total_ports = 0
while True:
port = 0
try :
port = self.queue.get(timeout=1)
except Queue.Empty :
print "Worker %d exiting.Scanned %d ports .."%(self.tid, total_ports)
return #Probleme de fin de boucle ici
ip = sys.argv[1]
response = sr1(IP(dst=ip)/TCP(dport=port, flags="S"), verbose=False, timeout=.2)
if response :
if response[TCP].flags == 18 :
print "ThreadId %d : Received Port number %d Status : OPEN"%(self.tid, port)
self.queue.task_done()
total_ports +=1
def proc1() :
name = multiprocessing.current_process().name
for i in range(1,10):
print "Creating workerThread: %d"%i
worker = WorkerThread(queue, i)
worker.setDaemon(True)
worker.start()
threads.append(worker)
print "WorkerThread %d Created"%i
for j in range (1,1000) :
queue.put(j)
queue.join()
for item in threads :
item.join()
print "Scan Done! Proc1"
def proc2() :
name = multiprocessing.current_process().name
print name, "Starting processeur 2"
queue = Queue.Queue()
threads = []
proc_1 = multiprocessing.Process(name='proc1', target = proc1)
proc_2 = multiprocessing.Process(name='proc2', target = proc2)
proc_1.start()
proc_2.start()
print "Terminer ici !" |
Donc le code a l'air de marcher correctement sauf que le premier processus ne se termine jamais je ne sais pas vraiment comment faire pour faire en sorte de sortir de la boucle while puisque si je demande la fin manuellement ca m'indique un message d'erreur (normal).
J'ai aussi un autre soucis lors de la reception du message d'indication du nombre de port scan par chaque thread ici :
Code:
print "Worker %d exiting.Scanned %d ports .."%(self.tid, total_ports)
Ca m'indique simplement le thread qui m'a trouvé un port ouvert comme si dessous :
Code:
1 2 3 4 5 6 7 8 9
| Worker 5 exiting.Scanned 0 ports ..
Worker 7 exiting.Scanned 0 ports ..
Worker 3 exiting.Scanned 0 ports ..
Worker 9 exiting.Scanned 0 ports ..
Worker 1 exiting.Scanned 0 ports ..
Worker 4 exiting.Scanned 0 ports ..
Worker 2 exiting.Scanned 1 ports ..
Worker 8 exiting.Scanned 0 ports ..
Worker 6 exiting.Scanned 0 ports .. |
Alors qu'en théorie ca devrait simplement indiqué le nombre de port scanné par exemple :
Code:
1 2 3 4 5 6 7 8 9 10
| Worker 8 exiting. Scanned 111 ports ..
Worker 3 exiting. Scanned 110 ports ..
Worker 7 exiting. Scanned 110 ports ..
Worker 6 exiting. Scanned 111 ports ..
Worker 1 exiting. Scanned 111 ports ..
Worker 5 exiting. Scanned 112 ports ..
Worker 4 exiting. Scanned 111 ports ..
Worker 9 exiting. Scanned 112 ports ..
Worker 2 exiting. Scanned 111 ports ..
Scan Done! |
Enfin je me demande lorsque j'aurais trouver la solution à tout ces problèmes si lorsque je vais utiliser la même fonction dans un second processus (avec une liste de port différente) est-ce je risque d'avoir des problèmes.
Merci beaucoup pour votre patience pour les courageux ayant lu ce long post ainsi que pour votre aide par avance.