I'm trying to slice a variable containing a number of bytes, so I can use unpack with a buffer of 14 bytes. But apparently it's not working. What am I doing wrong here?
import pcapy
from struct import *
import sys
devs = pcapy.findalldevs()
cap = pcapy.open_live(devs[4], 65535, 0, 1)
while 1:
(header, payload) = cap.next()
print(sys.getsizeof(payload))
l2hdr = payload[:14]
print(sys.getsizeof(l2hdr))
l2data = unpack("!6s6sH", l2hdr)
srcmac = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (l2hdr[0]), (l2hdr[1]), (l2hdr[2]), (l2hdr[3]), (l2hdr[4]), (l2hdr[5])
dstmac = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (l2hdr[6]), (l2hdr[7]), (l2hdr[8]), (l2hdr[9]), (l2hdr[10]), (l2hdr[11])
print("Source MAC: ", srcmac, " Destination MAC: ", dstmac)
and here's the result
Traceback (most recent call last):
File "C:/Users/admin/Downloads/test1.py", line 14, in <module>
l2data = unpack("!6s6sH", l2hdr)
17
struct.error: unpack requires a buffer of 14 bytes
17
Process finished with exit code 1