欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

一个强大的网络分析shell脚本分享(实时流量、连接统计)

程序员文章站 2022-06-13 17:18:29
介绍一个强大的分析网络的shell脚本,此脚本是从ezhttp拆分出来的,觉得有必要单独介绍下。 脚本运行效果截图: 此脚本包含的功能有: 1、实时监控任意网...

介绍一个强大的分析网络的shell脚本,此脚本是从ezhttp拆分出来的,觉得有必要单独介绍下。

脚本运行效果截图:

一个强大的网络分析shell脚本分享(实时流量、连接统计)

一个强大的网络分析shell脚本分享(实时流量、连接统计)

此脚本包含的功能有:

1、实时监控任意网卡的流量
2、统计10秒内平均流量
3、统计每个端口在10秒内的平均流量,基于客户端和服务端端口统计。可以看出哪些端口占流量比较大,对于web服务器,一般是80端口。其它端口受到攻击时,也有可能其它端口流量比较大。所以此功能可以帮助我们端口流量是否正常。
4、统计在10s内占用带宽最大的前10个ip。此项功能可以帮助我们来查出是否有恶意占用带宽的ip。
5、统计连接状态。此项功能可以让我们看出哪些连接状态比较大。如果syn-recv状态比较多的话,有可以受到半连接攻击。如果establised非常大,但通过日志发现没有那么多请求,或者通过tcpdump发现大量ip只建立连接不请求数据的话,可能是受到了全连接攻击,这时候如果你使用的是nginx服务器,可以在配置文件增加listen 80 deferred来防止。
6、统计各端口连接状态。当可能受到攻击时,此项功能可以帮助我们发现是哪个端口受到攻击。
7、统计端口为80且状态为estab连接数最多的前10个ip。此项功能可以帮助我们来找出创建连接过多的ip,进而屏蔽。
8、统计端口为80且状态为syn-recv连接数最多的前10个ip。当受到半连接攻击时,此项功能可以帮助我们找到恶意ip。

用到的网络分析工具:

1、tcpdump:此脚本用tcpdump来统计基于ip或基于端口的流量。
2、ss: 此脚本用ss命令来统计连接状态,实际使用发现ss比netstat高效得多。
3、/proc/net/dev,用来统计指定网卡的流量。
脚本下载地址:
下面贴出完整的脚本:

复制代码 代码如下:

#!/bin/bash
 
#write by zhumaohai(admin#centos.bz)
 
