テックブログ

技術ネタ

motdとFIGlet

こんにちは、技術部のfです。
今回は motd と FIGlet について少し触ってみたいと思います。

まずmotdとは「message of the day」の略で、
ユーザがログインした際にメッセージを表示させることができるファイルです。

Red Hat Customer Portal や Ubuntu manuals などにも
motdの使い方が紹介されています。

How to change the message of the day (motd) on Red Hat Enterprise Linux? – Red Hat Customer Portal

Ubuntu Manpage: motd – 今日のお知らせ(message of the day)

/etc/motd というファイルにメッセージを記述することで、
ログインシェルが起動される前にメッセージを表示させることができます。

それでは早速試してみたいと思います。
今回使用した環境は下記の通りです。

$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

/etc/motd ファイルの存在を確認します。

# ls -l /etc/motd
-rw-r--r--. 1 root root 0  9月 10  2018 /etc/motd

0byteなので中身は空ですが、ファイルは存在しています。
まずはシンプルに「Hello, World!」と表示させる為、
/etc/motd を編集します。

# vi /etc/motd
# cat /etc/motd
Hello, World!

別のユーザでSSHログインしてみます。

$ ssh *****
Enter passphrase for key '*****': 
Hello, World! 
Last login: Tue Mar 31 --:--:-- 2020 from ***.***.***.***
$

色々と情報は伏せていますが、SSH Keyのパスフレーズを聞かれた後、
「Last login:」の情報が出力される前に /etc/motd に記述した一文が
表示されているのがわかります。
(わかりやすくする為に色を付けていますが、実際は特に色付けはされていません)

たとえばサーバの用途(WEBサーバやDBサーバなど)を書いたりすると、
SSHした時に対象のサーバが間違っていないか、少しわかりやすくなるかもしれません。

もちろん、下記のように複数行のメッセージを表示させることもできます。

$ ssh *****
Enter passphrase for key '*****':
#############
#Test Server#
#############
Last login: Tue Mar 31 --:--:-- 2020 from ***.***.***.***
$

一行だけだと「Last login:」のメッセージなどに埋もれてしまって
あまり目立たないですが、記号などで囲ってみると少し存在感が増しました。
(フォントの関係でちょっと見づらいですが)

せっかくなので、「FIGlet」という、テキストをロゴのようなアスキーアートに
変換することができるツールも使ってみたいと思います。

CentOS系の場合はepelリポジトリから、yumやdnfでインストールすることができます。
まずはinfoでfigletパッケージの情報を見てみます。

# dnf info figlet
利用可能なパッケージ
名前 : figlet
バージョン : 2.2.5
リリース : 18.20151018gita565ae1.el8
Arch : x86_64
サイズ : 132 k
ソース : figlet-2.2.5-18.20151018gita565ae1.el8.src.rpm
リポジトリー : epel
概要 : A program for making large letters out of ordinary text
URL : http://www.figlet.org/
ライセンス : BSD and MIT
説明 : FIGlet prints its input using large characters (called "FIGcharacters") made
: up of ordinary screen characters (called "sub-characters"). FIGlet output is
: generally reminiscent of the sort of "signatures" many people like to put at
: the end of e-mail and UseNet messages. It is also reminiscent of the output of
: some banner programs, although it is oriented normally, not sideways.

epelリポジトリも導入済みの為、そのままdnfでインストールしてみます。

# dnf install figlet

依存関係が解決しました。
================================================
パッケージ アーキテクチャー バージョン リポジトリー サイズ
================================================
インストール:
figlet x86_64 2.2.5-18.20151018gita565ae1.el8 epel 132 k
トランザクションの概要
================================================
インストール 1 パッケージ

ダウンロードサイズの合計: 132 k
インストール済みのサイズ: 672 k
これでよろしいですか? [y/N]: y

(略)

インストール済み:
figlet-2.2.5-18.20151018gita565ae1.el8.x86_64

完了しました!

特に依存関係で導入されるパッケージもなく、figletのインストールが完了しました。

manコマンドでマニュアルを見ると、-vオプションを指定することでfigletのバージョンと
コマンドの使い方を見ることができるようです。

$ man figlet
-------------------------------------------------------------------
-v
-I infocode
These options print various information about FIGlet, then exit. If several of these options are given on the command line,
only the last is executed, and only after all other command-line options have been dealt with.
-v prints version and copyright information, as well as a ``Usage: ...'' line. -I prints the information corresponding to the given infocode in a consistent, reliable (i.e., guaranteed to be the same in future releases) format. -I is primarily intended to be used by programs that use FIGlet. infocode can be any of the following.
-------------------------------------------------------------------

manのマニュアルでももちろんコマンドの使い方は確認できますが、
まずは-vでfigletコマンドを実行してみます。

