Using Lamda functions with scapy

Another interesting feature of the sniff function is that it has the "prn" attribute, which allows us to execute a function each time a packet is captured. It is very useful if we want to manipulate and re-inject data packets:

scapy> packetsICMP = sniff(iface="eth0",filter="ICMP", prn=lambda x:x.summary())

For example, if we want capture n packets for the TCP protocol,we can do that with the sniff method:

scapy> a = sniff(filter="TCP", count=n)

In this instruction, we are capturing 100 packets for the TCP protocol:

scapy> a = sniff(filter="TCP", count=100)

In the following example, we see how we can apply custom actions on captured packets.We define a customAction method that takes a packet as a parameter. ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.