0

Bash script blcheck from github format issues with emailing output

Console output displays correct, but when sent to email via mailx will on go as attachment, not to body.
33%% dnsbl.inps.de ✓
50%% xbl.spamhaus.org ✓

Output to file or email example:
16%% dnsbl-3.uceprotect.net ^MESC(BESC[m 16%% dnsbl-.uceprotect.net ✓ESC(BESC[m

Advise pls on how can I correct the format in file & email ?

CMD that's not sending to eamil body:

sh blcheck -v xx.xx.xx.xx | mail -s "server-blcheckdate" user@email.exampel

script: https://github.com/IntellexApps/blcheck


After assistance of @john1024 & @cas & previous post Removing Control Chars below cmd resolved issue.

CMD: ./blcheck-l -v xx.xx.xx.xx | perl -pe 's/\e([^[]]|[.*?[a-zA-Z]|].*?\a)//g' | col -b | mail -s "blcheck-l -v `date`" email@address

darko-poljak has submitted a pull request on github with a new option to make output friendly for non interactive use.
https://github.com/IntellexApps/blcheck/pull/2

0
2

What you see are ANSI escape sequences. In blcheck, they are used to change the colors of the text that is displayed on the terminal. As you have discovered, they don't work in email messages.

To remove them permanently, edit the blcheck script with your favorite editor. Find the lines:

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CLEAR=$(tput sgr0)

And replace those lines with:

RED=
GREEN=
YELLOW=
CLEAR=

This will prevent blcheck from adding the ANSI sequences in the first place.

More flexible approach

It is possible to make the code work for either the terminal (ANSI) or a pipeline (no ANSI). The following code tests to see if stdout is a terminal. If it is, it sets the color variables to the required ANSI sequences. If it is not a terminal, no ANSI sequences are generated.

RED=
GREEN=
YELLOW=
CLEAR=
if [ -t 1 ]
then
    RED=$(tput setaf 1)
    GREEN=$(tput setaf 2)
    YELLOW=$(tput setaf 3)
    CLEAR=$(tput sgr0)
fi
23
  • You could also modify blcheck so that it uses colors if stdout is a terminal, and not use colors if stdout is a pipe. e.g. if [ -t 1 ] ; then RED=$(tput setaf 1) ; GREEN=... ; else RED= ; GREEN= ; ... ; fi
    – cas
    Nov 3 '15 at 6:52
  • @cas Good suggestion. I added the code for that.
    – John1024
    Nov 3 '15 at 6:58
  • @Snowie Glad it worked. (I cannot reproduce your email attachment issue: when I run the mail command that you provide, I get the output in the body of the email message.) Separately, blcheck is a bash script. So, for best results, you should call it as bash blcheck ...., not sh blcheck ... because, on many systems, sh is not bash.
    – John1024
    Nov 3 '15 at 7:06
  • Hey John1024 - tried with bash & still arrives as an email attachment, which will drive me crazy as I just want to have a quick glance at email to make sure no blacklisting, the server is running Postfix MTA.
    – Snowie
    Nov 3 '15 at 7:22
  • John1024 - wat MTA r u using? I just tried on server with exim MTA & also as attached file.
    – Snowie
    Nov 3 '15 at 7:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.