Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #显示菜单(单选)
  4. display_menu(){
  5. local soft=$1
  6. local prompt="which ${soft} you'd select: "
  7. eval local arr=(\${${soft}_arr[@]})
  8. while true
  9. do
  10. echo -e "#################### ${soft} setting ####################\n\n"
  11. for ((i=1;i<=${#arr[@]};i++ )); do echo -e "$i) ${arr[$i-1]}"; done
  12. echo
  13. read -p "${prompt}" $soft
  14. eval local select=\$$soft
  15. if [ "$select" == "" ] || [ "${arr[$soft-1]}" == "" ];then
  16. prompt="input errors,please input a number: "
  17. else
  18. eval $soft=${arr[$soft-1]}
  19. eval echo "your selection: \$$soft"
  20. break
  21. fi
  22. done
  23. }
  24.  
  25. #把带宽bit单位转换为人类可读单位
  26. bit_to_human_readable(){
  27. #input bit value
  28. local trafficValue=$1
  29.  
  30. if [[ ${trafficValue%.*} -gt 922 ]];then
  31. #conv to Kb
  32. trafficValue=`awk -v value=$trafficValue 'BEGIN{printf "%0.1f",value/1024}'`
  33. if [[ ${trafficValue%.*} -gt 922 ]];then
  34. #conv to Mb
  35. trafficValue=`awk -v value=$trafficValue 'BEGIN{printf "%0.1f",value/1024}'`
  36. echo "${trafficValue}Mb"
  37. else
  38. echo "${trafficValue}Kb"
  39. fi
  40. else
  41. echo "${trafficValue}b"
  42. fi
  43. }
  44.  
  45. #判断包管理工具
  46. check_package_manager(){
  47. local manager=$1
  48. local systemPackage=''
  49. if cat /etc/issue | grep -q -E -i "ubuntu|debian";then
  50. systemPackage='apt'
  51. elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat";then
  52. systemPackage='yum'
  53. elif cat /proc/version | grep -q -E -i "ubuntu|debian";then
  54. systemPackage='apt'
  55. elif cat /proc/version | grep -q -E -i "centos|red hat|redhat";then
  56. systemPackage='yum'
  57. else
  58. echo "unkonw"
  59. fi
  60.  
  61. if [ "$manager" == "$systemPackage" ];then
  62. return 0
  63. else
  64. return 1
  65. fi
  66. }
  67.  
  68.  
  69. #实时流量
  70. realTimeTraffic(){
  71. local eth=""
  72. local nic_arr=(`ifconfig | grep -E -o "^[a-z0-9]+" | grep -v "lo" | uniq`)
  73. local nicLen=${#nic_arr[@]}
  74. if [[ $nicLen -eq 0 ]]; then
  75. echo "sorry,I can not detect any network device,please report this issue to author."
  76. exit 1
  77. elif [[ $nicLen -eq 1 ]]; then
  78. eth=$nic_arr
  79. else
  80. display_menu nic
  81. eth=$nic
  82. fi
  83.  
  84. local clear=true
  85. local eth_in_peak=0
  86. local eth_out_peak=0
  87. local eth_in=0
  88. local eth_out=0
  89.  
  90. while true;do
  91. #移动光标到0:0位置
  92. printf "\033[0;0H"
  93. #清屏并打印Now Peak
  94. [[ $clear == true ]] && printf "\033[2J" && echo "$eth--------Now--------Peak-----------"
  95. traffic_be=(`awk -v eth=$eth -F'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
  96. sleep 2
  97. traffic_af=(`awk -v eth=$eth -F'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
  98. #计算速率
  99. eth_in=$(( (${traffic_af[0]}-${traffic_be[0]})*8/2 ))
  100. eth_out=$(( (${traffic_af[1]}-${traffic_be[1]})*8/2 ))
  101. #计算流量峰值
  102. [[ $eth_in -gt $eth_in_peak ]] && eth_in_peak=$eth_in
  103. [[ $eth_out -gt $eth_out_peak ]] && eth_out_peak=$eth_out
  104. #移动光标到2:1
  105. printf "\033[2;1H"
  106. #清除当前行
  107. printf "\033[K"
  108. printf "%-20s %-20s\n" "Receive: $(bit_to_human_readable $eth_in)" "$(bit_to_human_readable $eth_in_peak)"
  109. #清除当前行
  110. printf "\033[K"
  111. printf "%-20s %-20s\n" "Transmit: $(bit_to_human_readable $eth_out)" "$(bit_to_human_readable $eth_out_peak)"
  112. [[ $clear == true ]] && clear=false
  113. done
  114. }
  115.  
  116. #流量和连接概览
  117. trafficAndConnectionOverview(){
  118. if ! which tcpdump > /dev/null;then
  119. echo "tcpdump not found,going to install it."
  120. if check_package_manager apt;then
  121. apt-get -y install tcpdump
  122. elif check_package_manager yum;then
  123. yum -y install tcpdump
  124. fi
  125. fi
  126.  
  127. local reg=""
  128. local eth=""
  129. local nic_arr=(`ifconfig | grep -E -o "^[a-z0-9]+" | grep -v "lo" | uniq`)
  130. local nicLen=${#nic_arr[@]}
  131. if [[ $nicLen -eq 0 ]]; then
  132. echo "sorry,I can not detect any network device,please report this issue to author."
  133. exit 1
  134. elif [[ $nicLen -eq 1 ]]; then
  135. eth=$nic_arr
  136. else
  137. display_menu nic
  138. eth=$nic
  139. fi
  140.  
  141. echo "please wait for 10s to generate network data..."
  142. echo
  143. #当前流量值
  144. local traffic_be=(`awk -v eth=$eth -F'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
  145. #tcpdump监听网络
  146. tcpdump -v -i $eth -tnn > /tmp/tcpdump_temp 2>&1 &
  147. sleep 10
  148. clear
  149. kill `ps aux | grep tcpdump | grep -v grep | awk '{print $2}'`
  150.  
  151. #10s后流量值
  152. local traffic_af=(`awk -v eth=$eth -F'[: ]+' '{if ($0 ~eth){print $3,$11}}' /proc/net/dev`)
  153. #打印10s平均速率
  154. local eth_in=$(( (${traffic_af[0]}-${traffic_be[0]})*8/10 ))
  155. local eth_out=$(( (${traffic_af[1]}-${traffic_be[1]})*8/10 ))
  156. echo -e "\033[32mnetwork device $eth average traffic in 10s: \033[0m"
  157. echo "$eth Receive: $(bit_to_human_readable $eth_in)/s"
  158. echo "$eth Transmit: $(bit_to_human_readable $eth_out)/s"
  159. echo
  160.  
  161. local regTcpdump=$(ifconfig | grep -A 1 $eth | awk -F'[: ]+' '$0~/inet addr:/{printf $4"|"}' | sed -e 's/|$//' -e 's/^/(/' -e 's/$/)\\\\\.[0-9]+:/')
  162.  
  163. #新旧版本tcpdump输出格式不一样,分别处理
  164. if awk '/^IP/{print;exit}' /tmp/tcpdump_temp | grep -q ")$";then
  165. #处理tcpdump文件
  166. awk '/^IP/{print;getline;print}' /tmp/tcpdump_temp > /tmp/tcpdump_temp2
  167. else
  168. #处理tcpdump文件
  169. awk '/^IP/{print}' /tmp/tcpdump_temp > /tmp/tcpdump_temp2
  170. sed -i -r 's#(.*: [0-9]+\))(.*)#\1\n \2#' /tmp/tcpdump_temp2
  171. fi
  172.  
  173. awk '{len=$NF;sub(/\)/,"",len);getline;print $0,len}' /tmp/tcpdump_temp2 > /tmp/tcpdump
  174.  
  175. #统计每个端口在10s内的平均流量
  176. echo -e "\033[32maverage traffic in 10s base on server port: \033[0m"
  177. 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 | \
  178. sort -k 4 -nr | head -n 10 | while read a b c d;do
  179. echo "$a $b $c $(bit_to_human_readable $d)/s"
  180. done
  181. echo -ne "\033[11A"
  182. echo -ne "\033[50C"
  183. echo -e "\033[32maverage traffic in 10s base on client port: \033[0m"
  184. 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 | \
  185. sort -k 4 -nr | head -n 10 | while read a b c d;do
  186. echo -ne "\033[50C"
  187. echo "$a $b $c $(bit_to_human_readable $d)/s"
  188. done
  189.  
  190. echo
  191.  
  192. #统计在10s内占用带宽最大的前10个ip
  193. echo -e "\033[32mtop 10 ip average traffic in 10s base on server: \033[0m"
  194. 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 | \
  195. sort -k 4 -nr | head -n 10 | while read a b c d;do
  196. echo "$a $b $c $(bit_to_human_readable $d)/s"
  197. done
  198. echo -ne "\033[11A"
  199. echo -ne "\033[50C"
  200. echo -e "\033[32mtop 10 ip average traffic in 10s base on client: \033[0m"
  201. 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 | \
  202. sort -k 4 -nr | head -n 10 | while read a b c d;do
  203. echo -ne "\033[50C"
  204. echo "$a $b $c $(bit_to_human_readable $d)/s"
  205. done
  206.  
  207. echo
  208. #统计连接状态
  209. local regSS=$(ifconfig | grep -A 1 $eth | awk -F'[: ]+' '$0~/inet addr:/{printf $4"|"}' | sed -e 's/|$//')
  210. ss -an | grep -v -E "LISTEN|UNCONN" | grep -E "$regSS" > /tmp/ss
  211. echo -e "\033[32mconnection state count: \033[0m"
  212. awk 'NR>1{sum[$(NF-4)]+=1}END{for (state in sum){print state,sum[state]}}' /tmp/ss | sort -k 2 -nr
  213. echo
  214. #统计各端口连接状态
  215. echo -e "\033[32mconnection state count by port base on server: \033[0m"
  216. 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 100
  217. echo -ne "\033[11A"
  218. echo -ne "\033[50C"
  219. echo -e "\033[32mconnection state count by port base on client: \033[0m"
  220. 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 100 | awk '{print "\033[50C"$0}'
  221. echo
  222. #统计端口为80且状态为ESTAB连接数最多的前100个IP
  223. echo -e "\033[32mtop 10 ip ESTAB state count at port 80: \033[0m"
  224. 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 100
  225. echo
  226. #统计端口为80且状态为SYN-RECV连接数最多的前100个IP
  227. echo -e "\033[32mtop 10 ip SYN-RECV state count at port 80: \033[0m"
  228. 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 100
  229. }
  230.  
  231. main(){
  232. while true; do
  233. echo -e "1) real time traffic.\n2) traffic and connection overview.\n"
  234. read -p "please input your select(ie 1): " select
  235. case $select in
  236. 1) realTimeTraffic;break;;
  237. 2) trafficAndConnectionOverview;break;;
  238. *) echo "input error,please input a number.";;
  239. esac
  240. done
  241. }
  242.  
  243. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement