通过shell 脚本查看服务器的时时流量

DBA Blog 2012-11-11 23:49:00 累计浏览 5,244 次

vi network.sh

#!/bin/bash
 
  
ETH=$1
ETH=${ETH:-eth0}
IP=`ifconfig $ETH|awk -F '[ :]+' '/inet addr/{print $4}'`
  
while true;do
        let I+=1
        NOW=`date +"%F %T"`
        TX1=`ifconfig $ETH|grep bytes|awk -F '[ :]+' '{print $9}'`
        RX1=`ifconfig $ETH|grep bytes|awk -F '[ :]+' '{print $4}'`
        sleep 1
        TX2=`ifconfig $ETH|grep bytes|awk -F '[ :]+' '{print $9}'`
        RX2=`ifconfig $ETH|grep bytes|awk -F '[ :]+' '{print $4}'`
  
        let TX=(TX2-TX1)/1024
        let RX=(RX2-RX1)/1024
        let TX_TOTAL+=$TX
        let RX_TOTAL+=$RX
        let TX_AVERAGE=TX_TOTAL/${I}
        let RX_AVERAGE=RX_TOTAL/${I}
  
        clear
  
        printf "%10s\t%20s\n" "Device $ETH [$IP]" "$NOW"
        echo "============================================================="
        printf "%10s\t%20s\t%20s\n" CURRENT: in:${RX}KB/s out:${TX}KB/s
        printf "%10s\t%20s\t%20s\n" AVERAGE: in:${RX_AVERAGE}KB/s out:${TX_AVERAGE}KB/s
        printf "%10s\t%20s\t%20s\n" TOTAL: in:${RX_TOTAL}KB out:${TX_TOTAL}KB
done

   使用方法:

   ./network.sh eht0

   Device eth0 [192.168.1.54]       2012-11-06 17:11:09

   =============================================================

    CURRENT:                 in:28KB/s              out:38KB/s

    AVERAGE:                 in:48KB/s              out:72KB/s

    TOTAL:                  in:679KB              out:1020KB

同分类推荐文章

  1. NGINX Gateway Fabric 2.8: IP allow and deny lists, passive health checks, and cookie-based session persistence (2026-11-14 01:10:02)
  2. NGINX Ingress Controller 5.7: HTTP/2 to your upstreams, Opaque Secrets, and cert-manager challenges behind HTTPS redirects (2026-11-14 00:10:02)
  3. Hexo 文章一键发布到微信公众号 (2026-11-13 19:10:02)

查看更多 DevOps 文章 →

建议继续学习

  1. Linux如何统计进程的CPU利用率 (累计阅读 16,953)
  2. 我的 RHCA 之路 (累计阅读 14,368)
  3. Linux内存点滴 用户进程内存空间 (累计阅读 13,956)
  4. 给程序员新手的一些建议 (累计阅读 13,518)
  5. Linux 性能监控、测试、优化工具 (累计阅读 13,460)
  6. 关于linux内存free的一些事情 (累计阅读 13,399)
  7. Google怎么用linux (累计阅读 13,159)
  8. ps - 按进程消耗内存多少排序 (累计阅读 12,983)
  9. Linux Used内存到底哪里去了? (累计阅读 12,523)
  10. find命令的一点注意事项 (累计阅读 12,370)