#显示菜单(单选)
display_menu(){
local soft=$1
local prompt="which ${soft} you'd select: "
eval local arr=(\${${soft}_arr[@]})
while true
do
    echo -e "#################### ${soft} setting ####################\n\n"
    for ((i=1;i<=${#arr[@]};i++ )); do echo -e "$i) ${arr[$i-1]}"; done
    echo
    read -p "${prompt}" $soft
    eval local select=\$$soft
    if [ "$select" == "" ] || [ "${arr[$soft-1]}" == ""  ];then
        prompt="input errors,please input a number: "
    else
        eval $soft=${arr[$soft-1]}
        eval echo "your selection: \$$soft"            
        break
    fi
done
}
 
#把带宽bit单位转换为人类可读单位
bit_to_human_readable(){
    #input bit value
    local trafficvalue=$1
 
    if [[ ${trafficvalue%.*} -gt 922 ]];then
        #conv to kb
        trafficvalue=`awk -v value=$trafficvalue 'begin{printf "%0.1f",value/1024}'`
        if [[ ${trafficvalue%.*} -gt 922 ]];then
            #conv to mb
            trafficvalue=`awk -v value=$trafficvalue 'begin{printf "%0.1f",value/1024}'`
            echo "${trafficvalue}mb"
        else
            echo "${trafficvalue}kb"
        fi
    else
        echo "${trafficvalue}b"
    fi
}
 
#判断包管理工具
check_package_manager(){
    local manager=$1
    local systempackage=''
    if cat /etc/issue | grep -q -e -i "ubuntu|debian";then
        systempackage='apt'
    elif cat /etc/issue | grep -q -e -i "centos|red hat|redhat";then
        systempackage='yum'
    elif cat /proc/version | grep -q -e -i "ubuntu|debian";then
        systempackage='apt'
    elif cat /proc/version | grep -q -e -i "centos|red hat|redhat";then
        systempackage='yum'
    else
        echo "unkonw"
    fi
 
    if [ "$manager" == "$systempackage" ];then
        return 0
    else
        return 1
    fi  
}
 
 
#实时流量
realtimetraffic(){
    local eth=""
    local nic_arr=(`ifconfig | grep -e -o "^[a-z0-9]+" | grep -v "lo" | uniq`)
    local niclen=${#nic_arr[@]}
    if [[ $niclen -eq 0 ]]; then
        echo "sorry,i can not detect any network device,please report this issue to author."
        exit 1
    elif [[ $niclen -eq 1 ]]; then
        eth=$nic_arr
    else
        display_menu nic
        eth=$nic
    fi  
 
    local clear=true
    local eth_in_peak=0
    local eth_out_peak=0
    local eth_in=0
    local eth_out=0
 
    while true;do
        #移动光标到0:0位置
        printf "\033[0;0h"
        #清屏并打印now peak
        [[ $clear == true ]] && printf "\033[2j" && echo "$eth--------now--------peak-----------"
        traffic_be=(`awk -v eth=$eth -f'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
        sleep 2
        traffic_af=(`awk -v eth=$eth -f'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
        #计算速率
        eth_in=$(( (${traffic_af[0]}-${traffic_be[0]})*8/2 ))
        eth_out=$(( (${traffic_af[1]}-${traffic_be[1]})*8/2 ))
        #计算流量峰值
        [[ $eth_in -gt $eth_in_peak ]] && eth_in_peak=$eth_in
        [[ $eth_out -gt $eth_out_peak ]] && eth_out_peak=$eth_out
        #移动光标到2:1
        printf "\033[2;1h"
        #清除当前行
        printf "\033[k"  
        printf "%-20s %-20s\n" "receive:  $(bit_to_human_readable $eth_in)" "$(bit_to_human_readable $eth_in_peak)"
        #清除当前行
        printf "\033[k"
        printf "%-20s %-20s\n" "transmit: $(bit_to_human_readable $eth_out)" "$(bit_to_human_readable $eth_out_peak)"
        [[ $clear == true ]] && clear=false
    done
}
 
#流量和连接概览
trafficandconnectionoverview(){
    if ! which tcpdump > /dev/null;then
        echo "tcpdump not found,going to install it."
        if check_package_manager apt;then
            apt-get -y install tcpdump
        elif check_package_manager yum;then
            yum -y install tcpdump
        fi
    fi
 
    local reg=""
    local eth=""
    local nic_arr=(`ifconfig | grep -e -o "^[a-z0-9]+" | grep -v "lo" | uniq`)
    local niclen=${#nic_arr[@]}
    if [[ $niclen -eq 0 ]]; then
        echo "sorry,i can not detect any network device,please report this issue to author."
        exit 1
    elif [[ $niclen -eq 1 ]]; then
        eth=$nic_arr
    else
        display_menu nic
        eth=$nic
    fi
 
    echo "please wait for 10s to generate network data..."
    echo
    #当前流量值
    local traffic_be=(`awk -v eth=$eth -f'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
    #tcpdump监听网络
    tcpdump -v -i $eth -tnn > /tmp/tcpdump_temp 2>&1 &
    sleep 10
    clear
    kill `ps aux | grep tcpdump | grep -v grep | awk '{print $2}'`

    #10s后流量值
    local traffic_af=(`awk -v eth=$eth -f'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
    #打印10s平均速率
    local eth_in=$(( (${traffic_af[0]}-${traffic_be[0]})*8/10 ))
    local eth_out=$(( (${traffic_af[1]}-${traffic_be[1]})*8/10 ))
    echo -e "\033[32mnetwork device $eth average traffic in 10s: \033[0m"
    echo "$eth receive: $(bit_to_human_readable $eth_in)/s"
    echo "$eth transmit: $(bit_to_human_readable $eth_out)/s"
    echo

    local regtcpdump=$(ifconfig | grep -a 1 $eth | awk -f'[: ]+' '$0~/inet addr:/{printf $4"|"}' | sed -e 's/|$//' -e 's/^/(/' -e 's/$/)\\\\\.[0-9]+:/')
 
    #新旧版本tcpdump输出格式不一样,分别处理
    if awk '/^ip/{print;exit}' /tmp/tcpdump_temp | grep -q ")$";then
        #处理tcpdump文件
        awk '/^ip/{print;getline;print}' /tmp/tcpdump_temp > /tmp/tcpdump_temp2
    else
        #处理tcpdump文件
        awk '/^ip/{print}' /tmp/tcpdump_temp > /tmp/tcpdump_temp2
        sed -i -r 's#(.*: [0-9]+\))(.*)#\1\n    \2#' /tmp/tcpdump_temp2
    fi
   
    awk '{len=$nf;sub(/\)/,"",len);getline;print $0,len}' /tmp/tcpdump_temp2 > /tmp/tcpdump

    #统计每个端口在10s内的平均流量
    echo -e "\033[32maverage traffic in 10s base on server port: \033[0m"
    awk -f'[ .:]+' -v regtcpdump=$regtcpdump '{if ($0 ~ regtcpdump){line="clients > "$8"."$9"."$10"."$11":"$12}else{line=$2"."$3"."$4"."$5":"$6" > clients"};sum[line]+=$nf*8/10}end{for (line in sum){printf "%s %d\n",line,sum[line]}}' /tmp/tcpdump | \
    sort -k 4 -nr | head -n 10 | while read a b c d;do
        echo "$a $b $c $(bit_to_human_readable $d)/s"
    done
    echo -ne "\033[11a"
    echo -ne "\033[50c"
    echo -e "\033[32maverage traffic in 10s base on client port: \033[0m"
    awk -f'[ .:]+' -v regtcpdump=$regtcpdump '{if ($0 ~ regtcpdump){line=$2"."$3"."$4"."$5":"$6" > server"}else{line="server > "$8"."$9"."$10"."$11":"$12};sum[line]+=$nf*8/10}end{for (line in sum){printf "%s %d\n",line,sum[line]}}' /tmp/tcpdump | \
    sort -k 4 -nr | head -n 10 | while read a b c d;do
            echo -ne "\033[50c"
            echo "$a $b $c $(bit_to_human_readable $d)/s"
    done  
       
    echo

    #统计在10s内占用带宽最大的前10个ip
    echo -e "\033[32mtop 10 ip average traffic in 10s base on server: \033[0m"
    awk -f'[ .:]+' -v regtcpdump=$regtcpdump '{if ($0 ~ regtcpdump){line=$2"."$3"."$4"."$5" > "$8"."$9"."$10"."$11":"$12}else{line=$2"."$3"."$4"."$5":"$6" > "$8"."$9"."$10"."$11};sum[line]+=$nf*8/10}end{for (line in sum){printf "%s %d\n",line,sum[line]}}' /tmp/tcpdump | \
    sort -k 4 -nr | head -n 10 | while read a b c d;do
        echo "$a $b $c $(bit_to_human_readable $d)/s"
    done
    echo -ne "\033[11a"
    echo -ne "\033[50c"
    echo -e "\033[32mtop 10 ip average traffic in 10s base on client: \033[0m"
    awk -f'[ .:]+' -v regtcpdump=$regtcpdump '{if ($0 ~ regtcpdump){line=$2"."$3"."$4"."$5":"$6" > "$8"."$9"."$10"."$11}else{line=$2"."$3"."$4"."$5" > "$8"."$9"."$10"."$11":"$12};sum[line]+=$nf*8/10}end{for (line in sum){printf "%s %d\n",line,sum[line]}}' /tmp/tcpdump | \
    sort -k 4 -nr | head -n 10 | while read a b c d;do
        echo -ne "\033[50c"
        echo "$a $b $c $(bit_to_human_readable $d)/s"
    done

    echo
    #统计连接状态
    local regss=$(ifconfig | grep -a 1 $eth | awk -f'[: ]+' '$0~/inet addr:/{printf $4"|"}' | sed -e 's/|$//')
    ss -an | grep -v -e "listen|unconn" | grep -e "$regss" > /tmp/ss
    echo -e "\033[32mconnection state count: \033[0m"
    awk 'nr>1{sum[$(nf-4)]+=1}end{for (state in sum){print state,sum[state]}}' /tmp/ss | sort -k 2 -nr
    echo
    #统计各端口连接状态
    echo -e "\033[32mconnection state count by port base on server: \033[0m"
    awk 'nr>1{sum[$(nf-4),$(nf-1)]+=1}end{for (key in sum){split(key,subkey,subsep);print subkey[1],subkey[2],sum[subkey[1],subkey[2]]}}' /tmp/ss | sort -k 3 -nr | head -n 10  
    echo -ne "\033[11a"
    echo -ne "\033[50c"
    echo -e "\033[32mconnection state count by port base on client: \033[0m"
    awk 'nr>1{sum[$(nf-4),$(nf)]+=1}end{for (key in sum){split(key,subkey,subsep);print subkey[1],subkey[2],sum[subkey[1],subkey[2]]}}' /tmp/ss | sort -k 3 -nr | head -n 10 | awk '{print "\033[50c"$0}'  
    echo  
    #统计端口为80且状态为estab连接数最多的前10个ip
    echo -e "\033[32mtop 10 ip estab state count at port 80: \033[0m"
    cat /tmp/ss | grep estab | awk -f'[: ]+' '{sum[$(nf-2)]+=1}end{for (ip in sum){print ip,sum[ip]}}' | sort -k 2 -nr | head -n 10
    echo
    #统计端口为80且状态为syn-recv连接数最多的前10个ip
    echo -e "\033[32mtop 10 ip syn-recv state count at port 80: \033[0m"
    cat /tmp/ss | grep -e "$regss" | grep syn-recv | awk -f'[: ]+' '{sum[$(nf-2)]+=1}end{for (ip in sum){print ip,sum[ip]}}' | sort -k 2 -nr | head -n 10
}
 
main(){
    while true; do
        echo -e "1) real time traffic.\n2) traffic and connection overview.\n"
        read -p "please input your select(ie 1): " select
        case  $select in
            1) realtimetraffic;break;;
            2) trafficandconnectionoverview;break;;
            *) echo "input error,please input a number.";;
        esac
    done  
}
 
main