[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: home server for email box



Le 3/12/23 à 14:50, Greg Wooledge a écrit :
On Sun, Mar 12, 2023 at 12:13:54PM +0100, Yassine Chaouche wrote:
function net.ip.reputation(){
     revip=$(net.ip.reverse "$1")
     results=$(dig +short $revip.zen.spamhaus.org)
     [[ -z $results ]] && (echo "clean"; return 0)

You're caling return inside a subshell.  This doesn't actually return
from the function, since it occurs in a child process.

unicorn:~$ f() { true && (echo true; return 0); echo problem; }
unicorn:~$ f
true
problem

You either need to use "if", or use a curly-braced command group instead
of your parenthesized subshell command.

     [[ -z $results ]] && { echo "clean"; return 0; }

It would also be polite to declare your function's local variables as
"local".

     for result in $results

You're using 3 variables locally, so you could add

     local revip results result

at the top of the function.

Finally, you're relying on the output of "dig +short ..." to be safely
word-splittable.  I don't know whether this is always going to be true.
However, it does look like dig +short writes its output as a sequence
of items, one per line.  This means you can read them into an array
without performing word-splitting or globbing:

     mapfile -t results < <(dig +short "$revip".zen.spamhaus.org)
     if (( ${#results[@]} == 0 )); then
         echo clean
         return 0
     fi
     for result in "${results[@]}"
     do
         ...

The way you've got it *might* be OK.  I just don't know whether the
output of dig +short can ever contain internal whitespace or globbing
characters.  I'd rather not take that chance.



Thank you Greg, for your very valuable feedback,
care,
kindness,
and patience,
as usual.

function net.ip.reputation(){
    local revip result results
    revip=$(net.ip.reverse "$1")

    # parsing a string where spaces can be a problem
    # results=$(dig +short $revip.zen.spamhaus.org)

    # better way to retrive results in an array
    mapfile -t results < <(dig +short $revip.zen.spamhaus.org)
    (( ! ${#results[@]} )) && { echo "clean"; return 0; }
    for result in "${results[@]}"
    do
        case "$result" in
            127.0.0.2)
                echo "SBL      : SPAM sender"
                ;;
            127.0.0.3)
                echo "CSS      : snowshoe"
                ;;
            127.0.0.[4-7])
                echo "XBL/CBL  : trojans, exploits"
                ;;
            127.0.0.1[01])
                echo "PBL(ISP) : not supposed to send mail"
                ;;
        esac
    done
    return 1
}


Best,
--
yassine -- sysadm
+213-779 06 06 23
http://about.me/ychaouche
Looking for side gigs.


Reply to: