Linux

ssh 配置:ProxyCommand 的替代品?

  • July 20, 2021

在我的幫助下,ProxyCommand我設置了一些 ssh 收藏夾以便於使用:

host some_server
   hostname some_server
   port 22
   user some_user
   IdentityFile /home/user/.ssh/id_rsa
   ProxyCommand ssh frontserver1 -W %h:%p

host frontserver1
   hostname frontserver1.url.tld
   port 22
   user some_user
   IdentityFile /home/user/.ssh/id_rsa

今天frontserver1有很長的停機時間,但我也可以通過frontserver2或連接frontserver3。但是,我將不得不重新設置一切some_server_via_front2,依此類推。這將導致我想要訪問的每個 Intranet 伺服器(有很多)都有 n 個條目,其中 n 是前端伺服器的數量。

有沒有更簡單的方法?

我可以設置替代品ProxyCommand嗎?

類似的東西:如果ProxyCommand ssh frontserver1 -W %h:%p無法到達,那麼去ProxyCommand ssh frontserver2 -W %h:%p,然後frontserver3,……

鑑於ssh_config手冊上的內容:

 ProxyCommand
         Specifies the command to use to connect to the server.  The com-
         mand string extends to the end of the line, and is executed using
         the user's shell `exec' directive to avoid a lingering shell
         process.

您應該能夠使用 shell 的邏輯 OR 運算符,因此:

host some_server
   hostname some_server
   port 22
   user some_user
   IdentityFile /home/user/.ssh/id_rsa
   ProxyCommand ssh frontserver1 -W %h:%p || ssh frontserver2 -W %h:%p || ssh frontserver3 -W %h:%p

host frontserver1
   hostname frontserver1.url.tld
   port 22
   user some_user
   IdentityFile /home/user/.ssh/id_rsa
   ConnectTimeout 5

host frontserver2
   hostname frontserver1.url.tld
   port 22
   user some_user
   IdentityFile /home/user/.ssh/id_rsa
   ConnectTimeout 5

host frontserver3
   hostname frontserver1.url.tld
   port 22
   user some_user
   IdentityFile /home/user/.ssh/id_rsa
   ConnectTimeout 5

我冒昧地ConnectTimeout為每個代理主機添加了一個指令,這樣最多需要 15 秒才能最終通過列表中的第三台主機失敗,而不是n倍主機數量乘以預設值您主機上的 TCP 超時設置恰好是。

引用自:https://unix.stackexchange.com/questions/427754