1

I'm trying to monitor command execution on a shell. I need to separate the input command, for example:

input: 
   ls -l /
output: 
   total 76
   lrwxrwxrwx   1 root root     7 Aug 11 10:25 bin -> usr/bin
   drwxr-xr-x   3 root root  4096 Aug 11 11:18 boot
   drwxr-xr-x  17 root root  3200 Oct 11 11:10 dev
   ...

Also, I want to do the same if I open another shell, for example, after connection through ssh to another server. I've been using script command to do this and it works just fine!

It logs all command input and output even if the shell changes (through ssh, or entering a msfconsole, for example).

Nevertheless, I found two main issues:

  1. For my project, I need to separate (using a decoder) each command from the rest, also it would be awesome to be able to separate command input and output, for example:
    cmd1. pwd  ---> /var/
    cmd2. echo "hello world" ---> "hello world"
    ....
  1. Sometimes the script command could generate an output with garbage due to shell special characters (for colors, for example) which I would like to filter out.

So I've been thinking about this and I guess I could create a simple script that read from the file written by "script" command and processed the data. Nevertheless, I'm not sure about what could be the best approach to do this.

I'm evaluating different solutions and I would like to know different proposals from the community. Maybe I'm losing something and you know a better tool rather than script command or have some idea I've not considered.

Best regards,

agc
  • 7,973
  • 2
  • 29
  • 50
spotHound
  • 320
  • 2
  • 15
  • 1
    check if [this](https://stackoverflow.com/questions/54771786/record-bash-interaction-saving-stdin-stdout-seperately) meets your requirement – confused genius Oct 11 '20 at 10:44
  • It's better [not to parse `ls`](https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead) if avoidable. – agc Oct 11 '20 at 16:01

1 Answers1

1

A useful util for distinguishing stdout from stderr is annotate-output, (install the "devscripts" package), which sends stderr and stdin both to stdout along with helpful little prefixes. For example, let's try counting characters of a file that exists, plus one that doesn't exist:

annotate-output wc -c /bin/bash /bin/nosuchshell

Output:

00:29:06 I: Started wc -c /bin/bash /bin/nosuchshell
00:29:06 E: wc: /bin/nosuchshell: No such file or directory
00:29:06 O: 1099016 /bin/bash
00:29:06 O: 1099016 total
00:29:06 I: Finished with exitcode 1

That output could be parsed separately using sed, awk, or even a tee and a few greps.

agc
  • 7,973
  • 2
  • 29
  • 50
  • I've been thinking about doing something just like that, redirecting console emulator STDIN and STOUT/STDERR to separate user input and output. But after doing some testing I've decided that this doesn't suit my purpose because it could derivate from a lot of issues with the processes themselves. The program that you propose, for example, seems pretty useful but may change the way users interact with the shell and I want to do all of this transparent to the user. Anyway, I appreciate your help, thank you very much. – spotHound Oct 14 '20 at 14:58