3

Towards a better message of the day

 3 years ago
source link: https://dev.to/moopet/towards-a-better-message-of-the-day-1oh8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What's a MOTD?

A Message Of The Day is something that's displayed to you when you connect to a service. It can be the welcome message for an FTP session, the rules for an IRC server, or it can be the first thing you see when you open a terminal.

There's a file which lives at /etc/motd and its contents are displayed immediately before executing a login shell. What does that mean? It means it appears before anything in your shell startup files, like .bash_profile or .zshrc.

What's the point of it?

It's there so the owners of a system can tell all its users something when they connect. Something like "Welcome to FooCorp, your commands are all logged." or "Advance notice: this system will be rebooted for maintenance tonight at 7PM."
Anyone with super user rights can edit it.

Handy, eh? Well not so much for most developers these days, because you lot will mostly be using your system as a single user, and have no need to present that kind of information to yourself.

If you're on a foreign system and don't want to see the same boring old MOTD they've had for donkey's years, you can get round it by creating a file in your home directory called .hushlogin.

This is one time I'd be okay with you using touch to create a file.

My MOTD replacement

I share my shell configuration and init scripts across the systems I use and connect to a lot of remotes. I have the hostname in my prompt because otherwise it's too easy to choose the wrong terminal window and clobber my work files.

funky_motd() {
  printer="cat"
  filter="cat"

  if command -v toilet >/dev/null; then
    printer="toilet -t -f future"
  elif command -v figlet >/dev/null; then
    printer="figlet"
  fi

  if command -v lolcat >/dev/null; then
    filter="lolcat -S $(hostname | shasum | tr -d '[a-f]' | cut -b-4)"
  fi

  printf "\n"
  eval "echo $* | $printer | $filter"
  printf "\n"
}
Enter fullscreen modeExit fullscreen mode

Bonus level

Since I want to mainly use this as a welcome message when switching environments, I can add it to nvm or venv changes or whatever I want.

The main use though is for seeing in big letters what system I've connected to.

display_host_info() {
  # My work laptop has a dumb corporate name.
  if [[ "$(hostname)" =~ "\.local$" ]]; then
    funky_motd "Macbook"
  elif [ -n "$SSH_CLIENT" -o -n "$SSH_TTY" ]; then
    funky_motd "$(hostname)"
  elif [ -d /mnt/c/WINDOWS ]; then
    # WSL leaves you in the Windows directory by default rather than your home.
    cd

    funky_motd "arcade"
  fi
}
Enter fullscreen modeExit fullscreen mode

Arcade1 is the name of my gaming PC.

This is kinda what it looks like in use:

Screenshot of a terminal with large colourful representations of host names

Specifically, I'm using toilet or figlet to display the "future" typeface, and filtering that through lolcat to create the colour effects.

If any of these programs aren't installed, it falls back to the next best thing it can do. Worst case, you get a regularly echoed hostname.

There's a little bit of magic with shasum to ensure that the random seed for lolcat's colour gradient stays consistent - so in the example above, I always see the same colours for Arcade whenever I log in, and the same for my Macbook1. This is a neat trick you can use to keep a different colour shell prompt or background or anything you like on a per-host basis. Anything to make it more difficult to type something into the wrong window!

Arcade2 is the name of my gaming PC, if you're curious.


  1. Ok, there's a fudge in there because my work Macbook has a hostname like "EDLA0692BSI.local" for Important Corporate Reasons and that's not what I want to see when I look at my terminal. 

  2. All my personal machines are all named after computers from TV and film. What do you mean you don't recognise this one? 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK