0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ssh で接続するとランダムで背景色を変更する

Last updated at Posted at 2023-07-06

最近ボケてて、ローカルのつもりでリモートを操作してあれれ? となることがたびたびあった。
よく使うリモートは背景などを設定しているが、使い捨ての設定などでも常に背景色を変更したい。

  • ターミナルエミュレーター固有の設定は使用せず、なるべく標準の技術を使用する
  • ssh コマンドに対してエリアスを設定して、操作に透過性を
  • 色はランダムで決定
  • ssh 終了時に復元する
  • 視認性を下げないように、背景の輝度を低く

環境

  • GNOME Terminal 3.44.0
    • Ubuntu Linux 22.04 LTS GUI での標準ターミナル

であるが、xterm プロトコルを使用しているのでターミナルの種類に依存しないはず・・・だけれどもプロトコルサポート状況によってはダメなものもあるかも。

sshrndcolor.sh

「ターミナルの情報をエスケープシーケンスで取得する」
https://qiita.com/nanbuwks/items/e691c3fd5f3f9a835a10

を用いて、以下のように作った。

restore_settings=$(stty -g) 
stty -icanon -echo
echo -en "\033]11;?\033\\"
read -d R -n 23 BGCOLOR
stty "$restore_settings"
BGCOLOR=`echo "$BGCOLOR" | tail -c +2`
OLDBGCOLOR=`echo "$BGCOLOR" | awk '{print substr($0, 11, 2) substr($0, 14, 2) substr($0, 19, 2)} '`
echo "$OLDBGCOLOR"
NEWCOLOR=`python3 -c "import random;print('{:06x}'.format(random.randint(0,100)*65536+random.randint(0,100)*256+random.randint(0,100)))"`
echo -e "\033]11;#${NEWCOLOR}\a"
ssh $@
echo -e "\033]11;#${OLDBGCOLOR}\a"

2024/5/14へんこ:
Ubuntu の初期設定に沿って、9行目の pythonpython3 に変更しました。

使用方法

  • chmod 755 sshrndcolor.sh
  • /usr/local/bin にコピー
  • $ echo alias ssh=/usr/local/bin/sshrndcolor.sh >> ~/.bashrc
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?