回应"写"消息

Ali*_*tin 7 bash

想知道是否有人知道如何捕获和识别write到终端的传入命令.我已经尝试使用script -f然后使用tail -f跟随while循环的输出,但因为我跟踪的终端没有启动write它不会输出.不幸的是我没有root权限,不能玩pts或screendump,想知道是否有人知道实现这个的方法?

例:

Terminal 1 $ echo hello | write Terminal 2

Terminal 2 $
Message from Terminal 1 on pts/0 at 23:48 ...
hello
EOF

*Cause a trigger from which I can send a return message
Run Code Online (Sandbox Code Playgroud)

Mat*_*ery 3

我想不出任何明显的方法来做到这一点。这就是为什么...

您的 shell 从某个终端设备接收输入并将其输出发送到某个终端设备:

   +-----------+
   |           |
   |   bash    |
   |           |
   +-----------+
         ^ |
         | |
         | v
      +----------------------+
      | some terminal device |
      +----------------------+
Run Code Online (Sandbox Code Playgroud)

write写入终端时,它直接将数据发送到同一终端设备。它不会靠近你的外壳:

   +-----------+     +-----------+
   |           |     |           |
   |   bash    |     |   write   |
   |           |     |           |
   +-----------+     +-----------+
         ^ |             |
         | |             |
         | v             v
      +----------------------+
      | some terminal device |
      +----------------------+
Run Code Online (Sandbox Code Playgroud)

因此,为了让您能够捕获 发送的内容write,您需要终端设备本身提供的一些钩子,并且我认为没有任何东西可以用来执行此操作。

那么它是如何script工作的,为什么它不捕获write输出呢?

script也无法连接到终端设备。它确实想将自己插入到 shell 和终端之间,但没有直接做到这一点的好方法。

所以,它创建了一个新的终端设备(伪终端,也称为“pty”)并在其中运行一个新的 shell。pty 由两侧组成:“主设备”(只是字节流)和“从设备”(看起来就像任何其他交互式终端设备一样)。

新的 shell 连接到从属端,并script控制主端 - 这意味着它可以将字节流保存到文件中,并在新 shell 和原始终端之间转发它们:

   +-----------+
   |           |
   |   bash    |
   |           |
   +-----------+
         ^ |
         | |
         | v
+-----------------+  <=== slave side of pty -- presents the interface of
| pseudo-terminal |                                an interactive terminal
+-----------------+  <=== master side of pty -- just a stream of bytes
         ^ |                      
         | v     
   +-----------+
   |           |
   |  script   |
   |           |
   +-----------+
         ^ |
         | |
         | v
      +----------------------+
      | some terminal device |
      +----------------------+
Run Code Online (Sandbox Code Playgroud)

write现在您可以看到原始终端设备的a绕过了所有内容,就像上面的简单情况一样:

   +-----------+
   |           |
   |   bash    |
   |           |
   +-----------+
         ^ |
         | |
         | v
+-----------------+  <=== slave side of pty -- presents the interface of
| pseudo-terminal |                                an interactive terminal
+-----------------+  <=== master side of pty -- just a stream of bytes
         ^ |                      
         | v     
   +-----------+     +-----------+
   |           |     |           |
   |  script   |     |   write   |
   |           |     |           |
   +-----------+     +-----------+
         ^ |             |
         | |             |
         | v             v
      +----------------------+
      | some terminal device |
      +----------------------+
Run Code Online (Sandbox Code Playgroud)

如果您将数据写入此处的终端的从机端,您看到输出显示出来,因为它将出现在主机端的数据流中,script可以看到。tty您可以使用shell 中的命令找到新 pty 的名称script

不幸的是,这对 没有帮助write,因为您可能无法做到write这一点:您的登录会话与原始终端相关联,而不是新终端,并且write可能会抱怨您尚未登录。但是如果例如echo hello >/dev/pts/NNN,您会看到它确实出现在script输出中。