-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffbbaef
commit 11dbf7d
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python3.9 | ||
|
||
""" | ||
Command line utility tool for listening to udp traffic and | ||
outputting to stdout. | ||
""" | ||
|
||
import sys | ||
import socket | ||
import struct | ||
import argparse | ||
from base64 import b64encode | ||
|
||
|
||
def process_multicast(args: argparse.Namespace): | ||
"""Listen in on multicast traffic and output to stdout | ||
Args: | ||
args (argparse.Namespace): Command-line arguments | ||
""" | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, args.TTL) | ||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, int(args.loopback)) | ||
sock.setsockopt( | ||
socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(args.interface) | ||
) | ||
sock.setsockopt( | ||
socket.IPPROTO_IP, | ||
socket.IP_ADD_MEMBERSHIP, | ||
socket.inet_aton(args.group) + socket.inet_aton(args.interface), | ||
) | ||
sock.bind((args.group, args.port)) | ||
sock.setblocking(True) | ||
|
||
try: | ||
while True: | ||
data = sock.recvmsg(65535)[0] | ||
sys.stdout.write( | ||
(b64encode(data).decode() + "\n") if args.encode else data.decode() | ||
) | ||
sys.stdout.flush() | ||
finally: | ||
sock.setsockopt( | ||
socket.IPPROTO_IP, | ||
socket.IP_DROP_MEMBERSHIP, | ||
socket.inet_aton(args.group) + socket.inet_aton(args.interface), | ||
) | ||
sock.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="UDP listener") | ||
parser.add_argument( | ||
"--encode", | ||
action="store_true", | ||
default=False, | ||
help="base64 encode each packet content", | ||
) | ||
|
||
sub_commands = parser.add_subparsers() | ||
|
||
multicast_parser = sub_commands.add_parser("multicast") | ||
multicast_parser.add_argument("group", type=str) | ||
multicast_parser.add_argument("port", type=int) | ||
multicast_parser.add_argument("--interface", type=str, default="0.0.0.0") | ||
multicast_parser.add_argument("--loopback", type=bool, default=False) | ||
multicast_parser.add_argument( | ||
"--TTL", | ||
type=int, | ||
default=1, | ||
) | ||
multicast_parser.set_defaults(func=process_multicast) | ||
|
||
args = parser.parse_args() | ||
args.func(args) |