Linux下跟踪记录每个用户对主机服务器进行的操作

参考:https://www.cnblogs.com/haochuang/p/7680773.html

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值