验证 ssh 配置中的命令 shell (~/.ssh/config)

验证 ssh 配置中的命令 shell (~/.ssh/config)

因此,我为我的用户提供了一个 ssh 配置文件,使用Match exec它来获取一些 IP 匹配项:

Match exec "echo %h | grepcidr 192.168.12.0/20  &>/dev/null "
  User         ubuntu
  ProxyCommand          ssh my-bastion -W %h:%
  IdentityFile          ~/.ssh/target.pem

这在我的主机上运行良好,但我试图在安装了 openssh-client 的容器上启动它,然而 ssh 连接失败,因为它认为命令已经成功(即使我没有针对网络),所以它会尝试通过堡垒

debug1: Executing command: 'echo my.host.com | grepcidr 192.168.12.0/20 &>/dev/null '
debug3: command returned status 0

在我的主机上它返回

debug1: Executing command: 'echo my.host.com | grepcidr 192.168.12.0/20 &>/dev/null '
debug3: command returned status 1

这应该是容器 ssh 命令返回的内容

我一直在挖掘,结果发现容器中的 ssh 命令是使用启动的,sh -c而在我的主机上启动的 ssh 命令是,bash -c 我可以通过在命令行上进行测试并检查命令的返回来确认这是原因echo $?

有什么方法可以指定仅使用 ssh 命令bash

我可以SHELL在命令行开头覆盖环境变量

SHELL=/bin/bash ssh [email protected]

但我的目标是在所有这些之后使用 ansible,所以我想验证所有 SSH 的 shell。

答案1

您可以指定以 开头的替代命令bash -c,或者放弃 Bashism(即&>stdout-and-stderr 重定向)。

使用Match exec类似 C 函数的东西system(),因此由运行时系统(C 库)决定提供哪个 shell。在大多数 Unix 系统上,它会转换为sh -c加上整个命令行。您无法更改此部分。

相关内容