Recording User Activity with a Script

by mike on January 3, 2011

Using Variables in Scripts
The purpose of this script is to log the commands and output of a user so you have an accurate record of all activity.  One problem that you find if you depend upon logs with users and sudo is that sudo will not log the stdout nor the stderr.

# Capture keystrokes of a user and log

TIMESTAMP=$(date +%m%d%y%H%M%S)
HOST=$(hostname|cut -f1 -d.)

# Set Prompt
export PS1=”[$LOGNAME:$HOST]@”‘$PWD> ‘

chmod 600 ${LOGDIR}/${LOGFILE}

script ${LOGDIR}/${LOGFILE}
chmod 400 ${LOGDIR}/${LOGFILE}

Analysis of the Script
The bash shell is the shell to use with this script.

TIMESTAMP=$(date +%m%d%y%H%M%S)
This line creates a variable (the date followed by month,day,year,hour,minute,second).  Variables are symbolic names for memory in which you can assign values, as well as read the contents or manipulate the contents. The advantage of a variable is that once it is assigned you can use it over and over.  When you create variables it is important not to place spaces around the “=” sign. It is important to start and end your variables so the shell can tell where the variable ends, that is why you see examples of variable with ( ).   Note there must be a space after “date”.

Here is the name of the log, note the time stamp on the end.

HOST=$(hostname|cut -f1 -d.)
HOST is a variable that is created to indicate the machine logs that will be accessed.  It is created by two commands with the output of one piped into the second command.  The command hostname will print out the hostname of the computer the user is on.  That hostname could be a single hostname or it could be a Fully Qualified Domain Name (FQDN).
The hostname is piped into a second command with the “|” symbol which takes the output of one command and sends it to the second command.  So when you create the variable HOST the command is run and sent to the second command cut.  cut, as the name implies, is used to cut and display selected information from a text file or text input.  Think of it as something that will take a vertical slice of a text file, and send it to the output of your choice.  There are two ways to specify where you want to begin and end the slice.  You can specify it either by a starting and an ending character, or by fields.  To specify your “slice” by fields, you’ll need to use both the -d and -f switches.  The -d switch will specify the delimiter,  the character that separates the fields, in this case a dot.  That’s so that cut will know where each field begins and ends.   The -f switch will specify which fields you want to look at.  So the command you see with cut will take the first field and separate it from the other information that will be appended by a “.”.


If you wanted the see the first three fields of the hostname, FQDN, the script would be written like this:
HOST=$(hostname|cut -f1-3 -d.)

The variable $LOGDIR is created by determining the location of the log file after the “=”.  You can place the log wherever it is convenient.


Here the $LOGFILE variable is created by using three previously created variables, separated by a “.”, note the brackets.

The command touch creates an empty file that can be used by the information that is recorded.  The “/” separates the two variables which have been determined by the text above in the script.

export PS1=”[$LOGNAME:$HOST]@”‘$PWD> ‘


Shantanu Oak January 3, 2011 at 7:44 am

This is very useful. But is there any way to run the script automatically when I log-in to the shell? I tend to forget to run the script more often.

Shantanu Oak January 4, 2011 at 7:21 am

Answering my question, It seems that I need to edit the bash profile file.
vi ~/.bash_profile

mike January 4, 2011 at 12:12 pm

The other option is to run a script that detects your login and then initiates the script. Sometimes having too much happen at login can cause problems.

indiajoe January 4, 2011 at 8:24 am

For running it each time you login..
Call the script from .bashrc or .profile of your home directory..

Dave R January 4, 2011 at 3:11 pm

$ whatis script
script (1) – make typescript of terminal session

Comments on this entry are closed.

Previous post:

Next post: