问HN:如何从原始PTY会话中提取Shell命令?(Rewindtty)
项目网址:https://github.com/debba/rewindtty
我一直在开发 rewindtty,这是一个用 C 语言编写的轻量级终端会话录制和回放工具。它的工作方式类似于 script/scriptreplay,但输出结构化的 JSON,并包含一个基于浏览器的播放器,用于回放终端会话,支持时间控制、快进、书签等功能。
到目前为止,我一直是逐条命令地录制会话,分别捕获每个 shell 命令及其输出。这使得分析会话和按命令索引变得简单。
然而,我刚刚引入了一种新的交互模式,它的行为更像传统的 script:它通过 PTY 实时录制原始终端输入输出,捕获每个输入或显示的字符,包括控制序列。
这对于实现真实感和完整的会话保真度(例如,像 htop、vim、REPL 等交互式工具)非常有帮助,但这也使得命令检测变得更加困难——我不再在 shell 层面拦截输入。
我的问题是:如何从这个原始的 PTY 流中提取实际的命令?
我知道这很棘手,但我想知道:
- 有没有人尝试解析 ANSI 流以重建命令边界?
- 是否可以实时挂钩到 shell(bash、zsh 等)以拦截命令?
- 是否有 shell 选项或审计功能可以与原始捕获并行利用?
- 有没有我应该关注的相关文献或库?
我很想听听其他人在录制、分析或回放 shell 会话方面的做法。任何见解或建议都将非常有帮助。
查看原文
Project URL: https://github.com/debba/rewindtty<p>I've been working on rewindtty, a lightweight terminal session recorder and replayer written in C. It works like script/scriptreplay, but outputs structured JSON and includes a browser-based player for replaying terminal sessions with timing, scrubbing, bookmarks, and more.<p>Until now, I was recording sessions command-by-command, capturing each shell command and its output separately. That made it easy to analyze sessions and index them by command.<p>However, I just introduced a new interactive mode, which behaves more like traditional script: it records raw terminal I/O in real-time via a PTY, capturing every character typed or displayed, including control sequences.<p>This is great for realism and full session fidelity (e.g. interactive tools like htop, vim, REPLs), but it makes command detection much harder — I'm no longer intercepting input at the shell level.<p>My question is: how can I extract actual commands from this raw PTY stream?<p>I'm aware it's tricky, but I'm wondering:<p><pre><code> Has anyone tried parsing the ANSI stream to reconstruct command boundaries?
Is it possible to hook into the shell (bash, zsh, etc.) in real-time to intercept commands?
Are there shell options or audit features that can be leveraged in parallel to raw capture?
Any prior art or libraries I should look at?
</code></pre>
I'd love to hear how others have approached this — either for recording, analyzing, or replaying shell sessions. Any insights or directions would be super helpful.