SSHbash -c 'cmd' ,为什么第一行输出丢失?

SSHbash -c 'cmd' ,为什么第一行输出丢失?

为什么我会丢失 shell 命令输出? (在本例中通过 ssh 连接到 Ubuntu Raspberry Pi)

$ ssh [email protected] bash -l -c 'echo 111'

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

^^^ 不打印 111。看起来第一行丢失了:

$ ssh [email protected] bash -l -c 'echo 111 && echo 222'

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.


222

(-l 没有区别)

它在主机上运行良好:

pi@raspberrypi:~ $ bash -c 'echo 111'
111

答案1

由于引用错误,您会丢失输出。

ssh [email protected] bash -l -c 'echo 111'

这里有一个调用,bash要求执行echo.剩余的参数111已提供bash但未使用。 (结果是一个空行。)

您可能想要的是以下替代方案之一。

ssh [email protected] 'echo 111'                 # Shell executing echo

ssh -t [email protected] 'echo 111'              # Interactive shell (with `.bashrc`)  executing echo

ssh [email protected] 'bash -l -c "echo 111"'    # Shell calling login shell (`.bash_profile`) to execute echo

相关内容