그래서 웹을 탐색 할 때 패킷 스니퍼에 대한 많은 정보가 있습니다. 그러나 모든 코드 또는 라이브러리는 python2에서만 사용되는 것으로 보입니다. 테스트 목적으로 python3에서 간단한 패킷 스니퍼를 만들려고합니다.python3의 패킷 스니퍼
http://www.binarytides.com/python-packet-sniffer-code-linux/에서 코드를 가져 와서 python3으로 변환하려고했습니다. 그러나 python2 및 python3이 struct.unpack 함수를 처리하는 방식에 문제가 있습니다.
다음은 이더넷 헤더를 잡고 MAC 주소를 출력하는 코드 조각 (python3 용으로 약간 수정 됨)입니다. 인쇄 문을 삽입
def eth_addr (a) :
a = str(a) # added because TypeError occurs with ord() without it
b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
return b
#create a AF_PACKET type raw socket (thats basically packet level)
#define ETH_P_ALL 0x0003 /* Every packet (be careful!!!) */
try:
s = socket.socket(socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
except socket.error as msg:
msg = list(msg)
print('Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
# receive a packet
while True:
packet = s.recvfrom(65565)
#packet string from tuple
packet = packet[0]
#parse ethernet header
eth_length = 14
eth_header = packet[:eth_length]
eth = unpack('!6s6sH' , eth_header)
eth_protocol = socket.ntohs(eth[2])
print('Destination MAC : ' + eth_addr(packet[0:6]) + ' Source MAC : ' + eth_addr(packet[6:12]) + ' Protocol : ' + str(eth_protocol))
어떻게 파이썬 3에서 MAC 주소를 올바르게 포맷 할 수 있습니까?
감사
감사합니다. 나는이 문제를 직접 알아 냈습니다. 그러나 이것이 왜 그런지에 대한 설명을 찾는 것이 도움이됩니다. – zoozoc