实时监控网卡流量的脚本

8 篇文章 0 订阅

闲来无聊,写一个实时监控网卡流量的脚本

cat net_traffic_monitor.sh 
#!/bin/bash
source ~/.bash_profile
if [ -n "${1}" ];then NetDevice=${1};else NetDevice=eth0;fi
printf "|------ Time  ------|---- NetIn ----|--- NetOUt ----|\n"
while true
do
  time=$(date +%F" "%T)
  rx_before=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $4}')
  tx_before=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $9}')
  sleep 1
  rx_after=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $4}')
  tx_after=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $9}')
  rx_result=$(((rx_after-rx_before)/1024))
  tx_result=$(((tx_after-tx_before)/1024))
  printf "|%19s|%10s kbps|%10s kbps|\n" "${time}" "${rx_result}" "${tx_result}"
  # sleep 1
done

执行效果:
这里写图片描述

可以方便的使用dstat命令实现:
这里写图片描述

确实无聊…

[TOC]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值