commit 37d3aa45fd339449cdd8f547f82914dcba57e0db Author: 12Funday Date: Thu Jan 25 11:47:44 2024 +0700 syslog with python diff --git a/main.py b/main.py new file mode 100644 index 0000000..d48b8d1 --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +## Tiny Syslog Server in Python. +## +## This is a tiny syslog server that is able to receive UDP based syslog +## entries on a specified port and save them to a file. +## That's it... it does nothing else... +## There are a few configuration parameters. + +LOG_FILE = 'Master_C.log' +HOST, PORT = "0.0.0.0", 48514 + +# +# NO USER SERVICEABLE PARTS BELOW HERE... +# + +import logging +import socketserver + +logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a') + +class SyslogUDPHandler(socketserver.BaseRequestHandler): + + def handle(self): + data = bytes.decode(self.request[0].strip()) + socket = self.request[1] + print( "%s : " % self.client_address[0], str(data)) + logging.info(str(data)) + +if __name__ == "__main__": + try: + server = socketserver.UDPServer((HOST,PORT), SyslogUDPHandler) + server.serve_forever(poll_interval=0.5) + print(f"Syslog running on host {HOST} port {PORT}") + except (IOError, SystemExit): + raise + except KeyboardInterrupt: + print ("Crtl+C Pressed. Shutting down.") \ No newline at end of file