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

Snowie
  • 59

1 Answers1

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
John1024
  • 74,655
  • 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 03 '15 at 06:52
  • @cas Good suggestion. I added the code for that. – John1024 Nov 03 '15 at 06: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 03 '15 at 07: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 03 '15 at 07:22
  • John1024 - wat MTA r u using? I just tried on server with exim MTA & also as attached file. – Snowie Nov 03 '15 at 07:55
  • are you using bsd-mailx or heirloom-mailx? – cas Nov 03 '15 at 08:31
  • @Snowie, just add #!/bin/bash as the first line and make it executable with chmod +x blcheck. then you can run it with just blcheck if it's in the PATH or ./blcheck if it's in current dir. – cas Nov 03 '15 at 08:32
  • Thanks cas - centos 6 with postfix as MTA - mailx.x86_64 12.4-8.el6_6 – Snowie Nov 03 '15 at 09:36
  • @Snowie I am using bsd-mailx. The server is exim. – John1024 Nov 03 '15 at 14:34
  • Thanks John1024 & cas - centos 6 with postfix as MTA - mailx.x86_64 12.4-8.el6_6 its from heirloom with a BSD licence. Also tried on server running same malix ver but with Exim & end up with an attachment. Another script I run pflogsumm.pl with the same mail cmd dumps it to email body OK. – Snowie Nov 04 '15 at 01:28
  • @John1024, cas, The reason its still mailing as attachment for me is the formatting, if I dump to file then remove Ctrl Char with: cat myfile | perl -pe 's/\e([^\[\]]|[.?[a-zA-Z]|].?\a)//g' | col -b > myfile-fixed I can then mail & as body. Now I know why but need to figure out how to edit blcheck so it outputs in an mailx email friendly way for me. – Snowie Nov 04 '15 at 02:57
  • @Snowie I downloaded blcheck as per your link. After I removed the calls to tput (as per this answer), I have tried running it and I do not find any escapes (\E) or alerts (\a) in the output. I tested this by running your perl script on the output, and then comparing with cmp: no difference. Can you tell under what circumstances the modified script produces these characters? Or, if not, can show me sample output that contains those characters so I can look in the source to try to see where they are coming from? – John1024 Nov 04 '15 at 03:58
  • @john1024 this is with the tput removed " PTR resolves to mydomain.com Matching against 3 blacklists 16%% dnsbl-3.uceprotect.net ^M 16%% dnsbl-3.uceprotect.net ✓ " This ^M character I guess is the problem – Snowie Nov 04 '15 at 04:46
  • @John1024 when I run script I use -v but either way the carriage returns ^M are there, when I output to file they can be seen & if send to email they r also there, not seen in output console of course. – Snowie Nov 04 '15 at 05:17
  • @Snowie The ^M character appears in the script several times in the form \r. You can remove it by doing a search-and-replace in your favorite editor or by running sed 's/\r/CR/g' blcheck >blcheck.new. However,^M` is not one of the characters affected by your perl script and it seems like an unlikely character to trigger a problem with text attachment. Try removing it and see what happens but there is a chance that the problem is somewhere else. – John1024 Nov 04 '15 at 06:08
  • @Snowie It looks to me like \r probably only appears in the output if verbose is set. It might be simpler to just try it without -v. – John1024 Nov 04 '15 at 06:31
  • @John1024 with all the \r & tput removed it still fails if I use -v verbose this char can be seen in email attachment when using -v. Sends to email body with no -v. Also sends if I dump to file then run perl -pe 's/\e([^[]]|[.?[a-zA-Z]|].?\a)//g' | col -b then mail – Snowie Nov 04 '15 at 06:51
  • @Snowie OK. Let's try dumping to file, say file1. Then run perl -pe 's/\e([^[]]|[.*?[a-zA-Z]|].*?\a)//g' file1 | col -b >file2. Then run diff file1 file2 and let's see where the differences really are. – John1024 Nov 04 '15 at 07:18
  • OK done how do I best show u the diff outpu? – Snowie Nov 04 '15 at 08:11
  • @John1024 comment box not enough to show u diff result. – Snowie Nov 04 '15 at 08:14
  • @John1024 OK its working! - emailing to body. blcheck edited with mod to #colors as above & all /r rmoved. CMD that's working: ./blcheck-latest -v xx.xx.xx.xx | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b | mail -s "blcheck latestdate" email@address – Snowie Nov 04 '15 at 08:59
  • @Snowie Glad it's working. For future reference, it is considered good stackoverflow style for the OP (that's you, here) to respond to comments by editing his question (where full formatting is possible) to add the additional information. (Stackoverflow considers comments to be disposable and subject to deletion at any time.) – John1024 Nov 04 '15 at 22:33
  • @John1024 thanks again John, I've had a go at tidy up edit of post. – Snowie Nov 05 '15 at 13:08