Solved Colors in the motd?

So I came accross the Thread 20501 and found the example to have escapes with color codes.

I'm curious, I could not get the motd to have colores, even using different escape sequences (i.g. 033) – so in which environment does login(1) print the motd? I was guessing it would be in the users context and prefered shell?



I did not post this in the off topic section since the underlying question is on topic.
 
Cool idea! I just tried it, and it works great. I added ESC [ 3 1 m before the text, and ESC [ 0 m afterwards, and now it is red.

In what context? I'm not sure I understand that question. login() first prints a line that shows when you last logged in, and then prints the content of motd (much of this is configurable in login, this is the default configuration, I haven't messed with it). If your terminal emulator (could be the hardware console) can display ANSI colors, the color shows up. Here is an example of the output from my system (cut and paste from a terminal emulator on a Macintosh, so the color is gone):
Code:
ralphmac-wl_~ > ssh house.lr.los-gatos.ca.us
Last login: Wed Aug  2 07:19:38 2017 from ralphmac-wl.lr.los-gatos.ca.us
FreeBSD 11.1-RELEASE (GENERIC) #0 r321309: Fri Jul 21 04:10:47 UTC 2017
Host house.lr.los-gatos.ca.us

Unauthorized use of this computer is prohibited, and will be punished.
house_~ >
The line that says "Unauthorized use..." is now read. Here is a hex dump of my motd file:
Code:
00000000  46 72 65 65 42 53 44 20  31 31 2e 31 2d 52 45 4c  |FreeBSD 11.1-REL|
00000010  45 41 53 45 20 28 47 45  4e 45 52 49 43 29 20 23  |EASE (GENERIC) #|
00000020  30 20 72 33 32 31 33 30  39 3a 20 46 72 69 20 4a  |0 r321309: Fri J|
00000030  75 6c 20 32 31 20 30 34  3a 31 30 3a 34 37 20 55  |ul 21 04:10:47 U|
00000040  54 43 20 32 30 31 37 0a  48 6f 73 74 20 68 6f 75  |TC 2017.Host hou|
00000050  73 65 2e 6c 72 2e 6c 6f  73 2d 67 61 74 6f 73 2e  |se.lr.los-gatos.|
00000060  63 61 2e 75 73 0a 0a 1b  5b 33 31 6d 55 6e 61 75  |ca.us...[31mUnau|
00000070  74 68 6f 72 69 7a 65 64  20 75 73 65 20 6f 66 20  |thorized use of |
00000080  74 68 69 73 20 63 6f 6d  70 75 74 65 72 20 69 73  |this computer is|
00000090  20 70 72 6f 68 69 62 69  74 65 64 2c 20 61 6e 64  | prohibited, and|
000000a0  20 77 69 6c 6c 20 62 65  20 70 75 6e 69 73 68 65  | will be punishe|
000000b0  64 2e 1b 5b 30 6d 0a                              |d..[0m.|
000000b7

Suggestion for debugging: Are you 100% sure that your terminal emulator can actually display colors? Try echo'ing in the shell: "ESC [ 3 1 m H a l l o ESC [ 0 m", and it should be red; if you use "3 4" in front, it should be blue. How you enter ESC depends on your shell; in both csh and bash it is Control-V Escape (using the escape key).

Warning: There is a motd service that lives in /etc/rc.d/motd. If you enable it with "motd_enable=YES" in /etc/rc.conf, then on every boot motd will be modified, but the modifications are minimal: It simply replaces the FreeBSD version in the first line with the up-to-date version.
 
Warning: There is a motd service that lives in /etc/rc.d/motd. If you enable it with "motd_enable=YES" in /etc/rc.conf, then on every boot motd will be modified, but the modifications are minimal: It simply replaces the FreeBSD version in the first line with the up-to-date version
Almost, it's actually update_motd="YES" instead and it's switched on by default (see /etc/defaults/rc.conf). It's not really a service, it's just a script that gets executed during boot.

Code:
update_motd="YES"       # update version info in /etc/motd (or NO)
 
Oh boy, let me answer this step by step, you won't believe where I messed up.

Are you 100% sure that your terminal emulator can actually display colors?

Yes, I use set prompt = "%N@%{\e[31m%}%m%{\e[0m%}:%~ %# " in my .cshrc as a reminder on which machine I'm on (different colors for different machines).

In what context? I'm not sure I understand that question.
Basically I wanted to know whether the user shell ( awk -F":" '{print $7}' /etc/passwd) is used or not.
Spoiler: It is!

(cut and paste from a terminal emulator on a Macintosh, so the color is gone)
Bam, this is just what hit me. I did copy the file (motd.txt) from my browser to a terminal.

How you enter ESC depends on your shell; in both csh and bash it is Control-V Escape (using the escape key).
And this was my second mistake, after also failing by using \e, \033, \[ and finally ^[ … I now got it working by using Control-V Escape.

Also your thanks for pointing out hexdump, that also showed my error:
5e 5b 5b 30 6d 0a |^[[0m.| wrong
1b 5b 30 6d 0a |.[0m.| right!


Using update_motd="YES" only messes with the prepending line, so I'm fine to use it.


Thank you, I'll flag this thread as solved.
 
login() first prints a line that shows when you last logged in, and then prints the content of motd (much of this is configurable in login, this is the default configuration, I haven't messed with it).

I don't have colors but I edited my MOTD:

Code:
                  Unauthorized Access Denied!!!
                           ,          ,
                          /(          )\
                          \\ \\___   / /
                          /- _  `-/  ' !
                          (/\\/ \\ \\   /
                          / /   | `    \
                          O O   ) /    |
                         `-^--'`<     '
                        (_.)  _  )   /
                         `.___/`    /
                           `-----' /
              <----.     __ / __   \\
              <----|====O)))==) \\) /====
              <----'    `--' `.__,' \\
                            |        |
                            \\       /
                      ______( (_  / \\______
                    ,'  ,-----'   |        \\
                    `--{__________)        \\/
I got the ascii art from games/cowsay.
 
Back
Top