• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > 统计网卡流量的两段shell脚本(使用ifconfig)

统计网卡流量的两段shell脚本(使用ifconfig)

作者:mdxy-dxy 字体:[增加 减小] 来源:互联网

mdxy-dxy 通过本文主要向大家介绍了ifconfig看不到网卡,shell ifconfig,ifconfig,ifconfig命令,ifconfig设置ip等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

使用shell脚本计算Linux网卡流量,方法中最关键点:

ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'
</div>

通过ifconfig eth0|grep bytes 得到输入输出的流量。

/@rac2=>dd2$ifconfig eth0|grep bytes
RX bytes:1638005313300 (1.4 TiB) TX bytes:3408060482049 (3.0 TiB)
</div>

再将结果通过awk 得出所要的字段值。
固定时间得到这些值,在写个循环计算一下就能得到网卡流量。
完整代码:

代码一:

#!/bin/bash
# 统计网卡流量
# link:www.jb51.net
# date:2013/2/26
n=10
 
date
rm -rf /tmp/ifconfig_log
while (( $n >= 0 ))
do
 n=$(($n - 1));
 date >> /tmp/ifconfig_log
 ifconfig eth1 >> /tmp/ifconfig_log
 sleep 1
done
 
grep "RX bytes:" /tmp/ifconfig_log | awk -F"[:| ]" '{print $13}' | awk 'BEGIN{tmp=$1}{if(FNR > 1)print $1-tmp}{tmp=$1}'
</div>

代码二:

#!/bin/bash
if [ -n "$1" ]; then
 eth_name=$1
else
 eth_name="eth0"
fi
i=0
send_o=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
recv_o=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
send_n=$send_o
recv_n=$recv_o
while [ $i -le 100000 ]; do
 send_l=$send_n
 recv_l=$recv_n
 sleep 1
 send_n=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
 recv_n=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
 i=`expr $i + 1`
 send_r=`expr $send_n - $send_l`
 recv_r=`expr $recv_n - $recv_l`
 total_r=`expr $send_r + $recv_r`
 send_ra=`expr \( $send_n - $send_o \) / $i`
 recv_ra=`expr \( $recv_n - $recv_o \) / $i`
 total_ra=`expr $send_ra + $recv_ra`
 sendn=`ifconfig $eth_name | grep bytes | awk -F \( '{print $3}' | awk -F \) '{print $1}'`
 recvn=`ifconfig $eth_name | grep bytes | awk -F \( '{print $2}' | awk -F \) '{print $1}'`
 clear
 echo "==================================================" 
 echo "Last second :  Send rate: $send_r Bytes/sec Recv rate: $recv_r Bytes/sec Total rate: $total_r Bytes/sec"
 echo "Average value:  Send rate: $send_ra Bytes/sec Recv rate: $recv_ra Bytes/sec Total rate: $total_ra Bytes/sec"
 echo "Total traffic after startup:  Send traffic: $sendn Recv traffic: $recvn"
 echo "=================================================="
done
</div>

代码三:

#!/bin/bash  
# Link: www.51bbo.com  
###  
while :  
do  
  Time=`date +%F” “%T.%N`  
  rx_before=`ifconfig eth0 |sed -n 8p |awk ‘{print $2}'| cut -c7-`  
  tx_before=`ifconfig eth0 |sed -n 8p |awk ‘{print $6}'| cut -c7-`  
  sleep 2  
  rx_after=`ifconfig eth0 |sed -n 8p |awk ‘{print $2}'| cut -c7-`  
  tx_after=`ifconfig eth0 |sed -n 8p |awk ‘{print $6}'| cut -c7-`  
 
  rx_result=$[(rx_after – rx_before)/512]  
  tx_result=$[(tx_after – tx_before)/512]  
  echo -e “$Time nNow_In_Speed: ‘$rx_result'Kbps Now_OUt_Speed: ‘$tx_result'Kbpsn”  
done
</div>

</div>

您可能想查找下面的文章:

  • 统计网卡流量的两段shell脚本(使用ifconfig)

相关文章

  • shell脚本中取消重定向的方法实例
  • Shell脚本实现监控swap空间使用情况和查看占用swap的进程
  • 利用linux的timerfd_create实现计时器示例分享
  • Shell脚本把文件从GBK转为UTF-8编码
  • Python执行Linux系统命令的4种方法
  • 一天一个shell命令 linux文本内容操作系列-cut命令详解
  • linux shell命令行参数用法详解
  • Shell脚本实现批量下载网络图片代码分享
  • C语言实现的ls命令源码分享
  • Shell脚本去重的几种方法实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • getcwd cannot access parent directories错误解决方法
    • 分享awk变量$0的妙用
    • 简介Linux中cp和mv搭配{,}在shell当中的用法
    • 后台实时分流文件的shell脚本
    • 完美解决ntp的错误问题no server suitable for synchronization fo
    • 检查linux网络状态的两个脚本
    • Centos下查看网卡的实时流量命令
    • rsync结合find技巧分享
    • linux rsync安装 配置 实例详解
    • Shell脚本遍历目录并批量修改文件编码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有