bash 通过 ssh 运行程序失败,并显示“打开终端时出错:未知”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30469813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:03:16  来源:igfitidea点击:

running a program through ssh fails with "Error opening terminal: unknown."

linuxbashparsingsshpipe

提问by Yuval Atzmon

When I try to execute a simple command through ssh, then it is successful. e.g.

当我尝试通过 ssh 执行一个简单的命令时,它就成功了。例如

#] ssh servername "echo abcd"
abcd
#] 

However, when I try the following command, it fails:

但是,当我尝试以下命令时,它失败了:

#] ssh servername  ~/htopmem.sh
Error opening terminal: unknown.
#] 

where the content of htopmem.sh is below. (inspired by the answer of Marwan Alsabbagh on htop output to human readable file)

htopmem.sh 的内容在下面。(灵感来自 Marwan Alsabbagh 对htop 输出到人类可读文件的回答)

#!/bin/bash
echo q | htop | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | ~/aha --black --line-fix | grep Mem | grep -E -o "[0-9]+/[0-9]+"

If I manually ssh to the server and run htopmem, then the execution is successful:

如果我手动ssh到服务器并运行htopmem,则执行成功:

#] ./htopmem.sh
6515/24021
#] 

any idea on how to make the "ssh servername ~/htopmem.sh" command work?

关于如何使“ssh servername ~/htopmem.sh”命令起作用的任何想法?

Thank you!

谢谢!

回答by Thomas Dickey

A plain sshcommand like that does not have a tty(terminal). Use the -toption to force sshto open the terminal on its way in.

ssh像这样的普通命令没有tty(终端)。使用-t选项强制ssh打开终端。

From the manual:

手册

-t

Force pseudo-tty 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 -toptions force tty allocation, even if ssh has no local tty.

-t

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

So this would work (better):

所以这会起作用(更好):

ssh -t servername  ~/htopmem.sh