From 31a2f897cfd72618879f348b9c6c63b6f317d2c5 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 3 Feb 2008 22:09:07 +0000 Subject: Rewrite to use common localshowlogentries. --- localshowlogentries | 61 +++++++++++++++++++++++++++++++++++++++ localshowmaildelivered | 64 +++++++++++++---------------------------- localshowmailreceived | 50 ++++++-------------------------- localshowmailreceivedfromdomain | 18 +++++++----- localshowuserfromip | 54 +++++++--------------------------- 5 files changed, 111 insertions(+), 136 deletions(-) create mode 100755 localshowlogentries diff --git a/localshowlogentries b/localshowlogentries new file mode 100755 index 0000000..4b7986b --- /dev/null +++ b/localshowlogentries @@ -0,0 +1,61 @@ +#!/bin/sh +# +# /usr/local/sbin/localshowlogentries +# Copyright 2007-2008 Jonas Smedegaard +# +# $Id: localshowlogentries,v 1.1 2008-02-03 22:09:07 jonas Exp $ +# +# List recent log entries matching certain criteria +# + +set -e + +output="${output:-shortline}" +logfilecount="${logfilecount:-1}" +pattern="${pattern:-user_dovecot}" + +searchstring="$1" +egrep_before="$2" +egrep_after="$3" +sed_shortline="${4:-$sed_longline}" +sed_msgid="$5" +sed_qid="$6" +sed_username="$7" +sed_longline="${8:-s/^\(.\{15\}\) [^:]*: /\1 /}" +sed_time="${8:-s/^\(.\{15\}\) .*$/\1 /}" + +case $output in + shortline) + sedstring="$sed_shortline" + ;; + msgid) + sedstring="$sed_msgid" + ;; + qid) + sedstring="$sed_qid" + ;; + username) + sedstring="$sed_username" + ;; + longline) + sedstring="$sed_longline" + ;; + time) + sedstring="$sed_time" + ;; + *) + echo >&2 "ERROR: unknwon output \"$output\"" + exit 1 + ;; +esac + +if [ -z "$sedstring" ]; then + echo >&2 "ERROR: empty sed string for output \"$output\"" + exit 1 +fi + +find /var/log -name 'mail.log*' | sort -nr -t. -k3 | tail -n "$logfilecount" | xargs zcat -f \ + | egrep -- "$egrep_before$searchstring$egrep_after" \ + | sed "$sedstring" + +exit 0 diff --git a/localshowmaildelivered b/localshowmaildelivered index 2b4f992..5dea331 100755 --- a/localshowmaildelivered +++ b/localshowmaildelivered @@ -1,43 +1,42 @@ #!/bin/sh # # /usr/local/sbin/localshowmaildelivered -# Copyright 2007 Jonas Smedegaard +# Copyright 2007-2008 Jonas Smedegaard # -# $Id: localshowmaildelivered,v 1.6 2007-10-04 00:38:03 jonas Exp $ +# $Id: localshowmaildelivered,v 1.7 2008-02-03 22:09:07 jonas Exp $ # # List recently delivered emails # -input="${input:-user}" -output="${output:-shortline}" -logfilecount="${logfilecount:-1}" pattern="${pattern:-user_dovecot}" - mailname="$(cat /etc/mailname | head -n 1)" - sed_longline='s/^\(.\{15\}\) [^:]*: /\1 /' -sed_time='s/^\(.\{15\}\) .*$/\1 /' case $pattern in user_dovecot) # Dovecot "deliver" - egrep_before='deliver\((' - egrep_after=')\).*saved mail to ' - sed_shortline='s/^\(.\{15\}\) [^)]*): msgid=/\1 (/;s/: saved mail to/)/;s/ INBOX$//' - sed_msgid='s/^[^)]*): msgid=//;s/: .*$//' + localshowlogentries "$1" \ + 'deliver\((' \ + ')\).*saved mail to ' \ + 's/^\(.\{15\}\) [^)]*): msgid=/\1 (/;s/: saved mail to/)/;s/ INBOX$//' \ + 's/^[^)]*): msgid=//;s/: .*$//' ;; user_procmail) # Postfix/local via procmail - egrep_before=' postfix/local.*: to=<' - egrep_after='@'"$mailname"'>, .*status=sent \(delivered to command: procmail' - sed_shortline='s/^\(.\{15\}\) [^:]*: /\1 (/;s/: .*$/)/' - sed_qid='s/^[^:]*: //;s/: .*$//' + localshowlogentries "$1" \ + ' postfix/local.*: to=<' \ + '@'"$mailname"'>, .*status=sent \(delivered to command: procmail' \ + 's/^\(.\{15\}\) [^:]*: /\1 (/;s/: .*$/)/' \ + '' \ + 's/^[^:]*: //;s/: .*$//' ;; qid_postfix) - egrep_before='[^[:xdigit:]]' - egrep_after='[^[:xdigit:]]' - sed_shortline="$sed_longline" - sed_qid='s/^[^:]*: //;s/: .*$//' + localshowlogentries "$1" \ + '[^[:xdigit:]]' \ + '[^[:xdigit:]]' \ + "$sed_longline" \ + '' \ + 's/^[^:]*: //;s/: .*$//' ;; *) echo >&2 "ERROR: unknwon pattern \"$pattern\"" @@ -45,27 +44,4 @@ case $pattern in ;; esac -case $output in - shortline) - sedstring="$sed_shortline" - ;; - longline) - sedstring="$sed_longline" - ;; - msgid) - sedstring="$sed_msgid" - ;; - qid) - sedstring="$sed_qid" - ;; - *) - echo >&2 "ERROR: unknwon output \"$output\"" - exit 1 - ;; -esac - -localuser="$1" - -find /var/log -name 'mail.log*' | sort -nr -t. -k3 | tail -n "$logfilecount" | xargs zcat -f \ - | egrep -- "$egrep_before$localuser$egrep_after" \ - | sed "$sedstring" +exit 0 diff --git a/localshowmailreceived b/localshowmailreceived index 38bf64a..9278191 100755 --- a/localshowmailreceived +++ b/localshowmailreceived @@ -3,55 +3,21 @@ # /usr/local/sbin/localshowmailreceived # Copyright 2007-2008 Jonas Smedegaard # -# $Id: localshowmailreceived,v 1.4 2008-02-03 20:10:45 jonas Exp $ +# $Id: localshowmailreceived,v 1.5 2008-02-03 22:09:07 jonas Exp $ # # List recently received emails # # TODO: Handle other patterns than AMaViS # -input="${input:-user}" -output="${output:-shortline}" -logfilecount="${logfilecount:-1}" -pattern="${pattern:-user_amavis}" +set -e mailname="$(cat /etc/mailname | head -n 1)" -sed_longline='s/^\(.\{15\}\) [^:]*: /\1 /' -sed_time='s/^\(.\{15\}\) .*$/\1 /' +localshowlogentries "$1" \ + ' -> <' \ + '@'"$mailname" \ + 's/^\(.\{15\}\) [^)]*) \([^,]*\), [^]]*] \([^ ]*\) -> .*Message-ID: \([^>,]*>\?\).*/\1 \3 (\4) \2/' \ + 's/^.*Message-ID: //;s/: .*$//' -case $pattern in - user_amavis) - # AMaViSd new - egrep_before=' -> <' - egrep_after='@'"$mailname" - sed_shortline='s/^\(.\{15\}\) [^)]*) \([^,]*\), [^]]*] \([^ ]*\) -> .*Message-ID: \([^>,]*>\?\).*/\1 \3 (\4) \2/' - sed_msgid='s/^.*Message-ID: //;s/: .*$//' - ;; - *) - echo >&2 "ERROR: unknwon pattern \"$pattern\"" - exit 1 - ;; -esac - -case $output in - shortline) - sedstring="$sed_shortline" - ;; - longline) - sedstring="$sed_longline" - ;; - msgid) - sedstring="$sed_msgid" - ;; - *) - echo >&2 "ERROR: unknwon output \"$output\"" - exit 1 - ;; -esac - -localuser="$1" - -find /var/log -name 'mail.log*' | sort -nr -t. -k3 | tail -n "$logfilecount" | xargs zcat -f \ - | egrep -- "$egrep_before$localuser$egrep_after" \ - | sed "$sedstring" +exit 0 diff --git a/localshowmailreceivedfromdomain b/localshowmailreceivedfromdomain index ab84bb8..dfe986f 100755 --- a/localshowmailreceivedfromdomain +++ b/localshowmailreceivedfromdomain @@ -1,17 +1,21 @@ #!/bin/sh # # /usr/local/sbin/localshowmailreceivedfromdomain -# Copyright 2007 Jonas Smedegaard +# Copyright 2007-2008 Jonas Smedegaard # -# $Id: localshowmailreceivedfromdomain,v 1.3 2007-09-27 21:43:37 jonas Exp $ +# $Id: localshowmailreceivedfromdomain,v 1.4 2008-02-03 22:09:07 jonas Exp $ # # List emails recently received from a certain domain # -# TODO: Handle other indicatiors than AMaViS +# TODO: Handle other patterns than AMaViS # -logfiles="${logfiles:-1}" +set -e -find /var/log -name 'mail.log*' | sort -nr -t. -k3 | tail -n "$logfiles" | xargs zcat -f \ - | egrep -- '@'"$1"'> -> ' \ - | sed 's/^\(.\{15\}\) [^)]*) \([^,]*\), [^]]*] \([^ ]*\) -> .*Message-ID: \([^>,]*>\?\).*/\1 \3 (\4) \2/' +localshowlogentries "$1" \ + '@' \ + '> -> ' \ + 's/^\(.\{15\}\) [^)]*) \([^,]*\), [^]]*] \([^ ]*\) -> .*Message-ID: \([^>,]*>\?\).*/\1 \3 (\4) \2/' \ + 's/^.*Message-ID: //;s/: .*$//' + +exit 0 diff --git a/localshowuserfromip b/localshowuserfromip index 69e9122..cfd5d05 100755 --- a/localshowuserfromip +++ b/localshowuserfromip @@ -1,53 +1,21 @@ #!/bin/sh # # /usr/local/sbin/localshowuserfromip -# Copyright 2007 Jonas Smedegaard +# Copyright 2007-2008 Jonas Smedegaard # -# $Id: localshowuserfromip,v 1.3 2007-11-14 13:31:37 jonas Exp $ +# $Id: localshowuserfromip,v 1.4 2008-02-03 22:09:07 jonas Exp $ # # List recent identifiable users from some IP address # -input="${input:-user}" -output="${output:-shortline}" -logfilecount="${logfilecount:-1}" -pattern="${pattern:-user_dovecot}" +set -e -sed_longline='s/^\(.\{15\}\) [^:]*: /\1 /' -sed_time='s/^\(.\{15\}\) .*$/\1 /' +localshowlogentries "$1" \ + 'dovecot: .*: Login: .*, rip=(' \ + '), lip=' \ + 's/^\(.\{15\}\) [^:]*: \([^-]*\)-login: Login: user=, .*$//' \ + '' \ + '' \ + 's/^.* user=, .*$//' -case $pattern in - user_dovecot) - # Dovecot "deliver" - egrep_before='dovecot: .*: Login: .*, rip=(' - egrep_after='), lip=' - sed_shortline='s/^\(.\{15\}\) [^:]*: \([^-]*\)-login: Login: user=, .*$//' - sed_username='s/^.* user=, .*$//' - ;; - *) - echo >&2 "ERROR: unknwon pattern \"$pattern\"" - exit 1 - ;; -esac - -case $output in - shortline) - sedstring="$sed_shortline" - ;; - longline) - sedstring="$sed_longline" - ;; - username) - sedstring="$sed_msgid" - ;; - *) - echo >&2 "ERROR: unknwon output \"$output\"" - exit 1 - ;; -esac - -ip="$1" - -find /var/log -name 'syslog*' | sort -nr -t. -k3 | tail -n "$logfilecount" | xargs zcat -f \ - | egrep -- "$egrep_before$ip$egrep_after" \ - | sed "$sedstring" +exit 0 -- cgit v1.2.3