说,我~/.ssh/config
有几行:
Host machine1
User user
HostName machine1
ProxyCommand ssh server nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)
这工作正常,但问题是我有很多machines
:machine1, machine2, machine3, ...
那么如何在不手动复制相同类型的行的情况下设置所有这些行
des*_*ert 12
你可以这样做:
Host machine1 machine2 machine3
User user
ProxyCommand ssh server nc %h %p 2>/dev/null
Run Code Online (Sandbox Code Playgroud)
您只需要列出该Host
行中的主机,以空格分隔,HostName
如果它与您在该Host
行中给出的名称不同,则可以省略。请参阅ssh 配置中的多个类似条目 · U&L。
为了进一步简化它,有通配符*
并?
具有通常的含义,因此Host machine?
对于您的示例来说也是可能的。
如果您的主机名符合某个模式,您可以使用 SSH 的模式:
您可以在~/.ssh/config
. 来自man ssh_config
:
PATTERNS
A pattern consists of zero or more non-whitespace characters, ‘*’ (a
wildcard that matches zero or more characters), or ‘?’ (a wildcard that
matches exactly one character). For example, to specify a set of
declarations for any host in the “.co.uk” set of domains, the following
pattern could be used:
Host *.co.uk
The following pattern would match any host in the 192.168.0.[0-9] network
range:
Host 192.168.0.?
Run Code Online (Sandbox Code Playgroud)
因此,如果您想代理 中的所有内容*.example.com
,则在您的~/.ssh/config
中放置:
Host *.example.com
User user
ProxyCommand ssh server nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)
或者,使用ssh
自己的选项,您可以避免使用 netcat:
Host *.example.com
User user
ProxyCommand ssh -qW %h:%p server
Run Code Online (Sandbox Code Playgroud)
来自man ssh
:
-W host:port
Requests that standard input and output on the client be
forwarded to host on port over the secure channel. Implies -N,
-T, ExitOnForwardFailure and ClearAllForwardings.
Run Code Online (Sandbox Code Playgroud)