Filtering UDP packets

In the following example, we see how we define a function that will be executed every time a packet of type UDP is obtained when making a DNS request:

scapy> a = sniff(filter="UDP and port 53",count=100,prn=count_dns_request)

This function can be defined from the command line in this way. First we define a global variable called DNS_QUERIES, and when scapy finds a packet with the UDP protocol and port 53, it will call this function to increment this variable, which indicates there has been a DNS request in the communications:

>>> DNS_QUERIES=0>>> def count_dns_request(package):>>>    global DNS_QUERIES>>>    if DNSQR in package:>>>        DNS_QUERIES +=1

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.