基于网络IP的ProxyJump ssh动态应用
我在私有网络中有一个私有 git 服务器,可以通过代理跳转盒访问。
问题:当我在本地网络上时,不需要代理,因为我可以直接连接到盒子。
两种情况都使用相同的主机名。
问题:有没有办法根据我是否在 git 主机本地网络上动态地将 ProxyJump 配置应用到 ssh 命令?
I have a private git server in a private network, that is accessible via a proxy jump box.
Problem: When I'm on the local network the proxy isn't needed as I can directly connect to the box.
Both situations use the same hostname.
Question: Is there a way to dynamically apply the ProxyJump configuration to the ssh command based on wether or not I'm on the network local to the git host?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Match exec
指令检查您的网络并在~/.ssh/config
中启用 ProxyJump语法如下:
示例:
You can use
Match exec
directive to check your network and enable ProxyJump in your~/.ssh/config
Here is the syntax:
Example:
您希望完成的是所谓的堡垒主机配置。下面的配置使用 SSH 的
Match
指令与nc
命令相结合来检测与远程主机的连接。如果在本地连接到目标主机,则不使用ProxyJump,否则,到远程主机的连接将通过堡垒主机建立连接。注意:这适用于 SSH v 7.3 及更高版本,并要求堡垒主机上的 ssh 守护程序启用了 AllowTcpForwarding。
您需要像这样配置您的
~/.ssh/config
:请注意,
Match
指令之后的所有指令都将根据Match< 的真实性执行/code> 指令。因此,您可以决定在
Match
指令之前(始终映射端口)或之后(仅在 Match 为 true 时映射端口)使用LocalForward
指令转发端口。~/.ssh/config
非常通用且功能强大。此外,此配置还允许您的主机指令和映射在任何连接管理应用程序中工作,甚至通过简单地调用ssh remote_host
在命令行上工作。它还可以移植到任何使用 ssh 的主机(Windows、Linux、MacOS 等)(特定于操作系统的nc
命令选项除外)。此方法还允许使用配置文件中的注释更好地记录事情,这在某些连接管理器中是不可能的。甚至可能有一种方法可以使用三元运算符来使用操作系统的正确选项来调用 nc 命令,但我还没有尝试过。
我用这种方法替换了一个非常大且复杂的仅 iTerm2 配置,以便我可以在需要时轻松地从 MacOS 来回迁移到 Linux。这很有用,因为我的连接管理器 (iTerm2) 现在仅用于颜色管理和其他特定于 UI 的选项。
有关详细信息,请参阅 https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts#Passing_Through_One_or_More_Gateways_Using_ProxyJump
What you are looking to accomplish is a called a Bastion Host configuration. The configuration below uses SSH's
Match
directive combined with thenc
command to detect connectivity to the remote host. If you are connected locally to the target host, no ProxyJump is used, otherwise, the connection to the remote host passes through the bastion host to establish the connection.Note: This works on SSH v 7.3 and above and requires that the ssh daemon on the bastion host have AllowTcpForwarding enabled.
You need to configure your
~/.ssh/config
like so:Note that all directives after the
Match
directive will be executed based on the truthiness of theMatch
directive. So you may decide to forward ports using theLocalForward
directive before theMatch
directive (always map the port) or after (only map the port if Match is true).~/.ssh/config
is extremely versatile and powerful. Also this configuration allows your host directives and mappings to work in any connection management application or even on the command line by simply callingssh remote_host
. It is also portable (except for OS-specificnc
command options) to any host that uses ssh (Windows, Linux, MacOS, etc). This method also allows for documenting things much better using comments in the config file which isn't possible in some connection managers.There might even be a way to use a ternary operator to call the
nc
command using the right options for the OS, but I haven't tried it.I replaced a very large and complicated iTerm2-only configuration with this method so that I can easily migrate back and forth from MacOS to Linux when I need to. This is useful since my connection manager (iTerm2) is now only used for color management and other UI-specific options.
For more info see https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts#Passing_Through_One_or_More_Gateways_Using_ProxyJump