Linux 監控指定網卡的流量信息,以及指定端口的連接數量

#!/bin/bash

ethn=$1
port=$2

while true
do
 RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 sleep 1
 RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')

 port_connect_num=$(netstat -nat | grep -w $port | wc -l)

 clear
 echo -e "\t RX `date +%k:%M:%S` TX      port_connect_num"

 RX=$((${RX_next}-${RX_pre}))
 TX=$((${TX_next}-${TX_pre}))

 if [[ $RX -lt 1024 ]];then
 RX="${RX}B/s"
 elif [[ $RX -gt 1048576 ]];then
 RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
 else
 RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
 fi

 if [[ $TX -lt 1024 ]];then
 TX="${TX}B/s"
 elif [[ $TX -gt 1048576 ]];then
 TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
 else
 TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
 fi

 echo -e "$ethn \t $RX $TX $port_connect_num"

done
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章