19
5

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コマンドを.ssh/configのhostで補完させよう

Last updated at Posted at 2018-01-26

はじめに

.ssh/configが長すぎていつもgrepしている人へってのを書いたら、もっとバリ便利な方法がありました!

.bash_profileにsshコマンド用のBash-Completionを設定

.bash_profileに下記を追加してやりましょう。

~/.bash_profile
# 下記を追加
function _compreply_ssh(){
  COMPREPLY=(`cat ~/.ssh/config | grep -i -e '^host' | cut -d " " -f 2 | grep -E "$2"`)
}
complete -F _compreply_ssh ssh

使用例

例えばこんな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         ****
... 略)

こんな感じで、補完してくれます。

コマンド
$ ssh dev-【tabキーを叩く】
dev-front-hoge  dev-backoffice-hoge   dev-api-hoge    dev-ci-hoge

おわりに

have a good ssh life!

19
5
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
19
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?