pythontcpscapy

Scapy - persistent RandIP


I am trying to simulate a TCP communication between two hosts with scapy.

My problem is, that I can't save the random IP addresses scapy generates for me.

This code

src_IP = RandIP()

print(src_IP)
print(src_IP)
print(src_IP)

gives me an output like this

234.200.98.20
147.3.56.17
135.102.142.49

So every time I access src_IP it has a new value.

Is there a way to save a random IP from scapy? So I could generated 2 IPs at the beginning of my function and use them as source and destination for my TCP communication.
I could generate the IPs myself, but I thought there had to be a more elegant solution to it.

BTW. non of the packets are going to be sent, they will be written into a PCAP file. Therefor I have to create both sides of the communication.


Solution

  • The accepted answer is a hack; it is correct (as in "it works and do what was asked"), but it is not the correct way to do that in Scapy.

    The method you are looking for is ._fix(). It will work with any volatile value type.

    src_IP = RandIP()._fix()
    

    If you also need for example a random source port, you could do:

    src_port = RandShort()._fix()