linux查看远程连接的ip,linux 检查远程登录IP,并发送告警

需求简介

记录操作命令的记录

定时检查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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值