Archive

Kategorien

Löschen von Mails aus der Queue mit bestimmten Logins

#!/bin/bash
 
#########################################################################
#									#
# searchMsgId4SASL.sh							#
# @author	tobi <jahlives@gmx.ch>					#
# @License	GPL3							#
# @copyright	Can be changed as you want. No need for specify the	#
#		original author						#
# It's allowed for commercial usage but please ask via email prior	#
# to use it in any commercial matter					#
#									#
# This script is as it is. No guarantee for anything. Use at own risk	#
#									#	
#########################################################################
 
DELETE=0
SEARCH=''
STRING=''
while [ $# -gt 0 ] ; do
 case "$1" in
  '-lu')
   shift
   SEARCH="$1"
   shift
  ;;
  '-d')
   DELETE=1
   shift
  ;;
  '-s')
   shift
   STRING="$1"
   shift
  ;;
  '-h')
   echo '# searchMsgId4SASL.sh helps admins to quickly remove msg from mailqueue for a certain user SASL login'
   echo "# $0 -lu me@mydomain.tld [-d]"
   echo '# -lu  email address of the user to clean from queue (username used in SASL login)'
   echo '# -d   enforces delete mode expects -lu to be set. If not set matching msg ids will be printed only. Be VERY careful using this switch. !!!"Quadrupel" check what you set in -lu!!!'
   echo '#      WARNING: if you specify -lu and -d then the script goes to delete mode. There wont be any "are you sure"-windows style question but all mails from user that match $SEARCH will be deleted'
   echo "# -s   defines a custom search string. Use it if you're looking for a special content string of a mail"
   echo '#      -s and -lu CANNOT be set at the same time. Be careful with -s as it searches the entire mail (headers and body)!!!'
   echo '# -h   show this help'
   exit 0
  ;;
  *)
   echo "Unknown parameter $1. Type -h for short help" && exit 1
  ;;
 esac
done
[[ "x$SEARCH" = 'x' && "x$STRING" = 'x' ]] && echo '###' && $0 -h && echo '###' && exit 1
[[ "x$SEARCH" != 'x' && "x$STRING" != 'x' ]] && $0 -h && echo '###' && exit 1
 
for i in $(mailq|tail -n +2|awk 'BEGIN { RS = "" }/'""'/{ if($1 != "'--'") print $1 }' | tr -d '*!') ; do
 if [ "x$SEARCH" != 'x' ] ; then
  RET=$(find /var/spool/postfix -type f -name "$i" -exec grep "sasl_username=$SEARCH" {} \;)
 elif [ "x$STRING" != 'x' ] ; then
  RET=$(find /var/spool/postfix -type f -name "$i" -exec grep "$STRING" {} \;)
 fi 
 [[ "x$RET" != 'x' && $DELETE -eq 0 ]] && echo $i
 [[ "x$RET" != 'x' && $DELETE -eq 1 ]] && postsuper -d $i
done