$ figlet -v
FIGlet Copyright (C) 1991-2012 Glenn Chappell, Ian Chai, John Cowan,
Christiaan Keet and Claudio Matsuoka
Internet: info@figlet.org Version: 2.2.5, date: 31 May 2012
FIGlet, along with the various FIGlet fonts and documentation, may be
freely copied and distributed.
If you use FIGlet, please send an e-mail message to info@figlet.org.
The latest version of FIGlet is available from the web site,
http://www.figlet.org/
Usage: figlet [ -cklnoprstvxDELNRSWX ] [ -d fontdirectory ]
[ -f fontfile ] [ -m smushmode ] [ -w outputwidth ]
[ -C controlfile ] [ -I infocode ] [ message ]

バージョンとコピーライト、使い方が出てきました。
試しに何もオプションを指定しないで実行してみます。
(ブログに使用しているフォントの関係上、表示崩れが発生してしまう為、出力結果は画像にしています)

$ figlet netassist

これだけでも指定した文字が変換されました。

次に-fオプションを試してみます。
manコマンドでは次のように説明されています。

-f fontfile
Select the font. The .flf suffix may be left off of fontfile, in which case FIGlet automatically appends it.
FIGlet looks for the file first in the default font directory and then in the current directory, or, if fontfile was given as a full pathname, in the given directory.
~略~

拡張子「.flf」のファイルを指定することができるようです。
インストールしたfigletパッケージに含まれているflfファイルを見てみます。

$ rpm -ql figlet | grep flf
/usr/share/figlet/banner.flf
/usr/share/figlet/big.flf
/usr/share/figlet/block.flf
/usr/share/figlet/bubble.flf
/usr/share/figlet/digital.flf
/usr/share/figlet/ivrit.flf
/usr/share/figlet/lean.flf
/usr/share/figlet/mini.flf
/usr/share/figlet/mnemonic.flf
/usr/share/figlet/script.flf
/usr/share/figlet/shadow.flf
/usr/share/figlet/slant.flf
/usr/share/figlet/small.flf
/usr/share/figlet/smscript.flf
/usr/share/figlet/smshadow.flf
/usr/share/figlet/smslant.flf
/usr/share/figlet/standard.flf
/usr/share/figlet/term.flf

18個のフォントがfigletパッケージに含まれていました。
最初にデフォルトのフォントディレクトリを検索してくれるようなので、
念の為、-I2オプションでデフォルトのフォントディレクトリを確認してみます。
(-Iオプションの詳細は割愛しますが、manのマニュアルにはfigletに関する
詳細な情報を出力するオプション、とあります)

$ figlet -I2
/usr/share/figlet

先程確認したflfファイルの保存先と同じなので、ファイル名のみで指定してみます。

$ figlet -f block.flf netassist

先程とは異なるフォントで出力されました。
ただ、途中で改行されてしまい、あまり見栄えがよくありません。

Usageで[ -w outputwidth ]という幅を指定できそうなオプションが
ありましたので、マニュアルで内容を見てみます。

-w outputwidth
These options control the outputwidth, or the screen width FIGlet assumes when formatting its output.
FIGlet uses the outputwidth to determine when to break lines and how to center the output.
Normally, FIGlet assumes 80 columns so that people with wide terminals won't annoy the people they e-mail FIGlet output to.

デフォルトでは80列が指定されるようです。
tの一文字分を広げたいので、適当に100列で出力してみます。

$ figlet -f block -w 100 netassist

これで一行で出力することができました。
ちなみに-fでフォントを指定する場合、拡張子は入れなくても良いようです。

せっかくなので、figletパッケージに含まれていたフォントを全て見てみましょう。
ファイル名とfigletで変換した結果をまとめて出力してみます。

$ for i in $(rpm -ql figlet|grep flf);do echo $i;figlet -f $i -w 100 netassist;done

これ以外にも、figlet公式サイトの「font database」からフォントが配布されていますので、
font examples」で気に入ったフォントがあったらダウンロードして使うこともできるようです。

ロゴ的なものとは少し違いますが、
モールス符号に変換される「morse」なんてフォントもあるようです。

これまたせっかくなのでダウンロードして試してみたいと思います。

# cd /usr/share/figlet/
# wget http://www.figlet.org/fonts/morse.flf
# ls -l morse.flf
-rw-r--r-- 1 root root 2496 7月 5 2002 morse.flf

figletコマンドでmorseフォントを指定し、実行してみます。

$ figlet -f morse netassist
-. . - .- … … .. … -

いかがでしょうか。

イレギュラーな使い方だとは思いますが、モールス符号変換用として
使ってみるのもあり……かもしれません。

話戻って、figletで変換したものをmotdに書いてみました。

# figlet -f banner Test Server > /etc/motd

存在感がかなり増しました。
読んでほしいメッセージなどは目に付きやすそうです。

実運用で使うことはあまりないと思いますが、テストサーバなどでは
ちょっとした遊び心として設定してみると楽しいかもしれません。

この記事をシェアする

  • facebook
  • twitter
  • hatena
  • line
URLとタイトルをコピーする

実績数30,000件!
サーバーやネットワークなど
ITインフラのことならネットアシストへ、
お気軽にご相談ください