ssh内带有多个命令的ssh

问题描述 投票:-1回答:2

当我尝试多个命令通过其他ssh内的ssh远程执行命令时,我得到了奇怪的结果。

来自man ssh:

-t

强制伪终端分配。这可以用来执行远程计算机上的任意基于屏幕的程序,这可能非常有用的,例如实施菜单服务时。多个-t选项强制tty分配,即使ssh没有本地tty。

如果我这样做

ssh -t root@host1 ssh root@host2 "cat /etc/hostname && cat /etc/hostname"

ssh -t root@host1 ssh -t root@host2 "cat /etc/hostname && cat /etc/hostname"

在两种情况下我都知道

host1
Connection to host1 closed.
host2
Connection to host2 closed.

我想要这个结果:

host1
host1
Connection to host1 closed.
Connection to host2 closed.

我想在ssh中使用ssh在同一服务器上运行所有命令。

如果我仅使用一个ssh,它将起作用:

ssh -t root@host1 "cat /etc/hostname && cat /etc/hostname"
host1
host1
Connection to host1 closed.
linux ssh tty
2个回答
0
投票

我知道了,但是我无法解释发生了什么。

ssh -t root@host1 "ssh -t root@host2 \"cat /etc/hostname ; cat /etc/hostname\""
host1
host1
Connection to host1 closed.
Connection to host2 closed.

0
投票

尝试:

这不是-t的工作方式。

您可以选择尝试:

 ssh root@host1 .....; ssh root@host2 ....

否则,请使用将同时在两台服务器上执行正常运行时间命令的PSSH:

pssh -h hostfile -i uptime
© www.soinside.com 2019 - 2024. All rights reserved.