我想知道是否有人知道一种方法来捕获和识别进入终端的write命令。我尝试使用script -f,然后使用tail -f跟踪while循环上的输出,但是因为我跟踪的终端不会启动write,所以没有输出。不幸的是,我没有根权限,不能玩脚本或屏幕转储,想知道是否有人知道实现这一点的方法?
示例:
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发布于 2011-06-01 04:51:58
我想不出任何明显的方法来做到这一点。这就是为什么。
您的shell接收来自某个终端设备的输入,并将其输出发送到某个终端设备:
+-----------+
| |
| bash |
| |
+-----------+
^ |
| |
| v
+----------------------+
| some terminal device |
+----------------------+当write写入终端时,它会将数据直接发送到同一终端设备。它不会接近你的外壳:
+-----------+ +-----------+
| | | |
| bash | | write |
| | | |
+-----------+ +-----------+
^ | |
| | |
| v v
+----------------------+
| some terminal device |
+----------------------+因此,为了能够捕获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 |
+----------------------+现在您可以看到,到原始终端设备的write绕过了所有内容,就像上面这个简单的例子一样:
+-----------+
| |
| 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 |
+----------------------+如果在这里将数据写入新终端的从机端,您将看到显示的输出,因为它将出现在script看到的主机端的数据流中。您可以使用script内的shell中的tty命令找到新pty的名称。
不幸的是,这对write没有帮助,因为您可能无法通过write连接到它:您的登录会话与原始终端相关联,而不是与新终端相关联,并且write可能会抱怨您没有登录。但是如果你例如echo hello >/dev/pts/NNN,你会看到它确实出现在script输出中。
https://stackoverflow.com/questions/6178619
复制相似问题