在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
介绍一个强大的分析网络的shell脚本,此脚本是从EZHTTP拆分出来的,觉得有必要单独介绍下。 脚本运行效果截图: 此脚本包含的功能有: 1、实时监控任意网卡的流量 用到的网络分析工具: 1、tcpdump:此脚本用tcpdump来统计基于ip或基于端口的流量。 复制代码 代码如下: #!/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 regTcpdump=$(ifconfig | grep -A 1 $eth | awk -F'[: ]+' '$0~/inet addr:/{printf $4"|"}' | sed -e 's/|$//' -e 's/^/(/' -e 's/$/)\\\\\.[0-9]+:/') #统计每个端口在10s内的平均流量 #统计在10s内占用带宽最大的前10个ip echo |
请发表评论