Last active
November 11, 2024 18:04
-
-
Save barbudor/2f860f53bcb7131f538ceeb3db4142dc to your computer and use it in GitHub Desktop.
Dead simple syslog receiver and logger in Python
This file contains hidden or 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
#! /usr/bin/env python | |
import socket | |
from datetime import datetime | |
UDP_IP = "0.0.0.0" | |
UDP_PORT = 5144 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind((UDP_IP, UDP_PORT)) | |
while True: | |
data, addr = sock.recvfrom(1600) | |
t = datetime.now() | |
print( "%s: %s" % (t.strftime("%Y-%m-%d %H:%M:%S.%f"), data.decode())) |
This file contains hidden or 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
#! /usr/bin/env python3 | |
import socket | |
from datetime import datetime | |
import sys | |
UDP_IP = "0.0.0.0" | |
UDP_PORT = 5144 | |
SYSLOG_FILE = "syslog.log" | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind((UDP_IP, UDP_PORT)) | |
with open(SYSLOG_FILE, "w", encoding='utf-8') as fsyslog: | |
print(f"now recording to {SYSLOG_FILE}") | |
while True: | |
data, addr = sock.recvfrom(1600) | |
t = datetime.now() | |
message = f"{t.strftime('%Y-%m-%d %H:%M:%S.%f')}: {data.decode()}\n" | |
sys.stdout.write(message) | |
sys.stdout.flush() | |
fsyslog.write(message) | |
fsyslog.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment