加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

ssh – 如何处理“由于stdin不是终端,因此不会分配伪终端.”

发布时间:2020-12-14 01:06:02 所属栏目:Linux 来源:网络整理
导读:ssh -t remotehost vim /tmp/x.txt 我知道我可以运行如上所述的命令. 但我希望能够在远程计算机上运行任何本地bash代码.出于这个原因,我想调用远程’bash -s’,以便可以处理任何本地bash代码. ssh -t remotehost 'bash -s' vim /tmp/x.txt 但是,上面的例子显
ssh -t remotehost vim /tmp/x.txt

我知道我可以运行如上所述的命令.

但我希望能够在远程计算机上运行任何本地bash代码.出于这个原因,我想调用远程’bash -s’,以便可以处理任何本地bash代码.

ssh -t remotehost 'bash -s' <<< vim /tmp/x.txt

但是,上面的例子显示“由于stdin不是终端,因此不会分配伪终端”.有没有办法让ssh通过stdin获取本地bash代码并通过远程’bash -s’运行它?谢谢.

解决方法

ssh -t remotehost 'bash -s' <<< vim /tmp/x.txt

您正在获取“伪终端将不会被分配…”消息,因为当ssh进程的标准输入不是TTY时,您正在使用单个-t选项运行ssh.在这种情况下,ssh专门打印该消息. documentation for -t说:

-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine,which can be very useful,e.g. when implementing menu services. Multiple -t options force tty allocation,even if ssh has no local tty.

-t命令行选项与ssh配置选项RequestTTY相关:

RequestTTY
Specifies whether to request a pseudo-tty for the session. The argument may be one of: no (never request a TTY),yes (always request a TTY when standard input is a TTY),force (always request a TTY) or auto (request a TTY when opening a login session). This option mirrors the -t and -T flags for ssh(1).

单个-t相当于“RequestTTY yes”,而其中两个相当于“RequestTTY force”.

如果您希望远程命令与TTY一起运行,则指定-t两次:

ssh -tt remotehost 'bash -s' <<< vim /tmp/x.txt
or
ssh -t -t remotehost 'bash -s' <<< vim /tmp/x.txt

ssh将为远程系统分配一个TTY,它不会打印该消息.

如果在远程系统上运行的命令不需要TTY,则可以保留-t选项:

ssh remotehost 'bash -s' <<< vim /tmp/x.txt

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读