3
4

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/configが長すぎていつもgrepしている人へ

Last updated at Posted at 2018-01-18

はじめに

サーバアプリエンジニアにしてもインフラエンジニアにしても、サービス数×サーバ数×環境数みたいな感じで果てしなく長くなった.ssh/configにうんざりしている人が多いのではないでしょうか?
そんなあなたに、ちょっとだけ幸せを運ぶ小技を紹介します。

追記)
...だったんだけど、**こっちのほうが100倍便利!**なのでこちらへ
sshコマンドを.ssh/configのhostで補完させよう

.bash_profileにaliasに設定

下記の短い魔法のaliasを.bash_profileあたりに書いてやりましょう。

~/.bash_profile
alias sshconfig='cat ~/.ssh/config | grep -i host | cut -d " " -f 2 | grep -E $@'

使用例

例えばこんなconfigを書いていた場合

.ssh/config
# hoge service(develop)
Host dev-front-hoge
    Hostname 192.168.10.10
    User         hogeop
Host dev-backoffice-hoge
    Hostname 192.168.10.11
    User         hogeop    
Host dev-api-hoge
    Hostname 192.168.10.12
    User         hogeop
Host dev-ci-hoge
    Hostname 192.168.10.13
    User         hogeop
    
# hoge service(production)
Host prd-front-hoge
    Hostname ******
    User         ****
    ProxyCommand ssh bastion-hoge -W %h:%p
... 略)
# fuga service(develop)
Host dev-front-fuga
    Hostname ******
    User         ****
... 略)

developのサーバ一覧を表示したい場合は
$ sshconfig dev
とコマンドすると

dev-front-hoge
dev-backoffice-hoge
dev-api-hoge
dev-ci-hoge
...

ってな感じで表示されます。
微妙に名前覚えてないなぁって場合も
$ sshconfig d.*f.*hoge
んな感じで、コマンドすれば

dev-front-hoge

と表示されます。
一つに絞れる場合は、
$ ssh `sshconfig d.*f.*hoge`
とコマンドすれば、すぐそのサーバにsshログインできますね!

このaliasでエンジェルカードくらいのちょっとした幸せを運んでくれたのではないでしょうか。

おわりに

どうでもいい話ですが、Macのsedって大文字小文字を区別しないってどうやるんでしょう(+_+)
下記だと怒られるんですよね。。

cat ~/.ssh/config | sed -e 's/^HOST *//i'
3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?