Logging of every command & it's output
Posted 04-18-2008 at 03:58 AM by rameshj
Hi,
I have a requirement which need to log each and every command executed on the a linux/solaris/hpux machine console. I did this by using "script" command. For enable logging, I have put the following entry in the /etc/profile file.
function log_me {
DATE="/bin/date"
SCRIPT="/usr/bin/script -q"
COL="/usr/bin/col"
HOSTNAME=`hostname`
if [ -d "${LOGBASE}/${USERNAME}" ]; then
echo "yes dir founc"
TIMESTAMP="$( ${DATE} +%s )"000
NAME="${USERNAME}"
LOGFILE="${LOGBASE}/${USERNAME}/${USERNAME}-${TIMESTAMP}"
TARGET_FILE="${LOGBASE}/upload/${USERNAME}-${TIMESTAMP}.txt"
umask 077
[[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile
${SCRIPT} -q ${LOGFILE}
if [ -f "${COL}" ]
then
${COL} -bx < ${LOGFILE} > ${LOGFILE}.txt
if [ $? -eq 0 ]
then
rm -f ${LOGFILE}
fi
else
mv ${LOGFILE} ${LOGFILE}.txt
fi
RH=${REMOTEHOST}
if [ "${REMOTEHOST}" == "" ]
then
RH="Unknown Host"
fi
echo "#PMP_OUTPUT_SP_START:${RH}:${NAME}:${TIMESTAMP}:END#" >> ${TARGET_FILE}
cat ${LOGFILE}.txt >> ${TARGET_FILE}
rm -f ${LOGFILE}.txt
[[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
logout
fi
}
if [ -d "${LOGBASE}/${USERNAME}" ];then
log_me
fi
Every thing is working perfect.ie, if any user logged into the m/c using telnet the log file is created with commands and its outputs. But only problem is, I am not able to the login via xwindows if I put the above entry in the /etc/profile. The login screen repeatedly asking username/password. If I somehow able to get information in the /etc/profile about that login is an x-windows login, I may skip that login for triggering script command.
Kindly help me to resolve this issue.
Thanks,
Ramesh
I have a requirement which need to log each and every command executed on the a linux/solaris/hpux machine console. I did this by using "script" command. For enable logging, I have put the following entry in the /etc/profile file.
function log_me {
DATE="/bin/date"
SCRIPT="/usr/bin/script -q"
COL="/usr/bin/col"
HOSTNAME=`hostname`
if [ -d "${LOGBASE}/${USERNAME}" ]; then
echo "yes dir founc"
TIMESTAMP="$( ${DATE} +%s )"000
NAME="${USERNAME}"
LOGFILE="${LOGBASE}/${USERNAME}/${USERNAME}-${TIMESTAMP}"
TARGET_FILE="${LOGBASE}/upload/${USERNAME}-${TIMESTAMP}.txt"
umask 077
[[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile
${SCRIPT} -q ${LOGFILE}
if [ -f "${COL}" ]
then
${COL} -bx < ${LOGFILE} > ${LOGFILE}.txt
if [ $? -eq 0 ]
then
rm -f ${LOGFILE}
fi
else
mv ${LOGFILE} ${LOGFILE}.txt
fi
RH=${REMOTEHOST}
if [ "${REMOTEHOST}" == "" ]
then
RH="Unknown Host"
fi
echo "#PMP_OUTPUT_SP_START:${RH}:${NAME}:${TIMESTAMP}:END#" >> ${TARGET_FILE}
cat ${LOGFILE}.txt >> ${TARGET_FILE}
rm -f ${LOGFILE}.txt
[[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
logout
fi
}
if [ -d "${LOGBASE}/${USERNAME}" ];then
log_me
fi
Every thing is working perfect.ie, if any user logged into the m/c using telnet the log file is created with commands and its outputs. But only problem is, I am not able to the login via xwindows if I put the above entry in the /etc/profile. The login screen repeatedly asking username/password. If I somehow able to get information in the /etc/profile about that login is an x-windows login, I may skip that login for triggering script command.
Kindly help me to resolve this issue.
Thanks,
Ramesh