24

我在 Linux 中有一个程序,如果它的标准输入/标准输出不是 TTY(终端设备),它会拒绝运行。是否有一个易于使用的工具可以创建 PTY,使用新创建的 TTY 启动程序,并通过 stdin/stdout 复制所有数据?

用例不是交互式的,而是脚本。我正在寻找最轻量级的解决方案,最好不要创建 TCP 连接,并且不需要安装太多其他工具和库。

4

2 回答 2

37

unbuffer,expect 的一部分(sudo apt-get install expect-dev在 Ubuntu Lucid 上),可以欺骗程序认为它已连接到 TTY。

$ tty 
/dev/pts/3
$ echo | tty 
not a tty
$ echo | unbuffer tty 
/dev/pts/11
于 2010-11-20T16:48:50.523 回答
1

您可以socat为此使用:echo your stdin strings | socat EXEC:"your_program",pty STDIO >/stdout_file

例如bashecho ls | socat EXEC:'bash',pty STDIO >/tmp/ls_out

或者如此处所述,对于使用以下命令运行的程序docker

# Run the docker task, here bash, in background
docker run -it --rm --name test ubuntu &
# Send "ls -la" to the bash running inside docker
echo 'ls -la' | socat EXEC:'docker attach test',pty STDIN
# Show the result
docker logs test
于 2021-09-10T08:30:32.893 回答