2 comments to Löschen von Mails aus der Queue mit bestimmten Logins

  • Tobi

    Aktuellste Version. Als Action kann nun auch –hold angegeben werden, um Mails in die HOLD-Queue zu schieben. Zudem kann man mit dem Paramtert -cr die Recipients einer Mail ermitteln und sich die Msg-IDs ausgeben lassen, welche mehr Recipients haben als mit -cr erlaubt sind

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    #!/bin/bash

    #########################################################################
    #                                   #
    # searchMsgId4SASL.sh                           #
    # @author   tobi <jahlives@gmx.ch>                  #
    # @License  GPL3                            #
    # @copyright    Can be changed as you want. No need for specify the #
    #       original author                     #
    # It's allowed for commercial usage but please ask via email prior  #
    # to use it in any commercial matter                    #
    #                                   #
    # This script is as it is. No guarantee for anything. Use at own risk   #
    #                                   #  
    #########################################################################

    DELETE=0
    SEARCH=''
    STRING=''
    HOLD=0
    PRINT=0
    CR=0
    while [ $# -gt 0 ] ; do
     case "$1" in
      '-lu')
       shift
       SEARCH="$1"
       shift
      ;;
      '-d')
       DELETE=1
       shift
      ;;
      '-s')
       shift
       STRING="$1"
       shift
      ;;
      '--hold')
       HOLD=1
       shift
      ;;
      '-cr')
       shift
       CR=$1
       shift
      ;;
      '-h')
       echo ''
       echo '# searchMsgId4SASL.sh helps admins to quickly remove msg from mailqueue for a certain user SASL login'
       echo "# $0 -lu <string>|-s <string>|-cr <number> [-d|--hold|-?]"
       echo ''
       echo '# -lu    email address of the user to clean from queue (username used in SASL login)'
       echo '# -d     enforces delete mode expects -lu to be set. If not set matching msg ids will be printed only. Be VERY careful using this switch. !!!"Quadrupel" check what you set in -lu!!!'
       echo '#        WARNING: if you specify -lu and -d then the script goes to delete mode. There wont be any "are you sure"-windows style question but all mails from user that match $SEARCH will be deleted'
       echo "# -s     defines a custom search string. Use it if you're looking for a special content string of a mail"
       echo '#        -s and -lu CANNOT be set at the same time. Be careful with -s as it searches the entire mail (headers and body)!!!'
       echo '# -?     with -s specified the sasl_loginnames can be printed'
       echo '# --hold put matching mail in HOLD queue'
       echo '# -cr    maximum number of allowed recipients per mail'
       echo '# -h     show this help'
       exit 0
      ;;
      '-?')
       shift
       PRINT=1
       shift
      ;;
      '-c')
       echo '#########################################################################'
       echo '#                                                                       #'
       echo '# searchMsgId4SASL.sh                                                   #'
       echo '# @author       tobi <jahlives@gmx.ch>                                  #'
       echo '# @License      GPL3                                                    #'
       echo '# @copyright    Can be changed as you want. No need for specify the     #'
       echo '#               original author                                         #'
       echo '# It is allowed for commercial usage but please ask via email prior     #'
       echo '# to use it in any commercial matter                                    #'
       echo '#                                                                       #'
       echo '# This script is as it is. No guarantee for anything. Use at own risk   #'
       echo '#                                                                       #'
       echo '#########################################################################'
       exit 0
      ;;
      *)
       echo "Unknown parameter $1. Type -h for short help" && exit 1
      ;;
     esac
    done
    [[ "x$SEARCH" = 'x' && "x$STRING" = 'x' && CR = 0 ]] && echo '###' && $0 -h && echo '###' && exit 1
    [[ "x$SEARCH" != 'x' && "x$STRING" != 'x' && CR = 0 ]] && $0 -h && echo '###' && exit 1

    [[ $HOLD -eq 1 && $DELETE -eq 1 ]] && echo '###' && $0 -h && echo '###' && exit 1

    if [[ $CR -gt 0 ]] ; then
     for i in $(find /var/spool/postfix/deferred -type f) ; do
      COUNT=$(postcat $i 2>/dev/null | grep -c 'original_recipient')
      ii="${i##*/}"
      [ "x$ii" = 'x' ] && continue
      if [ $COUNT -gt $CR ] ; then
       [ $DELETE -eq 1 ] && postsuper -d $ii && continue
       echo "$i --> $COUNT" && continue
      else
       continue
      fi
     done
     exit 0
    fi
    for i in $(mailq|tail -n +2|awk 'BEGIN { RS = "" }/'""'/{ if($1 != "'--'") print $1 }' | tr -d '*!') ; do
     if [ "x$SEARCH" != 'x' ] ; then
      RET=$(find /var/spool/postfix -type f -name "$i" -exec grep "sasl_username=$SEARCH" {} \;)
     elif [ "x$STRING" != 'x' ] ; then
      if [ $PRINT -eq 0 ] ; then
       RET=$(find /var/spool/postfix -type f -name "$i" -exec grep "$STRING" {} \;)
      else
       RET=$(find /var/spool/postfix -type f -name "$i" -exec grep "$STRING" {} \;)
       if [ "x$RET" != 'x' ] ; then
        RET=$(find /var/spool/postfix -type f -name "$i" -exec grep sasl_username '{}' \; | awk '{print $3}')
        [ "x$RET" != 'x' ] && RET="$(postcat $RET | grep sasl_username | awk -F'=' '{print $2}')"
       fi
      fi
     fi
     [[ "x$RET" != 'x' && $DELETE -eq 0 && $HOLD -eq 0 && $PRINT -eq 0 ]] && echo $i
     [[ "x$RET" != 'x' && $DELETE -eq 1 ]] && postsuper -d $i
     [[ "x$RET" != 'x' && $HOLD -eq 1 ]] && postsuper -h $i
     [[ "x$RET" != 'x' && $PRINT -eq 1 ]] && echo "$RET"
    done

  • Tobi

    Nowadays with recent postfix versions such stunts are not required anymore to achive the same. Postfix postqueue command knows -j parameter to output the queue as json. That means that sender and recipient are now on the same line. So one can use two concatenated grep’s to determine the relevant json lines and extract the queue-id’s from those lines. Thanks @Wietse 🙂

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

  

  

  

8 − 3 =

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahren Sie mehr darüber, wie Ihre Kommentardaten verarbeitet werden .