forked from brthor/docker-layer2-icc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethListen.py
54 lines (40 loc) · 1.25 KB
/
ethListen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Based off: https://gist.github.com/cslarsen/11339448
"""
from socket import *
import time
ETH_P_ALL = 3
def printMacAddr():
from uuid import getnode as get_mac
mac = get_mac()
addrStr = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
# addrStr piped to ethSender, important it's printed first
print(addrStr)
print("^ Mac Address ^")
def printPacket(packet, now, message):
packetMessage = packet[14:].decode('ascii', 'ignore')
if packetMessage == "hello":
print ("Got message from sender.")
print(message, "Len:", len(packet), "bytes time:", now, "message:", packetMessage)
def listeneth(interface = "eth0"):
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
# From the docs: "For raw packet
# sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"
s.bind((interface, 0))
s.setblocking(0)
while True:
now = time.time()
try:
packet = s.recv(128)
except Exception as e:
if 'Resource temporarily unavailable' not in str(e):
print(e)
pass
else:
printPacket(packet, now, "Received:")
time.sleep(0.001001)
if __name__ == "__main__":
# MacAddr piped to ethSender, important it's printed first
printMacAddr()
print("Listening for packets")
listeneth()