set_system_history.sh
#!/bin/bash
# jihongrui@outlook.com
#主要功能:
#
#可以記錄哪個ip和時間(精確到秒)作了哪些命令
#
#通過用戶登錄時候白筹,重新定義HISTFILE
#
#HISTFILE文件名包含登錄用戶名硼莽,ip,登錄時間(精確到秒)等
#
#這樣即使相同的用戶從不同ip鲫骗、在不同的時間登錄都會被記錄
#
#可以記錄每條命令的開始執(zhí)行時間
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
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者