Extract MAC Address from Ethernet frame

344 Views Asked by At

I am trying to extract the Destination & Source MACs from an ethernet frame using python and Linux. I used the code below to print the bytes of a frame.

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
packet = s.recvfrom(65565)
data = packet[0]
for byte in range(0, len(data)):
    print(data[byte])

How can I convert the byes corresponding to the Destination Mac (or the ones corresponding to the Source Mac) to an actual adress?

1

There are 1 best solutions below

3
On

The data you receive through the socket does typically not include what you're looking for. An ethernet frame is a data link layer protocol unit, while the data received on the socket is the payload of the transport layer, with the network layer inbetween.

More info on the layers here: https://en.wikipedia.org/wiki/Internet_protocol_suite

Generally, this can't be done: Finding the MAC address of the sender of a multicast UDP message in Python?