监控端
工具 ansible
计划任务
*/10 * * * * /dev/shm/history_log.sh
构建脚本
vim /dev/shm/history_log.sh
#!/bin/bash
function history_log(){
local time
time=`date -d "-10 min" "+%F %H:%M"`
tim=${time%[0-9]}
remote_ab -h test -c "tail -300 /tmp/all_history"|grep "$tim" >> /tmp/All_history
}
history_log
which remote_ab
alias remote_ab=‘/dev/shm/remote_ansi‘
/dev/shm/remote_ansi
vim /dev/shm/remote_ansi
#!/bin/bash
while getopts "h:c:" opt
do
case $opt in
h)
host=$OPTARG
;;
c)
command=$OPTARG
;;
*)
echo "please click --help "
;;
esac
done
if [ "x$host" != "xtest" -a "x$host" != "xonline" ]
then
echo "make a choise in [ test|online ]"
exit
fi
remote_ansi -i /home/ec2-user/remote_ansi/hosts $host -a "$command"
remote_ab //ansible安装完后,重命名ansible,然后创建软链接
mv /usr/bin_ansible /usr/bin/remote_ansi
客户端
# echo "export HISTTIMEFORMAT=‘[ %F %T ] ‘ " >> /etc/profile
然后修改/etc/skel/.bash_logout
# ~/.bash_logout
history|awk ‘{$1=""}1‘ > /tmp/history_`whoami`
sed -i "s/^/`hostname` `whoami`/g" /tmp/history_`whoami`
cat /tmp/history_`whoami` >> /tmp/all_history
history -c
> ~/.bash_history
这个脚本可以让新建端用户实现搜集日志端功能,要向让当前存在端用户也可以被搜集日志,就需要修改存在用户端加目录下端
.bash_logout文件
内容与上相同,注意, /tmp/all_history 文件权限须为777
这样做完后,就可以监控服务器上所有用户端操作历史了。