2017-07-25 16 views
0

我的主機名列表開始WEB1,web2上,...,web200,web201.myserver.ltd等我可以使用通配符的bash別名SSH

我已經在的.ssh/config中使用:

Host * 
    User myuser 

和resolv.conf中:

search myserver.ltd 

因此,而不是使用ssh [email protected]我可以使用ssh web200

問題:有什麼辦法可以在我的.zshrc文件中設置通配符別名,因此當我鍵入web200時,它會執行ssh web200? (同樣適用於任何以webxxx開頭的服務器

我知道我可以通過爲每個webxxx服務器使用單獨的別名來設置它,但我正在尋找一條線(或幾條線)來實現這一點。

回答

2

隨着zsh中,你可以使用一個command_not_found_handler hook

command_not_found_handler() { 
[[ $1 = web* ]] && ssh "[email protected]" 
} 

然後:。

$ web200 
ssh: Could not resolve hostname web200: Temporary failure in name resolution 
zsh: command not found: web200 
+0

大,它的工作需要感謝您 –

+0

偉大的答案,這將在Bash工作也是如此。看到這篇文章:https://superuser.com/questions/787424/hook-into-command-not-found-handler-in-ubuntu – codeforester