iTermの背景にsshログイン情報を表示
概要
iTerm2をsshクライアントツールで愛用していますが、たまに複数のサーバーへ接続しているとどこがなんなのか分からなくなる場合があります。
その場合、接続情報を可視化したら良いかと思いまして色々調べて改善した内容です。(まだ、課題点はあり〜〜〜)
いきなりソース
自分も色々なところのソースを利用したので一旦ソースを共有した方が良いかと思います。
「~/bin/ssh-change-bg.sh」を作成します。
#!/bin/bash # SSH with host name and IP address in background (only in iTerm.app) # First, check to see if we have the correct terminal! if [ "$(tty)" == 'not a tty' ] || [ "$TERM_PROGRAM" != "iTerm.app" ] ; then /usr/bin/ssh "$@" exit $? fi function __calculate_iterm_window_dimensions { local size=( $(osascript -e " tell application \"System Events\" tell application process \"iTerm2\" get size of window 1 end tell end tell" | tr ',' ' ') ) local w=$(( ${size[0]} - 15 )) h=$(( ${size[1]} - 44 )) echo "${w}x${h}" } # Console dimensions DIMENSIONS=$(__calculate_iterm_window_dimensions) BG_COLOR="#000000" # Background color FG_COLOR="#C68080" # Foreground color GRAVITY="NorthEast" # Text gravity (NorthWest, North, NorthEast, # West, Center, East, SouthWest, South, SouthEast) OFFSET1="20,10" # Text offset OFFSET2="20,80" # Text offset FONT_SIZE="60" # Font size in points FONT_STYLE="Normal" # Font style (Any, Italic, Normal, Oblique) # Font path FONT="$HOME/.bash/resources/SimpleLife.ttf" HOSTNAME=`echo $@ | sed -e "s/.*@//" -e "s/ .*//"` configInfo=`pcregrep -M 'Host '$HOSTNAME'([\s\S](?!Host ))+' ~/.ssh/config` if [ ! -z "$configInfo" ]; then RESOLVED_HOSTNAME=$HOSTNAME CONFIG_HOSTNAME=`echo -e "$configInfo" | grep -oEi 'hostname\s+(.+)' | sed -e "s/ \+/ /g" |cut -f2 -d" "` output=`dscacheutil -q host -a name $CONFIG_HOSTNAME` RESOLVED_IP=`echo -e "$output"|grep '^ip_address:'|awk '{print $2}'` RESOLVED_USER=`echo -e "$configInfo" | grep -oEi 'user\s+(\S+)' | sed -e "s/ \+/ /g" |cut -f2 -d" "`"@" else output=`dscacheutil -q host -a name $HOSTNAME` RESOLVED_HOSTNAME=`echo -e "$output"|grep '^name:'|awk '{print $2}'` RESOLVED_IP=`echo -e "$output"|grep '^ip_address:'|awk '{print $2}'` fi function set_bg { local tty=$(tty) osascript -e " tell application \"iTerm2\" tell current session of current window set background image to \"$1\" end tell end tell" } on_exit () { if [ ! -f /tmp/iTermBG.empty.png ]; then convert -size "$DIMENSIONS" xc:"$BG_COLOR" "/tmp/iTermBG.empty.png" fi set_bg "/tmp/iTermBG.empty.png" rm "/tmp/iTermBG.$$.png" } trap on_exit EXIT convert \ -size "$DIMENSIONS" xc:"$BG_COLOR" -gravity "$GRAVITY" -fill "$FG_COLOR" -style "$FONT_STYLE" -pointsize "$FONT_SIZE" -antialias -draw "text $OFFSET1 '${RESOLVED_HOSTNAME:-$HOSTNAME}'" \ -pointsize 30 -draw "text $OFFSET2 '${RESOLVED_USER}${RESOLVED_IP:-}'" -alpha Off \ "/tmp/iTermBG.$$.png" set_bg "/tmp/iTermBG.$$.png" /usr/bin/ssh "$@"
実装権限を与えます。
% chmod +x ~/bin/ssh-change-bg.sh
~/.bash_profileに以下を追記してsshコマンドでssh-change-bg.shが実行されるようにします。
alias ssh='~/bin/ssh-change-bg.sh'
~/.bash_profileを読み込みましょう。
% source ~/.bash_profile
ソースを見るとわかる方もいるかと思いますが、「convert」を使用するために「ImageMagick」をインストールする必要があります。
ImageMagickのインストール
思ったより簡単です。
% brew install imagemagick
一応converterコマンドの参考URLです。
- ImageMagick: Convert, Edit, Or Compose Bitmap Images
- ImageMagick: Tools (how to use ImageMagick) www.imagemagick.org
利用可能範囲
iTerm2のPaneサイズの取得不可
現在のソースの「__calculate_iterm_window_dimensions」部分ですが、Window(Or Tab)のpixelサイズしか取得できない状態です。Apple Script?的な仕様上paneとかsessionにはSizeの属性がないためです。
sttyコマンドでライン数やカラム数の取得できますが、それをpixelで取得する方法がまだ見つかってないです。
That's stty size | cut -d" " -f1 for the height/lines and stty size | cut -d" " -f2 for the width/columns
現在右上に情報が見えるようにしていますが、左上に表示するようにすれば解決されるかなと思います。ただ、邪魔でした。(ㅠㅠ)
~/.ssh/configの参照
~/.ssh/configで定義されたパターン(自分が使っているパターン)を読み込んで情報として利用するようにしています。
こんな感じです。大文字小文字、英数字とかが関係すると思います。
Host raspi-of-local HostName 172.16.221.18 IdentityFile ~/.ssh/id_rsa User pi
上記のソースはここら辺です。
configInfo=`pcregrep -M 'Host '$HOSTNAME'([\s\S](?!Host ))+' ~/.ssh/config` if [ ! -z "$configInfo" ]; then RESOLVED_HOSTNAME=$HOSTNAME RESOLVED_IP=`echo -e "$configInfo" | grep -oE 'HostName\s+([0-9\.]+)' | sed -e "s/ \+/ /g" |cut -f2 -d" "` RESOLVED_USER=`echo -e "$configInfo" | grep -oE 'User\s+(\S+)' | sed -e "s/ \+/ /g" |cut -f2 -d" "`"@" else ... fi
課題
やっぱりpaneのサイズを正確に取得したいですね。ww