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?

Terminalの背景色を ssh 接続先に合わせて変更する自作コマンド

Last updated at Posted at 2021-06-07

概要

本番環境に ssh 接続した後、ローカル環境と間違えて誤ったコマンドを打たないように Terminal の背景色を動的に変更する

設定

1. ~/bin ディレクトリを作成する
2. ~/.zprofileexport PATH="$HOME/bin:$PATH" を追加する
3. ~/bin/sshh ファイルを作成し以下記載

sshh
#!/bin/bash

set_term_bgcolor(){
  local R=$1
  local G=$2
  local B=$3
  /usr/bin/osascript <<EOF
tell application "iTerm"
  tell the current window
    tell the current session
      set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
    end tell
  end tell
end tell
EOF
}

function tab-reset() {
   NAME="Default"
   echo -e "\033]50;SetProfile=$NAME\a"
}

trap "tab-reset" INT EXIT # exitで背景色を戻す
if [[ "$2" == prd ]]; then
  set_term_bgcolor 100 0 0
elif [[ "$2" == stg ]]; then
  set_term_bgcolor 0 0 100
else
  set_term_bgcolor 0 100 0
fi

ssh $1

4. $ chmod 755 ~/bin/sshh で実行権限を付与

使い方

$ sshh [sshの引数] [prd または stg または 引数なし]

# production 環境に接続(背景色が赤)
$ sshh xxxx@xxxx prd

# staging 環境に接続(背景色が青)
$ sshh xxxx@xxxx stg

# その他(背景色が緑)
$ sshh xxxx@xxxx
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?