记录Bash交互,分别保存STDIN,STDOUT [英] Record Bash Interaction, saving STDIN, STDOUT seperately
问题描述
所以我想记录我的bash交互,我知道我可以使用脚本或 ttyrec .除了我要比他们拥有更多的功能.分别保存输入(即STDIN)和输出(即STDOUT).
So I want to record my bash interaction, which I know I can do with script, or ttyrec. Except I want one feature more than they have. Save input (i.e STDIN), and output (i.e. STDOUT) separately.
类似(我键入了第一个"Hello World!")的东西,除了 script
当然需要一个 [file]
arg,而不是两个:
So something like (where I typed the first "Hello World!"), except of course script
takes one [file]
arg, not two:
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
的正常输出(我不想要,需要将其分开):
So I want a program like script
which saves STDIN & STDOUT separately. Since currently, this would be the normal output of script
(which I do not want, and need seperated):
Script started
user@pc:~$ paste > textfile.txt
Hello World!
user@pc:~$ cat textfile.txt
Hello World!
user@pc:~$ exit
exit
Script done
这是否存在,或者这可能吗?
Does this exist, or is this possible?
请注意 paste
命令的用法,因为我曾考虑过根据 user @ pc:〜$
过滤输出文件,但就我而言(如>粘贴
),将无法正常工作.
Note the usage of paste
command, since I had considered filtering the ouput file based on user@pc:~$
, but in my case (as with paste
) this would not work.
推荐答案
空
空被打包用于各种linux发行版(它是 empty-expect
在ubuntu上).
empty
empty is packaged for various linux distributions (it is empty-expect
on ubuntu).
- 打开两个终端
- 终端1 :运行
empty -f -i in.fifo -o out.fifo bash
- 终端1 :运行
tee stdout.log< out.fifo
- 终端2 :运行
stty -icanon -isig eol \ 001;tee stdin.log> in.fifo
- 在终端2 中键入命令,在终端1 中查看输出
- 使用
stty icanon isig -echo
修复终端设置 - 使用
exec 2> stderr.log
将stderr与stdout分开记录 - 完成后,
退出
bash shell;这两个tee
命令都将退出
- 使用
- open two terminals
- terminal 1 : run
empty -f -i in.fifo -o out.fifo bash
- terminal 1 : run
tee stdout.log <out.fifo
- terminal 2 : run
stty -icanon -isig eol \001; tee stdin.log >in.fifo
- type commands into terminal 2, watch the output in terminal 1
- fix terminal settings with
stty icanon isig -echo
- log stderr separately from stdout with
exec 2>stderr.log
- when finished,
exit
the bash shell; bothtee
commands will quit
- fix terminal settings with
其他一些选择:
Some other options:
您可以尝试 peekfd
(属于 psmisc
软件包的一部分).它可能需要以root身份运行:
You could try peekfd
(part of the psmisc
package). It probably needs to be run as root:
peekfd -c pid fd fd ... > logfile
其中pid是要附加的过程, -c
表示也要附加到子级,而 fd
是要观察的文件描述符列表(基本上是 0
, 1
, 2
).还有许多其他选项可调整输出.
where pid is the process to attach to, -c
says to attach to children too, and fd
are list of file descriptors to watch (basically 0
, 1
, 2
). There are various other options to tweak the output.
日志文件将需要进行后期处理以符合您的要求.
The logfile will need postprocessing to match your requirements.
在 unix stackexchange 上,已建议使用 SystemTap 工具.但是,配置并非易事,您仍然必须编写一个将stdin和stdout分开的模块.
Over on unix stackexchange, use of the SystemTap tool has been proposed. However, it is not trivial to configure and you'll still have to write a module that separates stdin and stdout.
sysdig 和使用 LD_PRELOAD ,您可以包装诸如write(2).
Using LD_PRELOAD, you can wrap lowlevel calls such as write(2).
您可以在 strace
或 ltrace
下运行Shell,并记录传递给系统和库函数(例如write)的数据.需要大量的后处理:
You can run your shell under strace
or ltrace
and record data passed to system and library functions (such as write). Lots of postprocessing needed:
ltrace -f -o ltrace.log -s 10000000000 -e write bash
修补ttyrec
ttyrec.c只有500行相当简单的代码,并且看起来很容易打补丁以使用多个日志文件.
patch ttyrec
ttyrec.c is only 500 lines of fairly straightforward code and looks like it would be fairly easy to patch to use multiple logfiles.
这篇关于记录Bash交互,分别保存STDIN,STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!