Last active
June 5, 2021 13:51
-
-
Save cdemers/105c36250da790e74e3917b014369c80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python code sample to listen to TCP multicast traffic on | |
# port 67 (?), which should be traffic destined to a DHCP server | |
# that will listen to multicast on that port. | |
# Tested on OSX. | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
# Next line might or might not work, depending on your platform. | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) | |
s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 20) | |
s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1) | |
dhcp_server_port=67 # DHCP Traffic trying to reach a server (not sure actually...) | |
addr="0.0.0.0" # From all Interfaces/IPs | |
buf_size=1024 | |
s.bind(('', dhcp_server_port)) | |
intf = socket.gethostbyname(socket.gethostname()) | |
s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf)) | |
# "IP_ADD_MEMBERSHIP" is an invalid argument on (OSX at least). | |
#s.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(addr) + socket.inet_aton(intf)) | |
data, sender_addr = s.recvfrom(buf_size) | |
# "IP_DROP_MEMBERSHIP" is also an invalid argument on (OSX at least). | |
#s.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(addr) + socket.inet_aton('0.0.0.0')) | |
s.close() | |
print ":".join("{:02x}".format(ord(c)) for c in data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment