#!/bin/bash
#!/bin/sh

<<COMMENT
This script is count the number of regional operators connected to IP.
Author: zhaochangming
Created: 2022-02-04 11:39:00
COMMENT

# Set environment
export PATH=/bin/:/sbin:/usr/bin/:/usr/sbin:/usr/local/sbin/:/usr/local/bin/:$PATH
CURTIME=$(date +%Y%m%d%H%M%S)


# 提示信息
echo "正在IP解析可能需要几分钟......"

# 配置python虚拟环境
if [[ ! -e /usr/local/bin/virtualenvwrapper.sh ]] && [[ ! -e /usr/share/virtualenvwrapper/virtualenvwrapper.sh ]];then
	# Centos
	if $(cat /etc/*-release | grep -iq centos);then
            command -v unzip &>/dev/null || yum -y install unzip >/dev/null 2>&1
	    yum -y install python3 python3-pip >/dev/null 2>&1
	    pip3 install virtualenvwrapper -i https://pypi.tuna.tsinghua.edu.cn/simple >/dev/null 2>&1
            [ $? -ne 0 ] && pip3 install virtualenvwrapper  -i https://mirrors.aliyun.com/pypi/simple >/dev/null 2>&1
	    source /usr/local/bin/virtualenvwrapper.sh >/dev/null 2>&1
	else 
	    #debain
	    apt update >/dev/null 2>&1
	    apt install -y python3-venv python3 python3-pip >/dev/null 2>&1
            pip3 install  pbr virtualenvwrapper -i https://pypi.tuna.tsinghua.edu.cn/simple >/dev/null 2>&1
            [ $? -ne 0 ] && echo "fail,安装virtualenvwrapper失败,请检查apt配置." && exit 110
	    source /usr/share/virtualenvwrapper/virtualenvwrapper.sh >/dev/null 2>&1
	fi 
	
	echo 'export WORKON_HOME=$HOME/.virtualenvs' >> /root/.bashrc
	echo 'export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3' >> /root/.bashrc
	source /root/.bashrc 
	mkvirtualenv -p /usr/bin/python3 python_op >/dev/null 2>&1
        pip config set  global.index-url  https://pypi.tuna.tsinghua.edu.cn/simple >/dev/null 2>&1
	pip install maxminddb >/dev/null 2>&1
        [ $? -ne 0 ] && pip install maxminddb  -i https://mirrors.aliyun.com/pypi/simple >/dev/null 2>&1
        deactivate

fi


# 下载解析IP区域运营商脚本
if [ ! -e /tmp/isp_count ];then
    wget -O /tmp/isp_count.tar.gz  http://tw15b0178.onething.net:9090/tool/isp_count.tar.gz >/dev/null 2>&1
    tar -xzf /tmp/isp_count.tar.gz -C /tmp
fi

# 统计建连外网IP
[ ! -d /tmp/isp_count/${CURTIME} ] && mkdir -p /tmp/isp_count/${CURTIME}
netstat -anutlp | grep -E "ESTABLISHED|SYN_SENT" | awk -F "[: ]+" '$6 !~ "^10\\.|^169\\.|^127\\.|[a-z]|^.$|^192|^[0-9]+$" {print$6,$8}' | awk  -v var="${CURTIME}" '{print $1 > ("/tmp/isp_count/" var "/iplist.txt") ;print $0}' > /tmp/isp_count/${CURTIME}/iplist_status.txt

# 基础信息
echo "++++++++++++++++++++++++++++++++++++++++++"
echo "Hostname:$(cat /etc/hostname)"
echo "Date:$(date +%F" "%T)"
echo "Total:$(cat /tmp/isp_count/${CURTIME}/iplist_status.txt | wc -l)"
echo "ESTABLISHED:$(cat /tmp/isp_count/${CURTIME}/iplist_status.txt | grep ESTABLISHED | wc -l)"
echo "SYN_SENT:$(cat /tmp/isp_count/${CURTIME}/iplist_status.txt | grep SYN_SENT | wc -l)"


# 执行IP解析脚本
command  -v workon >/dev/null 2>&1
if [ $? -ne 0 ];then
    if $(cat /etc/*-release | grep -iq centos);then 
        source /usr/local/bin/virtualenvwrapper.sh >/dev/null 2>&1
    else
        source /usr/share/virtualenvwrapper/virtualenvwrapper.sh >/dev/null 2>&1
    fi
    source /root/.bashrc
fi
workon python_op
cd /tmp/isp_count
python read_ipdb.py ${CURTIME}/iplist.txt > ${CURTIME}/iplist_isp.txt
deactivate


#运营商占比
echo
echo "++++++++按地区运营商统计连接数占比+++++++++"
total=$(cat /tmp/isp_count/${CURTIME}/iplist.txt | wc -l)
cat /tmp/isp_count/${CURTIME}/iplist_isp.txt | awk -F ':' '{print$2}' | sort | uniq -c | sort -nr | awk  -v var="$total"  'BEGIN {print "国家 省份 运营商 当前连接数 占比"} {print$2,$3,$4,$1,$1/var*100"%"}' | column -t | tee -a  /tmp/isp_count/${CURTIME}/iplist_isp_count.txt

# 连接top50
echo 
echo "+++++++按地区运营商统计连接数占比TOP50++++++"
cat /tmp/isp_count/${CURTIME}/iplist_isp.txt | sort  | uniq -c | sort -nr | awk -F "[: ]+"  'BEGIN {print "国家 省份 运营商 IP 连接数"}  {print$4,$5,$6,$3,$2}' | column  -t  | head -n 50  | tee -a /tmp/isp_count/${CURTIME}/iplist_isp_count_top50.txt
