2

I have a file which was created by a script which will be colored when I open using cat. But when I tried to send that file as attachment, it is not showing properly. Like below it is showing.

^[[33m================================================================================^[[m
                            ^[[34m172.29.0.110^[[m
^[[33m================================================================================^[[m
Filesystem              Size    Used    Avail   Use%    Mounted on
/dev/mapper/centos  109G   13G   91G  13% /
/dev/mapper/mpatha       1.6T  1.3T  277G  83% /var/lib/SQL
^[[33m================================================================================^[[m
                            ^[[34m172.29.8.110^[[m
^[[33m================================================================================^[[m
Filesystem              Size    Used    Avail   Use%    Mounted on
/dev/mapper/centos  117G  9.1G  102G   9% /
/dev/mapper/mpatha       1.6T  1.4T  109G  93% /var/lib/SQL
^[[33m================================================================================^[[m
                            ^[[34m172.29.16.110^[[m
^[[33m================================================================================^[[m
Filesystem              Size    Used    Avail   Use%    Mounted on
/dev/mapper/centos      117G   18G   94G  17% /
/dev/mapper/VG01-LV 1.5T  812G  590G  58% /var/lib/SQL
^[[33m================================================================================^[[m
                            ^[[34m172.29.26.110^[[m
^[[33m================================================================================^[[m
Filesystem              Size    Used    Avail   Use%    Mounted on
/dev/mapper/LogVol02  117G   22G   90G  20% /
/dev/mapper/mpathm    1.6T  1.1T  435G  71% /var/lib/SQL

The script(part of the script) that creates the file is ::

for IP in $(cat file.txt); do
    (echo -e  "\e[33m$LINE\e[m"
    echo -e "                            \e[34m$IP\e[m                                 "
    echo -e "\e[33m$LINE\e[m"
    echo -e "Filesystem\t\tSize\tUsed\tAvail\tUse%\tMounted\ton"
    ssh $SSH_ARG -q user@${IP} "df -Ph | egrep -iv 'filesystem|boot|tmpfs'") >> /disk_${DATE}_log
    echo -e "\e[33m$LINE\e[m"
    echo "Successful for $IP"
done

cat disk_${DATE}_log | mail -s "Disk space" mail@mail.com

I can remove those echo's that creating colors, but I want to know is there any way I can send this file properly by mail or can I do something in mail command to solve this?

Thomas Dickey
  • 76,765
prado
  • 930
  • 1
  • 11
  • 33
  • This is not a duplicate. This question is not asking how to strip out or to not even send the control codes, as the questioner clearly states. This question is asking how to generate non-text/plain mail messages with the sendmail and echo commands; or how to view the aforegiven bodypart in such a way that the escape sequences are processed. – JdeBP Mar 25 '17 at 14:12

1 Answers1

1

You can do what's asked by

  • converting the file with ANSI color escapes to HTML, and

  • sending the resulting HTML file as an attachment.

The command-line mail (or mailx) program may not be very useful for this, but there are different versions (see for example Mailx send html message). But without knowing what specific implementation of "mail" you are using, other (more predictable) tools work well.

Many mail clients (such as mutt) can be configured to display an HTML attachment using a suitable viewer. See for example Reading HTML email with Mutt.

For converting, there are a few scripts/programs, e.g., more than one named "ansi2html". I commented on one in Convert screen log to html, which may work for you (see link to script).

For sending the attachment, keep in mind that you cannot simplify pipe the data to the mail client, and that the command-line parameters differ. Some discussion focusing on mutt is given in How do I send a file as an email attachment using Linux command line?

Thomas Dickey
  • 76,765