linux 檢查遠程登錄IP,併發送告警

需求簡介

  1. 記錄操作命令的記錄
  2. 定時檢查IP,是否是合法IP
  3. 發送告警


linux 檢查遠程登錄IP,併發送告警


history

history 可以記錄終端的操作命令。

修改對應的格式變量

<code>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'
/<code>

這樣子命令記錄在 /tmp/command.log


linux 檢查遠程登錄IP,併發送告警


判斷是否是合法IP

<code>#!/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
/<code>

修改對應郵箱,如果使用外部郵箱,需要配置/etc/mail.rc



定時檢查

<code>crontab -e 
/<code>
<code>
* * * * * /opt/scripts/check_log.sh

/<code>


linux 檢查遠程登錄IP,併發送告警


分享到:


相關文章: