【问题标题】:Piping data to Linux program which expects a TTY (terminal)将数据传输到需要 TTY(终端)的 Linux 程序
【发布时间】:2011-05-13 03:20:29
【问题描述】:

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

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

【问题讨论】:

    标签: linux terminal pipe tty pty


    【解决方案1】:

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

    $ tty 
    /dev/pts/3
    $ echo | tty 
    not a tty
    $ echo | unbuffer tty 
    /dev/pts/11
    

    【讨论】:

    【解决方案2】:

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

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

    described here,对于使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-05
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2020-08-02
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多