views:

59

answers:

3

Basically I want to do a program almost like a keylogger. The thing is that I as network admin sometimes I don't remember what I did to a machine on certain case, or same times I make howto's and tutorials for linux. I want to record what have i done. So basically the idea of this program is: you type the name of the program, (I call it rat for the moment)

$ rat
Welcome everything from now on will be recorded
recording $ ls
file1 file2 file3
recording $ quit
Bye bye

Everything you do will go out to an xml file. Something like this

<?xml version='1.0' encoding='UTF-8' ?>
<rat>
 <command>
   <input>ls</input>
   <output>file1 file2 file3</output>
   <err><err>
 </command>
</rat>

i am doing some tests with fp_in = popen( input, "w"); and system, but first with popen i cant change directories and with "system i cant properly manage the input and output.

I was also checking if there is something I can do to bash like a plugin but haven't find any information.

At some points if feels like it I should create another shell (which is way beyond my current abilities) or fork bash sh. But it should been that complicated right. I am open to suggestion where to start. I am rusty with C, so I am reading again a lot of basic stuff.

With the xml file, later i was thinking on making a program to store this data and/or editing this data so i can create tutials and howto.

I can think of many ways of expanding this up to using printscreen so all the stored images go to a file you can upload to a server (for the moment i am glad to store the data). It could be a usefull tool.

ps. I do know this can be use for evil things too.

+1  A: 

Most shells have a script built-in which will simply record the text in- and out- from the command line. Not quite what you're looking for... To my surprise script is not a built in, which means it is a model for building what you want.

The script command does almost what you want: it simply records the text in- and out- from the command line.

If you make your prompt distinctive (so that you can reliably tell the difference between shell commands and everything else) you can post-process the output of script to achieve your goals. Alternately you can hack script to get it to emit the XML you're looking for.

dmckee
Here's a link to `script`: http://linux.die.net/man/1/script
bta
It seems like the shell thing right. I will have to investigate more, now i am thinking of hacking programs like midnight commander to understand how do they tackle this problem. They do similar things moving around directories.
Juan Diego
on a second look script does a lot of what i ask
Juan Diego
A: 

You can also try approaching this from a different angle. Instead of using a regular shell, connect to the machine using ssh or telnet and run your commands that way. Many ssh/telnet clients (PuTTY, for instance) have an option to log all console input and output during the session. You should be able to post-process this log to generate whatever type of logfile that you need.

Depending on your setup, you might not even have to use a second machine (you should be able to ssh into yourself).

bta
`screen` can also do logging.
Dennis Williamson
+5  A: 

There already exists the script command, which will record all input and output into the terminal, writing it into a transcript. I would recommend just using that, unless you have particular needs that it doesn't meet. Actually, the nicest version of script that I've seen has been the NetBSD version, so you may want to look into that if the Linux version doesn't meet your needs.

If you would like to write it yourself, instead of using system, I would recommend that you use fork/exec to create a single shell process, which you copy all input and output into. To get an idea of how this works, I'd recommend looking at the source code for an existing version of script.

Brian Campbell
script opens a psudo-terminal (the same as xterm, sshd, telnetd, ...) and then splices that to the terminal (possibly another psudo-terminal) that the user was already interacting with, and logs most everything that went on between the two terminals.
nategoose
Script does a lot of things, it is nice. It even logs the whitespaces and backspace. I dont think it can do the xml thing, but it is nice. It handles real time transaction thanks i think my question has been answered
Juan Diego