问题原文
原帖链接 · fazy · CC BY-SA 3.0
I'm looking for a way to use specific CIDR blocks to match hosts in the SSH client configuration (usually ~/.ssh/config). For example, I have an entry to forward all traffic through a bastion host if the IP falls into a certain range, let's say 10.1.0.0/16:
host 10.1.*
proxycommand ssh -q bastion -W %h:%p
This works very well, but how about when I add some ranges that don't fit the dot notation exactly?
# doesn't work, unfortunately
host 10.2.0.0/18
proxycommand ssh -q bastion-foo -W %h:%p
host 10.2.64.0/18
proxycommand ssh -q bastion-bar -W %h:%p
Is there something in the manual that I've missed, or a clever scripting trick that would enable matching these host IP ranges?
社区高分解答
原帖链接 · Alex Stragies · CC BY-SA 4.0
You could use the very powerful Match directive with the grepcidr tool:
Match exec "grepcidr 10.2.0.0/18 <(echo %h) &>/dev/null"
Proxycommand ssh -q bastion-foo -W %h:%p
How this works:
%hin the commandline gets expanded to the hostname (see Tokens)<(echo %h)wraps this into a temporary "file"- Maybe use
<(dig +short %h)if working with hostnames - then use
grepcidrto match the hostname%hagainst a.b.c.d/e &>/dev/nullbecause we only care about the return code, not the output.
This needs the additional tiny grepcidr package, which should exist in your package manager.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
