set_system_history.sh

set_system_history.sh

#!/bin/bash

# jihongrui@outlook.com

#主要功能:
#
#可以记录哪个ip和时间(精确到秒)作了哪些命令
#
#通过用户登录时候,重新定义HISTFILE
#
#HISTFILE文件名包含登录用户名,ip,登录时间(精确到秒)等
#
#这样即使相同的用户从不同ip、在不同的时间登录都会被记录
#
#可以记录每条命令的开始执行时间

if [[ $UID -eq 0 ]];then
    cat << EOF >> /etc/profile
# jihongrui add 
#history
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
USER_IP=\`who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g'\`
HISTDIR=/var/log/.hist
if [ -z \$USER_IP ]
then
USER_IP=\`hostname\`
fi
if [ ! -d \$HISTDIR ]
then
mkdir -p \$HISTDIR
chmod 777 \$HISTDIR
fi
if [ ! -d \$HISTDIR/\${LOGNAME} ]
then
mkdir -p \$HISTDIR/\${LOGNAME}
chmod 300 \$HISTDIR/\${LOGNAME}
fi
export HISTSIZE=409600
DT=\`date +%Y%m%d_%H%M%S\`
export HISTFILE="\$HISTDIR/\${LOGNAME}/\${USER_IP}.hist.\$DT"
chmod 600 \$HISTDIR/\${LOGNAME}/*.hist* 2>/dev/null
EOF
source /etc/profile
fi

python_file='Linux_history.py'

if [[ -f ${python_file} ]];then
    path_dir=$(cd `dirname ${0}` && pwd)

#Auto Crontab
abs_file="${path_dir}/${0}"
log_file="${abs_file}.log"

if [[ `grep ${abs_file} /etc/crontab|wc -l` -eq 0  ]] && [[ $UID -eq 0 ]]
then
    echo "59 */1 * * * root python "${abs_file}" &>> "${log_file}" ">> /etc/crontab
fi
fi
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 为何叫做 shell ? shell prompt(PS1) 与 Carriage Return(CR) 的关系?...
    Zero___阅读 3,182评论 3 49
  • 安装依赖库(支持Python 3、Lua、Ruby) sudo apt-get install libncurse...
    Cyfeng阅读 2,146评论 0 2
  • Ubuntu 常用命令大全 查看软件 xxx 安装内容 #dpkg -L xxx 查找软件 #apt-cache ...
    guiwuzhe阅读 2,601评论 0 14
  • 我妈是个善良、勤俭、吃苦耐劳、凡事将就、啰嗦、脾气暴躁又疑心病重的人。呵呵,是不觉得她有太多面了?确实,她就是一个...
    Danne008阅读 229评论 0 0
  • 1、新建Axure PR文件,取名“Axure制作首页轮播图片切换效果” 2、获取素材,进入36Kr官网主页,获取...
    shuytu阅读 3,768评论 1 1