Skip to Content
Linux Security Cookbook
book

Linux Security Cookbook

by Daniel J. Barrett, Richard E. Silverman, Robert G. Byrnes
June 2003
Intermediate to advanced
336 pages
8h 54m
English
O'Reilly Media, Inc.
Content preview from Linux Security Cookbook

9.32. Writing Log Entries via Shell Scripts

Problem

You want to add information to the system log using a shell script.

Solution

Use logger and this handy API, which emulates that of Perl and C:

               syslog-api.sh:
#!/bin/sh
ident="$USER"
facility="user"
openlog( ) {
        if [ $# -ne 3 ]
        then
                echo "usage: openlog ident option[,option,...] facility" 1>&2
                return 1
        fi
        ident="$1"
        local option="$2"
        facility="$3"
        case ",$option," in
                *,pid,*)            ident="$ident[$$]";;
        esac
}

syslog( ) {
        if [ $# -lt 2 ]
        then
                echo "usage: syslog [facility.]priority format ..." 1>&2
                return 1
        fi
        local priority="$1"
        local format="$2"
        shift 2
        case "$priority" in
                *.*)    ;;
                *)      priority="$facility.$priority";;
        esac
        printf "$format" "$@" | logger -t "$ident" -p "$priority"
}

closelog( ) {
        ident="$USER"
        facility="user"
}

To use the functions in a shell script:

#!/bin/sh
source syslog-api.sh
openlog `basename "$0"` pid local3
syslog warning "%d connections from %s" $count $host
syslog authpriv.err "intruder alert!"
closelog
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Linux Security Cookbook - Second Edition

Practical Linux Security Cookbook - Second Edition

Tajinder Kalsi
Mastering Linux Command Line

Mastering Linux Command Line

Coding Gears | Train Your Brain

Publisher Resources

ISBN: 0596003919Errata Page