#!/bin/bash
#This script name is scan_analyse.sh
. /etc/profile
echo "start time is $(date)"
time=$(date +"%Y-%m-%d")
yesterday=`date -d "1 day ago " +"%Y-%m-%d"`
work_dir="/root/nmap_scan"
now_dir="$work_dir/scan_result/$time"
IP='1.1.1.0/24'
contact_mail='xx@mail.com'
rm -rf $now_dir
if [ ! -d "$work_dir/scan_result/$time" ];then
mkdir -p $work_dir/scan_diff_result/$time
fi
rm -rf $work_dir/scan_diff_result/$time/result.log
ip_32=`echo $IP|cut -d . -f 1-3`
if [ ! -d $now_dir/$ip_32 ];then
mkdir -p $now_dir/$ip_32
fi
for i in {1..254}
do
nmap -sS -r -n $ip_32.$i |egrep -v "(Starting|scanned)"|egrep "(Nmap|open)" >$now_dir/$ip_32/$ip_32.$i
if [ `cat $now_dir/$ip_32/$ip_32.$i|wc -l` -eq 1 ];then
rm -rf $now_dir/$ip_32/$ip_32.$i
fi
done
echo "stop time is $(date)"
for b in $ip_32
do
for i in $(ls $now_dir/$b)
do
if [ ! -f "$work_dir/scan_source/$b/$i" ];then
echo "增加新主机 $i,下面是全部信息:">>$work_dir/scan_diff_result/$time/result.log
if [ `cat $now_dir/$b/$i|wc -l` -gt 100 ];then
echo "开启了所有端口,怀疑是有nat或者负载均衡!">>$work_dir/scan_diff_result/$time/result.log
else
cat $now_dir/$b/$i>>$work_dir/scan_diff_result/$time/result.log
fi
else
if [ `diff -u $now_dir/$b/$i $work_dir/scan_source/$b/$i|egrep -v "(\-\-\-|\+\+\+|@@)"|egrep "(Nmap|\-|\+)"|wc -l` -gt 100 ];then
head -n 1 $now_dir/$b/$i>>$work_dir/scan_diff_result/$time/result.log
echo "开启了所有端口,怀疑是有nat或者负载均衡!">>$work_dir/scan_diff_result/$time/result.log
else
diff -u $now_dir/$b/$i $work_dir/scan_source/$b/$i|egrep -v "(\-\-\-|\+\+\+|@@)"|egrep "(Nmap|\-|\+)"|sed -e 's# Nmap scan report for#扫描主机#g'|sed -e 's#^+#关闭了 #g' -e 's#^-#开启了 #g'>>$work_dir/scan_diff_result/$time/result.log
fi
fi
done
done
if [ `cat $work_dir/scan_diff_result/$time/result.log|wc -l` -eq 0 ];then
echo "今日一切正常,没有变化的端口!"|mail -s "【$time】所有IDC机房差异端口扫描结果" $contact_mail
else
sed -i "1i 大家好: \n 下面是$time日所有IDC机房扫描新增主机或已有主机新增或关闭端口情况,请各项目负责人及时认领与确认.\n" $work_dir/scan_diff_result/$time/result.log
cat $work_dir/scan_diff_result/$time/result.log|mail -s "【$time】所有IDC机房差异端口扫描结果" $contact_mail
fi
rm -rf $work_dir/scan_source/
cp -a $work_dir/scan_result/$time $work_dir/scan_source
if [ $? -eq 0 ];then
echo "运行完成,操作成功!"
else
echo "运行完成,操作失败!"
fi