Search code examples
pythonscapypacket

How to send a packet over and over until response (Scapy)


I've built an ARP packet in Scapy (using Python), and I'm trying send this packet over and over, nonstop, until receiving and answer from the destination, saving the response and breaking out of the loop. I tried to google it with no success. Therefore, I'm trying my luck over here and will be glad to receive an idea how to do so in an efficient way. Thanks!


Solution

  • Scapy's sr1 function sends a packet, and waits for its answer for at most timeout seconds. If the answer is received, sr1 returns the received packet if any or None otherwise. So you can basically do what you want using this loop:

    from scapy.all import ARP, sr1
    
    loop = True
    request = ARP(pdst="10.0.0.1")
    while loop:
       response = sr1(request, timeout=5)
       if response is None:
          print('no response after 5 seconds. retrying ...')
       else:
          loop = False
    print('I got an answer:')
    response.show()