3

Hi there!

I need to capture all command, stdout and stderr without any addition to the commands.
I mean, that I have to save/store the output of these:

  1. $ echo 'test'
  2. $ ls -l
  3. $ cat some_file

commands.file

  1. alex@bender:/pwd$ echo 'test'
  2. alex@bender:/pwd$ ls
  3. alex@bender:/pwd$ cat some_file
  4. alex@bender:/pwd$ fsdfsdfsd

stdout.file

  1. test
  2. test.txt some_file
  3. some file content

stderr.file

_4. fsdfsdfsd

as for first part of question, I found script, which use a trap and before evaluating the command it stores the command to log. But what about storing output? repeat: this should be solved without any additions to command. I mean, after running script, I dont need to write, for example first command ls > log.file I want see it without ">log.file"

I write some python script, it works like command wrapper.

if name == 'main':
user = os.getlogin()
machine_name = os.uname()[1]

stdinbuff = open('commands', 'w')                                             
stdoutbuff = open('outs', 'w')                                                

command_string = 1                                                            
while command_string != '0':                                                  

    a = user + '@' + machine_name + ":" + os.getcwd() + '>';                  
    a = a.replace('/home/', '~/')                                             
    print a,                                                                  
    command_string = raw_input()                                              
    command_args = command_string.split()                                     

    proc = subprocess.Popen(command_args, stdout=subprocess.PIPE)             
    (out, err) = proc.communicate()                                           
    print out                                                                 

    stdinbuff.write(command_string+'\n')                                      
    stdoutbuff.write(out+'\n')                                                

stdinbuff.close()                                                                 
stdoutbuff.close()

but in this wrapper, I can't use autocomplete, like in bash, and this script can't store all what I want.

Maybe, someone could tell me, what I have to do?

1

Maybe the screen tool can do the trick for you with the screen logging function. Just simply start screen in commandline and with the keys CTRL-a + H (first Combination of CTRL + a and then a capital H with SHIFT + H, just to clarify) everything will be logged in the file screenlog.0. The directory where the logfile is found depends on where you started screen, i. e. if you started screen in your $HOME then the logfile will be found there.

  • Great solution! You may want to clarify, that A is just the key a, but H refers to a capital H. So the combination effectively becomes CTRL-A and then SHIFT-H. – Tim Oct 13 '13 at 11:57
  • Thanks noggerl, but, may be you know another way to do this, because at some time i need to patch output on the fly.. – Alex Bender Oct 15 '13 at 12:56
  • The only solution for this i've got in my mind is start a second screen. In that screen you can start a script which patches the output of the other screen at runtime (i. e. in python, maybe a shellscript could do). Maybe I can help you a little more when you tell me what you're trying to do. @Tim: thanks for your hint, i've edited my solution with your suggestions. – noggerl Oct 15 '13 at 15:18
1

This can be done with python pty module: using a pseudo-terminal to record all input and output of a terminal session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.