Lunnaya
Lunnaya

Reputation: 1

Traceroute IP with python3.8 and multiprocessing

I need to traceroute some IP addresses. I create processes (with multoprocessing module) for each address. but I get the same result. I do not know what's the problem. Maybe I'm using multiprocessing incorrectly. Or it's about the ICMPLIB.

My code:

import multiprocessing as mp
from icmplib import traceroute


def tracerouting(target, q):  #tracerouting with icmplib

    data = traceroute(target)
    q.put(data)


if __name__ == "__main__":

    q = mp.Queue()
    targetList = ['google.com', 'mail.ru']
    #creating processes for addresses
    processes = [mp.Process(target=tracerouting, args = (ip, q))for ip in targetList]


    for p in processes:
        p.start()

    for p in processes:
        p.join()

    print('Queues')

    #printind data
    for p in processes:
        data = q.get()
        print(data)

Execution result:

Queues [<Hop 1 [192.168.1.1]>, <Hop 3 [212.176.119.178]>, <Hop 4 [195.151.241.162]>, <Hop 5 [195.151.234.230]>, <Hop 6 [195.151.240.237]>, <Hop 7 [216.239.50.46]>, <Hop 8 [209.85.254.20]>, <Hop 9 [142.250.56.15]>, <Hop 10 [217.69.139.200]>] [<Hop 1 [192.168.1.1]>, <Hop 3 [212.176.119.178]>, <Hop 4 [195.151.241.162]>, <Hop 5 [195.151.234.226]>, <Hop 6 [72.14.195.208]>, <Hop 7 [108.170.250.34]>, <Hop 8 [216.239.50.46]>, <Hop 9 [209.85.254.20]>, <Hop 10 [142.250.56.15]>, <Hop 11 [217.69.139.200]>]

Upvotes: 0

Views: 612

Answers (1)

Hugo
Hugo

Reputation: 31

I also use this library in my projects. I mainly use the ping and multiping functions but normally it works the same with the traceroute function.

From what I see on the project page (https://github.com/ValentinBELYN/icmplib#traceroute), you must use a different identifier for each process in order to match the responses with the requests. By default, the PID of your program is used on each call and therefore, there may be conflicts. Since you want to parallelize several traceroute, I recommend you to assign a unique ID (between 0 and 65535) for each process.

Otherwise why use multiprocessing for this kind of task? Threads seem more suitable.

traceroute(address, id=YOUR_THREAD_ID)

Finally, to answer your other question, no, there is no shared socket between all your traceroute. There are RAW sockets (very low level). There are essential to handle IP and ICMP headers but less flexible than DGRAM or STREAM sockets. RAW sockets can pick up ICMP messages that are not intended for them and libraries like icmplib generally use an identifier and a sequence number (automatically incremented) to match responses with requests. That is why, I advise you not to use the same identifier between your traceroute :)

I think this should fix your problem.

Upvotes: 1

Related Questions