记录Bash次交互,分别保存STDIN、STDOUT

Record Bash Interaction, saving STDIN, STDOUT seperately

所以我想记录我的 bash 互动,我知道我可以用 script, or ttyrec 做到这一点。除了我比他们更想要一个功能。分别保存输入(即 STDIN)和输出(即 STDOUT)。

所以类似(我输入第一个 "Hello World!" 的地方),当然 script 需要一个 [file] arg,而不是两个:

user@pc:~$ script input.txt output.txt
Script started
user@pc:~$ paste > textfile.txt
Hello World!
user@pc:~$ cat textfile.txt 
Hello World!
user@pc:~$ exit
Script done

所以 input.txt 看起来像:

user@pc:~$ paste > textfile.txt
Hello World!
user@pc:~$ cat textfile.txt 
user@pc:~$ exit

output.txt 看起来像:

Hello World!
exit

所以我想要一个像 script 这样的程序,它分别保存 STDIN 和 STDOUT。目前,这将是 script 的正常输出(我不想要,需要分开):

Script started

user@pc:~$ paste > textfile.txt
Hello World!
user@pc:~$ cat textfile.txt 
Hello World!
user@pc:~$ exit
exit

Script done

这是否存在,或者这可能吗?

注意 paste 命令的用法,因为我考虑过根据 user@pc:~$ 过滤输出文件,但在我的情况下(与 paste 一样)这不起作用。

empty 被打包用于各种 linux 发行版(在 ubuntu 上是 empty-expect)。

  1. 打开两个终端
  2. 终端 1 : 运行 empty -f -i in.fifo -o out.fifo bash
  3. 终端 1 : 运行 tee stdout.log <out.fifo
  4. 终端 2 : 运行 stty -icanon -isig eol [=15=]1; tee stdin.log >in.fifo
  5. 终端 2 中输入命令,在 终端 1 中观察输出
    • 使用 stty icanon isig -echo
    • 修复终端设置
    • 使用 exec 2>stderr.log
    • 将 stderr 与 stdout 分开记录
    • 完成后,exit bash shell;两个 tee 命令都会退出
  6. stdout.logstdin.log 包含日志

一些其他选项:

peekfd

您可以尝试 peekfdpsmisc 包的一部分)。它可能需要 运行 作为 root:

peekfd -c pid fd fd ... > logfile

其中 pid 是要附加到的进程,-c 表示也要附加到子进程,fd 是要监视的文件描述符列表(基本上是 01, 2).还有其他各种选项可以调整输出。

日志文件需要进行后处理以满​​足您的要求。

SystemTap 和类似的

已提议使用 unix stackexchange, use of the SystemTap 工具。 但是,配置并非易事,您仍然需要编写一个模块来分隔标准输入和标准输出。

sysdig and bpftrace看起来也很有趣

LD_PRELOAD/strace/ltrace

使用 LD_PRELOAD,您可以包装低级调用,例如 write(2)。

您可以 运行 您的 shell 在 straceltrace 下并记录传递给系统和库函数(例如写入)的数据。需要大量的后处理:

ltrace -f -o ltrace.log -s 10000000000 -e write bash

补丁 ttyrec

ttyrec.c 只有 500 行相当简单的代码,看起来很容易打补丁以使用多个日志文件。