需求简介
记录操作命令的记录
定时检查IP,是否是合法IP
发送告警
history
history 可以记录终端的操作命令。
编辑/etc/bashrc
修改对应的格式变量
if [ "$SSH_CONNECTION" == "" ]
then
SSH_CONNECTION=localhost
fi
export HISTTIMEFORMAT="%F %T"\ "$(echo $SSH_CONNECTION|awk '{print $1}') "
export PROMPT_COMMAND='history 1|tail -1 >> /tmp/command.log'
这样子命令记录在 /tmp/command.log
判断是否是合法IP
#!/bin/bash
while_ip=" localhost 192.168.4.2 "
to_mail="root@localhost"
OLDIFS=$IFS
IFS='\n'
for line in $(cat /tmp/command.log)
do
ip=$(echo $line | awk '{print $4}')
if grep "\ $ip\ " $while_ip >> /dev/null 2>&1
then
continue
fi
echo $line >> /tmp/warning.log
done
IFS=$OLDIFS
if [ -f /tmp/command.log ]
then
mv /tmp/command.log /tmp/command.log_$(date +%Y%m%d%H%M%S)
fi
if [ -f /tmp/warning.log ]
then
mail -s "有异常IP登录终端操作_"$(date +%Y%m%d%H%M%S) $to_mail < /tmp/warning.log
rm -f /tmp/warning.log
fi
修改对应邮箱,如果使用外部邮箱,需要配置/etc/mail.rc