1.记录操作信息
这个脚本需添加至/etc/profile,后执行source /etc/profile
### add by jim
# file name record
history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /tmp/operation ]
then
mkdir /tmp/operation
chmod 777 /tmp/operation
fi
if [ ! -d /tmp/operation/${LOGNAME} ]
then
mkdir /tmp/operation/${LOGNAME}
chmod 700 /tmp/operation/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +%Y%m%d`
# file=`find /tmp/operation/${LOGNAME}/ |awk '/'$DT'$/'`
file=/tmp/operation/${LOGNAME}/${USER_IP}-susworld.$DT
if [ -e "$file" ]
then
export HISTFILE="/tmp/operation/${LOGNAME}/${USER_IP}-susworld.$DT.tmp"
tmpfile=/tmp/operation/${LOGNAME}/${USER_IP}-susworld.$DT.tmp
if [ -e "$tmpfile" ]
then
cat /tmp/operation/${LOGNAME}/${USER_IP}-susworld.$DT.tmp >> $file
fi
else
export HISTFILE="/tmp/operation/${LOGNAME}/${USER_IP}-susworld.$DT"
fi
rm -f /tmp/operation/${LOGNAME}/${USER_IP}-susworld.$DT.tmp
chmod 600 /tmp/operation/${LOGNAME}/*susworld* 2>/dev/null
### end add by jim
2.查询操作信息
配合下面这个shell,进行查询,使用起来更加方便。
#!/bin/sh -
# filename getrecord
user=
time=
while [ $# -gt 0 ]
do
case $1 in
-u|--user) user=$2
shift 2
;;
-t|--time) time=$2
shift 2
;;
-*) echo "$0:$1 Wrong Options!" >&2
shift
exit 0
;;
--) break
;;
*) break
;;
esac
done
if [ -z "$user" ]
then
user=${LOGNAME}
fi
if [ -z "$time" ]
then
time=`date +%Y%m%d`
fi
file=`find /tmp/operation/$user/ |awk '/'$time'$/'`
cat $file
此段shell通过接收用户及时间参数来查看某用户的历史操作记录,如:
# getrecord -u root -t 20180923 --查看root在09月23号对服务器做的记录
如果不给定参数,shell将提取当前登陆用户及当日时间来进行查询.
也可以将此shell添加到bin中,直接使用命令来查询:
# mv getrecord /bin
# getrecord