小智 11
您也可以尝试使用ProxyCommand选项。
它使您能够控制用于连接到服务器的命令;听起来很麻烦,但我还没有发现任何问题。
从ssh_config文档:
ProxyCommand
Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a
lingering shell process.
In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. The command can be basically
anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i
somewhere. Host key management will be done using the HostName of the host being connected (defaulting to the name typed by the user). Setting the command to “none” disables
this option entirely. Note that CheckHostIP is not available for connects with a proxy command.
This directive is useful in conjunction with nc(1) and its proxy support. For example, the following directive would connect via an HTTP proxy at 192.0.2.0:
ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p
Run Code Online (Sandbox Code Playgroud)
假设您有一个命令或脚本可以敲入您的路径: knock.sh <host> <port1 port2 ... portX>
并且您想连接到某个主机: myhost.net
您可以将以下内容添加到您的 ~/.ssh/config
Host thehost
HostName myhost.net
ProxyCommand bash -c 'knock.sh %h <port1 port2 ... portX>; nc %h %p'
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式连接到您的主机: ssh user@thehost
您还应该能够直接从命令行使用该选项:
ssh -o ProxyCommand="bash -c 'knock.sh %h <port1 port2 ... portX>; nc %h %p'" user@myhost.net
如果程序默认将数据写入标准输出,它会被隐藏,如果您需要查看它,请-v在ssh.
还要注意,ssh在执行连接之前必须执行敲门,因此完成连接所需的总时间会更大。
我有一个启用了端口转发的当前配置,但还没有问题。
mvd*_*vds 10
正如所接受的答案中那样,将所有流量作为 ProxyCommand 提供的另一种方法是在 ~/.ssh/config 中nc添加一行,该行仅在连接之前有效地执行您的命令:Match host/execknock
Match host thehost exec "knock -d 500 %h 1234 5678"
Match host thehost.domain.com exec "knock -d 500 %h 1234 5678"
Run Code Online (Sandbox Code Playgroud)
该行实际上除了执行之外没有做任何其他事情knock,因为该指令后面没有任何内容。
更新
为了使敲击有条件(并防止延迟和大量冗余的防火墙规则),请在敲击之前使用 netcat 进行探测:
Match host thehost exec "nc -zw 3 %h 22 || knock -d 500 %h 1234 5678"
Match host thehost.domain.com exec "nc -zw 3 %h 22 || knock -d 500 %h 1234 5678"
Run Code Online (Sandbox Code Playgroud)
该-w 3标志用于指定探测的超时时间为 3 秒。
更新以考虑阿德里安的评论
| 归档时间: |
|
| 查看次数: |
5010 次 |
| 最近记录: |