shell脚本监控登录用户是否异常
1, 下载php
apt install php 或
yum install php
2. 用php命令来判断ip归属地
vi ip.php
<?php
$ip="192.168.1.109";
$json=file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
$arr=json_decode($json);
echo $arr->data->country;
echo $arr->data->area;
echo $arr->data->region;
echo $arr->data->city;
echo $arr->data->isp;
?>测试: php ip.php
3.1 检测登录IP是否包含在文件normal_ip.txt中
#!/bin/bash
for i in `who | grep "(" | cut -d "(" -f 2 | cut -d ")" -f 1`
do
count=`echo $i | grep "[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}" | wc -l`
if [ $count -ne 1 ];then
ip=`arp -a $i | cut -d "(" -f 2 | cut -d ")" -f 1`
else
ip=$i
fi
count=`grep $ip normal_ip.txt | wc -l`
if [ $count -ne 1 ];then
sudo sed -i '2s@^.*$@$ip="'$ip'";@' ip.php
extremely_address=`sudo php ip.php`
hostname=`hostname`
echo "$ip异常登录$hostname主机,归属地:$extremely_address"
fi
done3.2 检测登录IP是否包含在文件normal_ip.txt中,并且判断IP是否属于内网或深圳市
#!/bin/bash
for i in `who | grep "(" | cut -d "(" -f 2 | cut -d ")" -f 1`
do
count=`echo $i | grep "[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}" | wc -l`
if [ $count -ne 1 ];then
ip=`arp -a $i | cut -d "(" -f 2 | cut -d ")" -f 1`
else
ip=$i
fi
count=`grep $ip normal_ip.txt | wc -l`
if [ $count -ne 1 ];then
sudo sed -i '2s@^.*$@$ip="'$ip'";@' ip.php
extremely_address=`sudo php ip.php`
hostname=`hostname`
count=`echo $extremely_address | grep 内网 | wc -l`
count2=`echo $extremely_address | grep 深圳 | wc -l`
if [ $count -eq 0 -a $count2 -eq 0];then
echo "$ip异常登录$hostname,归属地:$extremely_address"
else
echo "$ip正常登录$hostname,归属地:$extremely_address"
fi
fi
done4. crontab 执行以上命令每分钟一次