You are not logged in.

#1 2009-08-25 16:47:48

yitzle
Member
Registered: 2008-10-19
Posts: 18

Following /var/logs on IRC

IRC wrote:

grawity| I have seen an IRC bot being used as the syslog-ng server.
grawity| http://sine.cluenet.org/~grawity/trash/syslogger.txt

Well. Interesting idea. So I slapped together a fast bash script.
Steps:
- build an array of regular files that are text files from /var/log/*
- connect to IRC
- watch those files and transfer output to IRC

USAGE: logsToIrc [-n NICK] [-c CHANNEL] [-s SERVER] [-p PORT] [-hv]
            -n NICK
                    Specify the NICK to use
            -c CHANNEL
                    Specify the CHANNEl to connect to - be sure to escape the # or bash will think it is a comment
            -s SERVER
                    Specify the SERVER to connect to
            -p PORT
                    Specify the PORT to use
            -h
                    This help message
            -v
                    Verbose. Stops burying nc output
#!/bin/bash

# Author: yitzle on bbs.archlinux.org
# Version 1.0.1

# CHANGELOG
# Version 1.0.1 - typo found bybrisbin33

# defaults
chan='#my_logs'
nick='logsToIrc'
serv='irc.freenode.net'
port=6667
loud=0
help=0

# Print help message
help () {
        echo "USAGE: $(basename $0) [-n NICK] [-c CHANNEL] [-s SERVER] [-p PORT] [-hv]"
        echo '            -n NICK'
        echo '                    Specify the NICK to use'
        echo '            -c CHANNEL'
        echo '                    Specify the CHANNEl to connect to - be sure to escape the # or bash will think it is a comment'
        echo '            -s SERVER'
        echo '                    Specify the SERVER to connect to'
        echo '            -p PORT'
        echo '                    Specify the PORT to use'
        echo '            -h'
        echo '                    This help message'
        echo '            -v'
        echo '                    Verbose. Stops burying nc output'
        exit 0
}

# Process parameters
while getopts 'c:s:n:p:hv' OPT; do
        case $OPT in
                c)
                        chan="$OPTARG"
                        ;;
                s)
                        serv="$OPTARG"
                        ;;
                n)
                        nick="$OPTARG"
                       ;;
                p)
                        port="$OPTARG"
                        ;;
                h)
                        help=1
                        ;;
                v)
                        ((loud++))
                        ;;
                *)
                        help=1
                        ;;
        esac
done

# Show the help or run
(( help )) && help

# Build the file list. Only regular files that the "file" command tells us is text
files=()
for f in /var/log/* ; do
        [[ -f $f ]] || continue
        file "$f" | grep -q 'text' || continue
        files+=("$f")
done

# Unless verbose, bury STDOUT
(( loud )) || exec >/dev/null
(( load )) && echo "Following ${#files[@]} files: ${files[@]}"

# The real work
{
        # Connect
        echo "PASS password"
        echo "NICK $nick"
        echo "USER $nick * 0 :logs"
        echo "JOIN $chan"

        # Watch the files
        tail -n 0 -f "${files[@]}" | while read ; do
                [[ $REPLY ]] || continue
                # On non-empty line, send it to the channel
               echo "PRIVMSG $chan :$REPLY" 
        done
} | nc "$serv" "$port"

Last edited by yitzle (2009-08-25 18:09:47)

Offline

#2 2009-08-25 17:53:19

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Following /var/logs on IRC

i may have spotted two typos:

(( load )) & echo "Following ${#files[@]} files: ${files[@]}"

# should be

(( load )) && echo "Following ${#files[@]} files: ${files[@]}"

and

        # Watch the files
        tail -n 0 -f "${files[@]}" | while read ; do
                (( loud >= 2 )) && echo "$REPLY" >&2


# should be

        # Watch the files
        tail -n 0 -f "${files[@]}" | while read REPLY; do
                (( loud >= 2 )) && echo "$REPLY" >&2

looks pretty nifty though big_smile

Offline

#3 2009-08-25 18:07:53

yitzle
Member
Registered: 2008-10-19
Posts: 18

Re: Following /var/logs on IRC

brisbin33 wrote:

i may have spotted two typos:

(( load )) & echo "Following ${#files[@]} files: ${files[@]}"

# should be

(( load )) && echo "Following ${#files[@]} files: ${files[@]}"

and

        # Watch the files
        tail -n 0 -f "${files[@]}" | while read ; do
                (( loud >= 2 )) && echo "$REPLY" >&2


# should be

        # Watch the files
        tail -n 0 -f "${files[@]}" | while read REPLY; do
                (( loud >= 2 )) && echo "$REPLY" >&2

looks pretty nifty though big_smile

Thanks!

The second one is actually not an error. 'help read' reports "If no NAMEs are supplied, the line read is stored in the REPLY variable."

The problem is that the script doesn't reply to PINGs from the IRC server which can get it disconnected. I need to find a way to solve that or change from nc to, say, a perl script.

Offline

#4 2009-08-26 00:21:10

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: Following /var/logs on IRC

This is cool. Nice work smile

Offline

#5 2009-08-26 00:32:18

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Following /var/logs on IRC

yitzle wrote:

The second one is actually not an error. 'help read' reports "If no NAMEs are supplied, the line read is stored in the REPLY variable."

awesome, learn something new every day.

Offline

#6 2009-08-26 01:23:15

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: Following /var/logs on IRC

This is quite a cool idea, I love the idea of IRC bots, and have written a few small ones myself, including one in PHP that I use daily, I have mine set up to check /tmp/messages every minute and print everything in it to the channel, so on my website I can just add a line of code here and there to print status messages to that file, which is handy for notifying about gallery uploads and that kind of thing smile

If anyone thinks the code might be useful for something like this, let me know and I'll pastebin it or something. It is made up of a small IRC class and bot example, and it all works using callbacks. It's used like this,

$bot = New IRC(Array('ip' => '127.0.0.1', 'port' => '6667', 'nick' => 'BotName', 'realname' => 'BotName', 'debug' => false, 'trigger' => '.'));
$bot->addHandler('startup', 'bot_startup'); // Function called after connect
$bot->addHandler('message', 'bot_nowplaying', 'nowplaying'); // Function called if ".nowplaying" is sent to the bot
$bot->addHandler('reply', 'bot_JOIN', 'JOIN'); // Can capture IRC events too
$bot->addHandler('reply', 'bot_QUIT', 'QUIT');
$bot->addHandler('timer', 'bot_updateStuff'); // Run each minute
$bot->runBot();

Offline

Board footer

Powered by FluxBB