autossh 的默认监控端口是什么?

autossh 的默认监控端口是什么?

自动SSH命令有以下-M选项:

   -M port[:echo_port]

specifies the base monitoring port to use. Without the echo port, this port
and the port immediately above it ( port + 1) should be something nothing else
is using. autossh will send test data on the base monitoring port, and receive
it back on the port above. For example, if you specify "-M 20000", autossh will
set up forwards so that it can send data on port 20000 and receive it back on
20001. 

或者,您可以使用环境变量指定相同的行为AUTOSSH_PORT

我的问题如下:

如果既未-M指定选项也未指定环境变量,则使用哪个端口进行监控?或者禁用了该行为?文档不清楚。

答案1

如果未-M指定选项或环境变量,则使用哪个端口进行监控?

在我对 Kubuntu 客户端的几次测试中,随机的高端口的使用范围约为(我的估计)30000-60000。比较:临时端口。我的意思是第一个(port)是随机选择的,第二个就在它上面(port+1)。

-M如果没有适当的参数(autossh -M -- …autossh -M foo …)则立即退出;它不会打印任何错误,但退出状态为1

autossh -M 0 …确实禁用了该功能。

但…

我尝试分析源代码,但未发现这种随机性。然后我发现了这一点:

$ type autossh
autossh is /usr/bin/autossh
$ file /usr/bin/autossh
/usr/bin/autossh: POSIX shell script, ASCII text executable
$ head -n 4 /usr/bin/autossh
#!/bin/sh
# little wrapper to choose a random port for autossh, falling back to $fallback_port

fallback_port="21021"
$ tail -n 1 /usr/bin/autossh
exec /usr/lib/autossh/autossh "$@"
$ file /usr/lib/autossh/autossh
/usr/lib/autossh/autossh: ELF 64-bit LSB executable, …

这意味着在 Kubuntu 中,我有一个包装器,如果未指定-Mnor ,它会随机选择一个端口AUTOSSH_PORT。我不会在这里发布完整的代码,但我已经阅读过它:如果包装器在 42 次尝试中无法随机命中一对未使用的端口(即portport+1),则它最终会尝试2102121022;如果这些端口无法使用,包装器将退出并出现错误。

如果包装器成功,它会将第一个端口导出为AUTOSSH_PORT并运行实际的autossh( /usr/lib/autossh/autossh)。此可执行文件需要 -MAUTOSSH_PORT,否则它将无法运行。

我的最终答案是:没有默认监控端口香草 autossh。您必须设置AUTOSSH_PORT或使用-M。这很不方便,因此需要包装器。您的 Linux 中可能有一个,也可能没有。某些版本的 Linux 可能使用包装器来设置和使用固定的默认端口。

答案2

没有默认端口,因为可以任何远程系统上未使用的端口,但它也必须是开放的。然而,我认为它没有任何“合理”的用例,正如文档中所述这里你还可以使用 SSH 自己的保持活动选项:

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 5000:localhost:3306 [email protected]

如果服务器 90 秒内没有响应,则会“断开连接”,然后 AutoSSH 会自动重新连接。

相关内容