Record Bash Interaction, saving STDIN, STDOUT seperately

烈酒焚心 提交于 2020-12-05 11:49:25

问题


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.

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

So input.txt looks like:

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

And output.txt looks like:

Hello World!
exit

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?

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.


回答1:


empty

empty is packaged for various linux distributions (it is empty-expect on ubuntu).

  1. open two terminals
  2. terminal 1 : run empty -f -i in.fifo -o out.fifo bash
  3. terminal 1 : run tee stdout.log <out.fifo
  4. terminal 2 : run stty -icanon -isig eol \001; tee stdin.log >in.fifo
  5. 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; both tee commands will quit
  6. stdout.log and stdin.log contain the logs

Some other options:

peekfd

You could try peekfd (part of the psmisc package). It probably needs to be run as root:

peekfd -c pid fd fd ... > logfile

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.

SystemTap and similar

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 and bpftrace also look interesting.

LD_PRELOAD / strace / ltrace

Using LD_PRELOAD, you can wrap lowlevel calls such as write(2).

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

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.



来源:https://stackoverflow.com/questions/54771786/record-bash-interaction-saving-stdin-stdout-seperately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!