如何使用单个Proxy命令配置多个跳转

如何使用单个Proxy命令配置多个跳转

我的~/.ssh/config文件为

Host head
  User Marry
  HostName xxx.xx.xxx.xx

Host machine1
  User Marry
  HostName xxx.xx1.xxx.xx
  IdentityFile /home/Marry/ssh_head_keys/id_rsa
  ProxyCommand  ssh head nc %h %p 2> /dev/null

Host machine2
  User Marry
  HostName xxx.xx2.xxx.xx
  IdentityFile /home/Marry/ssh_head_keys/id_rsa
  ProxyCommand  ssh head nc %h %p 2> /dev/null

Host machine3
  User Marry
  HostName xxx.xx3.xxx.xx
  IdentityFile /home/Marry/ssh_head_keys/id_rsa
  ProxyCommand  ssh head nc %h %p 2> /dev/null

Host machine4
  User Marry
  HostName xxx.xx4.xxx.xx
  IdentityFile /home/Marry/ssh_head_keys/id_rsa
  ProxyCommand  ssh head nc %h %p 2> /dev/null

但手动设置更多机器会很繁琐,我如何设置一个实例,例如

Host machine*
  User Marry
  HostName xxx.xx*.xxx.xx
  IdentityFile /home/Marry/ssh_head_keys/id_rsa
  ProxyCommand  ssh head nc %h %p 2> /dev/null

因为主机名地址仅在数字上有所不同。

更新根据 Tagwint 的建议,我得到:

ssh_exchange_identification: Connection closed by remote host

答案1

您可以使用ProxyJump命令来执行此操作:

Host machine1 machine2 machine3
  ProxyJump head

如果需要,您可以通过两个主机进行双跳,如下所示:

Host machine1 machine2 machine3
  ProxyJump head,jump

或者匹配域中的所有内容,如下所示:

Match host "*.example.com" 
  ProxyJump head,jump

您甚至可以根据您当前的 IP 地址执行此操作:

Match host "*.example.com" !exec "ifconfig | grep -q ' 172.16.1.40 '"
  ProxyJump head,jump.example.com

答案2

如果你可以像这样安排dns解析

xxx.xx1.xxx.xx machine1
xxx.xx2.xxx.xx machine2
xxx.xx3.xxx.xx machine3

(使用 DNS 服务器配置,或使用 /etc/hosts 文件中的条目)

然后 .ssh/config 中的最后一个实例将不包含主机名条目:

Host machine*
  User Marry
  IdentityFile /home/Marry/ssh_head_keys/id_rsa
  ProxyCommand  ssh head nc %h %p 2> /dev/null

相关内容