A-Net Online

  • Python hacks

    From chibiko@911:1423/0 to All on Sun Oct 13 16:31:53 2024
    This is a solution to the problem about acting on a log file as it is being written to, without much of a hassle. There's a way to do it purely in Python, but why do it when we can RE use it??
    It goes like this:

    --- cut here ---

    import re
    import subprocess

    log = '/home/user/log'

    f = subprocess.Popen(['tail', '-F', log], stdout=subprocess.PIPE)

    while True:
    line = f.stdout.readline()
    match = re.search(b'debug', line) # search for pattern
    if match:
    # do something!

    --- cut here ---

    Subprocess is used to open a tail process and send data to stdout, then there's an infinite loop that keeps reading a line and searches for a pattern to trigger an action.
    The main reason this should be used is to answer to logs in a fast way. It processes all lines passed through it. So, overall, pretty easy to use.

    --- Mystic BBS v1.12 A47 2021/12/25 (Windows/32)
    * Origin: d i s t o r t i o n // d1st.org (911:1423/0)
  • From paulie420@911:1503/0 to chibiko on Sun Oct 13 19:00:50 2024
    This is a solution to the problem about acting on a log file as it is being written to, without much of a hassle. There's a way to do it
    purely in Python, but why do it when we can RE use it??

    The main reason this should be used is to answer to logs in a fast way.
    It processes all lines passed through it. So, overall, pretty easy to
    use.

    Simple pimple - thanks for sharing your log watcher!



    |07p|15AULIE|1142|07o
    |08.........

    --- Mystic BBS v1.12 A49 2024/05/29 (Linux/64)
    * Origin: 2o fOr beeRS bbS>>20ForBeers.com:1337 (911:1503/0)
  • From dingo@911:1801/0 to chibiko on Tue Nov 5 21:28:01 2024

    On Sunday, October 13th chibiko said...
    This is a solution to the problem about acting on a log file as it is being written to, without much of a hassle.

    I love python as much as the next guy, heck I even contributed to it and wrote a bbs in python -- but maybe I can provide a counter-hack, that less(1) has the ability to "tail -F" a logfile builtin. And less(1) can also match patterns.

    Open a logfile with less, like "less log.txt", and press "F" to start "following" the file.

    At any time, you can press ^C, it won't stop the program, it will only stop following.

    You can then search. Since you're on the bottom of the file its probably best to search backwards with "?searchterm" but "/searchterm" is also fine.

    Anyway I used Linux and less for 20 years until I learned to stop executing tail, then stopping it to execute less or vi. Also you can open a file from less in your editor with 'v', so anyway its a good program to start with.




    --- ENiGMA 1/2 v0.0.14-beta (linux; x64; 18.18.2)
    * Origin: Xibalba -+- xibalba.l33t.codes:44510 (911:1801/0)