82
86

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

一撃必殺!踏み台を経由のssh接続、ついでに接続先毎にターミナルの背景色を変える。(iTerm)

Last updated at Posted at 2015-04-06

ざっくり

  • 踏み台サーバー経由でsshする時に面倒くさい(特にscp)
  • でも一撃で本番環境とかテスト環境とかssh出来ると楽ちん過ぎてオペミスが・・・
  • ターミナルの背景変えて緊張感を持たせる

多段sshを一撃で実施する

ssh時に読み込まれる設定ファイル(~/.ssh/config)にProxyコマンドを登録する事で解決出来る。

踏み台サーバーをbastion、接続したいサーバーをstageとしたサンプル。

~/.ssh/config
Host bastion-server                            # 名前は何でもOK。エイリアス。 ...[1]
    User         bastion-user                  # bastionサーバーに接続するユーザー名
    HostName     bastion-server.example.com    # bastionサーバーのhost名 or ip
    Port         10022                         # bastion踏み台サーバーのport番号(22の場合は省略可能)
    IdentityFile ~/.ssh/id_rsa_bastion         # bastion踏み台サーバーの秘密鍵

Host stage-server                              # 名前は何でもOK。エイリアス。
    User         stage-user                    # stageサーバーに接続するユーザー名
    HostName     stage-server.example.com      # stageサーバーのhost名 or ip
#    Port         22                            # 踏み台サーバーのport番号(22の場合は省略可能)
    IdentityFile ~/.ssh/id_rsa_stage           # stageサーバーの秘密鍵
    ProxyCommand ssh bastion-server -W %h:%p   # [1]で設定したエイリアスが生きる

これで一撃でstageサーバーにログイン出来る。

% ssh stage-server
% scp stage-server:/var/log/hoge.log ~/temp/.
% sftp stage-server

楽ちん。

複数接続先がある場合

bastion-server1bastion-server2prod-server
など複数踏み台がある場合も同様に書いていけば良い。

~/.ssh/config
Host bastion-server1
  User         bastion-user1
  HostName     bastion-server1.example.com

Host bastion-server2
  User         bastion-user2
  HostName     bastion-server2.example.com
  ProxyCommand ssh bastion-server1 -W %h:%p

Host prod-server
  User         prod-user
  HostName     prod-server.example.com
  ProxyCommand ssh bastion-server2 -W %h:%p

残念だったこと

これを設定すればアヒル(cyber duck)からも一撃でつながるかと思ったけどダメだった。
たぶん、鍵+パスワード認証のせいかな。

iTermの背景色を接続先毎に変更する

多分、改良すればデフォルトのターミナルやMac以外でも使える???

  • url(github)からssh-host-colorのコードをDL(コピペでもOK)する
  • パスの通ったディレクトリに配置する。
    実行権限付与も忘れずに。
  • if文に適当にホスト名を設定して、好きな色を設定する。

上記の~/.ssh/config を例にすると ssh-host-colorのif文はこんな感じに設定すると良い感じ?
色のセンスはお任せ。

# 踏み台サーバー
if [[ "$@" =~ bastion-server ]]; then
  set_term_bgcolor 0 0 40
# stageサーバー
elif [[ "$@" =~ stage-server ]]; then
  set_term_bgcolor 0 90 90
# 本番サーバー
elif [[ "$@" =~ prod-server ]]; then
  set_term_bgcolor 90 0 0
fi

以下のコマンドで背景色を変えつつ、ssh接続できる。
(exitすると元の色に戻る。サーバー側からの自動切断は・・・ごめんなさい。)

% ssh-host-color stage-server

ssh-host-colorのHeaderコメントにもあるように、シェルのプロファイルにエイリアスを追加すると便利。

% echo "alias ssh=~/bin/ssh-host-color" >> ~.zprofile
% source ~/.zprofile
% ssh stage-server

iTerm以外で実施する場合(が分からない)

デフォルトのターミナルでやりたい場合はtell application "iTerm"ブロックを消すか、tell application "teminal"みたいに変えれば大丈夫かも?

tell application "iTerm"ブロックを消す。
tell application "terminal"にする。
tell application "ターミナル"にする。
いずれもダメでした。

82
86
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
82
86

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?