Ask HN: 如何从原始 PTY 会话中提取 Shell 命令? (Rewindtty)
2 分•作者: debba•10 个月前
项目URL:https://github.com/debba/rewindtty
我一直在开发rewindtty,这是一个用C语言编写的轻量级终端会话录制和回放工具。它的工作方式类似于script/scriptreplay,但输出结构化的JSON,并包含一个基于浏览器的播放器,用于回放终端会话,具有时间轴、快进、书签等功能。
到目前为止,我一直在逐个命令地录制会话,分别捕获每个shell命令及其输出。这使得分析会话并按命令进行索引变得容易。
然而,我刚刚引入了一个新的交互模式,它的行为更像传统的script:它通过PTY实时录制原始终端I/O,捕获每个键入或显示的字符,包括控制序列。
这对于真实性和完整的会话保真度(例如,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.