In the previous article I demonstrated how to add a custom function to change the formatting of the packet output in the console or do some sort of custom action with each packet. The limitation of just including a function name in the
prn argument is that you cannot pass along any arguments other than the packet itself (implicitly passed).
Using nested functions to harness the power of closure, you can bind any number of arguments to the function that is executed on each packet by Scapy. From the part 1 article, you can see how we created a function and used the function to pass the actual function (not the returned value) to the
Scapy has a
sniff function that is great for getting packets off the wire, but I recently discovered just how great this feature really is. There’s an argument to pass a function that executes with each packet sniffed. The intended purpose of this function is to control how the packet prints out in the console, allowing you to replace the default .
nsummary display with a format of your choice.
In the ScapyDoc.pdf, the
prn argument is defined as:
prn: function to apply to each packet. If something is returned, it is displayed. For instance you can use prn = lambda x: x.summary().