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

时间:2019-02-19 17:24:12

标签: linux bash shell unix interaction

所以我想记录我的bash交互,我知道我可以使用scriptttyrec来进行交互。除了我要比他们拥有更多的功能。分别保存输入(即STDIN)和输出(即STDOUT)。

除了(script需要一个[file] arg,而不是两个参数之外,类似(我在其中键入了第一个“ Hello World!”)的东西:

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一样)。

>

1 个答案:

答案 0 :(得分:3)

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 \001; 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

您可以尝试peekfd(属于psmisc软件包的一部分)。它可能需要以root身份运行:

peekfd -c pid fd fd ... > logfile

其中pid是要附加的过程,-c表示也要附加到子级,而fd是要观察的文件描述符列表(基本上是012)。还有许多其他选项可调整输出。

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

SystemTap和类似的

unix stackexchange上,已经建议使用SystemTap工具。 但是,配置并非易事,您仍然必须编写一个将stdin和stdout分开的模块。

sysdigbpftrace看起来也很有趣。

LD_PRELOAD / strace / ltrace

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

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

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

修补程序ttyrec

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

相关问题