美文网首页网络
【网卡】网卡流量统计监控

【网卡】网卡流量统计监控

作者: Bogon | 来源:发表于2022-07-20 00:49 被阅读0次

    cat monitor_net.sh

    #!/bin/bash
    
    # sh monitor_net.sh  网卡名
    # sh monitor_net.sh  eth0
    
    ethn=$1
    
    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}')
    
      clear
      echo -e "\t RX `date +%k:%M:%S` TX"
    
      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 "
    done
    
    
    #  sh monitor_net.sh   eth0
    
    image.png

    相关文章

      网友评论

        本文标题:【网卡】网卡流量统计监控

        本文链接:https://www.haomeiwen.com/subject/xshtirtx.html