'get only associated devices to wifi using scapy

I'm using the following code to get the mac addresses of all station devices

import sys
from scapy.all import *
devices = set()



# Let's iterate through every packet
for pkt in PcapReader("example.cap"):
    if pkt.haslayer(Dot11):
        dot11_layer = pkt.getlayer(Dot11)

        if dot11_layer.addr2 and (dot11_layer.addr2 not in devices):
            devices.add(dot11_layer.addr2)
            print(dot11_layer.addr2)

but this get's all the devices from all sorts of packets so if the device is only probing for the AP it is added to the set, if the device is trying to connect and failing due to a wrong password or mac access-list policy then it's mac is also added to the set, I want to filter the macs and only get the addresses of the successfully connected devices, how can I modify the code to do that filtering?

Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source