远程登录linux_linux 检查远程登录IP,并发送告警
生活随笔
收集整理的這篇文章主要介紹了
远程登录linux_linux 检查远程登录IP,并发送告警
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求簡介
history
history 可以記錄終端的操作命令。
編輯/etc/bashrc
修改對應的格式變量
if [ "$SSH_CONNECTION" == "" ]then SSH_CONNECTION=localhostfiexport HISTTIMEFORMAT="%F %T" "$(echo $SSH_CONNECTION|awk '{print $1}') "export PROMPT_COMMAND='history 1|tail -1 >> /tmp/command.log'這樣子命令記錄在 /tmp/command.log
判斷是否是合法IP
#!/bin/bashwhile_ip=" localhost 192.168.4.2 "to_mail="root@localhost"OLDIFS=$IFSIFS=''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.logdoneIFS=$OLDIFSif [ -f /tmp/command.log ]thenmv /tmp/command.log /tmp/command.log_$(date +%Y%m%d%H%M%S)fiif [ -f /tmp/warning.log ]thenmail -s "有異常IP登錄終端操作_"$(date +%Y%m%d%H%M%S) $to_mail < /tmp/warning.log rm -f /tmp/warning.logfi修改對應郵箱,如果使用外部郵箱,需要配置/etc/mail.rc
定時檢查
crontab -e * * * * * /opt/scripts/check_log.sh總結
以上是生活随笔為你收集整理的远程登录linux_linux 检查远程登录IP,并发送告警的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django 不包括字段 序列化器_手写
- 下一篇: 怎么在linux操作python_pyt