linux下監(jiān)控網(wǎng)絡(luò)流量的腳本
更新時間:2013年01月23日 15:11:11 作者:
在我們的日常工作當(dāng)中,監(jiān)控網(wǎng)絡(luò)要么使用iptarf,ifstat這些命令實現(xiàn)的,但是需要裝額外的RPM包。特別是iptarf裝起來麻煩
我看了下,linux下的/proc/net/dev記錄了每塊網(wǎng)卡發(fā)送和接受的包和字節(jié)數(shù)。因此萌生想法,寫了一個。運行效果:
[root@74-82-173-217 ~]# ./net.sh
Current Ip: inet addr:74.82.173.217 Bcast:74.82.173.223 Mask:255.255.255.224
Summry info: RX bytes:203692709 (194.2 MiB) TX bytes:93525930 (89.1 MiB)
eth0 Receive Bytes: 573 Packets: 3
eth0 Send Bytes: 3086 Packets: 3
eth0 Receive Bytes: 378 Packets: 7
eth0 Send Bytes: 11236 Packets: 7
eth0 Receive Bytes: 324 Packets: 6
eth0 Send Bytes: 444 Packets: 2
eth0 Receive Bytes: 54 Packets: 1
eth0 Send Bytes: 0 Packets: 0
具體腳本的內(nèi)容如下,幾乎不需要修改,就可以拿到任何機器上去使用了。
[root@74-82-173-217 ~]# cat net.sh
#! /bin/bash
#Author: Vogts WangTao 2008-12-18
#Get summry info
echo "Current Ip: "`/sbin/ifconfig eth0 | grep inet`
echo "Summry info: "`/sbin/ifconfig eth0 | grep bytes`
#sleep 1 second ,monitor eth0
while true
do
receive1=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'`
receive_pack1=`cat /proc/net/dev|grep eth0 | awk '{print$2}'`
send1=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`
send_pack1=`cat /proc/net/dev|grep eth0 | awk '{print$10}'`
sleep 1
receive2=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'`
receive_pack2=`cat /proc/net/dev|grep eth0 | awk '{print$2}'`
receive_cnt=`expr $receive2 - $receive1`
receive_pack_cnt=`expr $receive_pack2 - $receive_pack1`
send2=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`
send_pack2=`cat /proc/net/dev|grep eth0 | awk '{print$10}'`
send_cnt=`expr $send2 - $send1`
send_pack_cnt=`expr $send_pack2 - $send_pack1`
echo 'eth0 Receive Bytes:' $receive_cnt ' Packets:' $receive_pack_cnt
echo 'eth0 Send Bytes:' $send_cnt ' Packets:' $send_pack_cnt
done
復(fù)制代碼 代碼如下:
[root@74-82-173-217 ~]# ./net.sh
Current Ip: inet addr:74.82.173.217 Bcast:74.82.173.223 Mask:255.255.255.224
Summry info: RX bytes:203692709 (194.2 MiB) TX bytes:93525930 (89.1 MiB)
eth0 Receive Bytes: 573 Packets: 3
eth0 Send Bytes: 3086 Packets: 3
eth0 Receive Bytes: 378 Packets: 7
eth0 Send Bytes: 11236 Packets: 7
eth0 Receive Bytes: 324 Packets: 6
eth0 Send Bytes: 444 Packets: 2
eth0 Receive Bytes: 54 Packets: 1
eth0 Send Bytes: 0 Packets: 0
具體腳本的內(nèi)容如下,幾乎不需要修改,就可以拿到任何機器上去使用了。
復(fù)制代碼 代碼如下:
[root@74-82-173-217 ~]# cat net.sh
#! /bin/bash
#Author: Vogts WangTao 2008-12-18
#Get summry info
echo "Current Ip: "`/sbin/ifconfig eth0 | grep inet`
echo "Summry info: "`/sbin/ifconfig eth0 | grep bytes`
#sleep 1 second ,monitor eth0
while true
do
receive1=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'`
receive_pack1=`cat /proc/net/dev|grep eth0 | awk '{print$2}'`
send1=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`
send_pack1=`cat /proc/net/dev|grep eth0 | awk '{print$10}'`
sleep 1
receive2=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'`
receive_pack2=`cat /proc/net/dev|grep eth0 | awk '{print$2}'`
receive_cnt=`expr $receive2 - $receive1`
receive_pack_cnt=`expr $receive_pack2 - $receive_pack1`
send2=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`
send_pack2=`cat /proc/net/dev|grep eth0 | awk '{print$10}'`
send_cnt=`expr $send2 - $send1`
send_pack_cnt=`expr $send_pack2 - $send_pack1`
echo 'eth0 Receive Bytes:' $receive_cnt ' Packets:' $receive_pack_cnt
echo 'eth0 Send Bytes:' $send_cnt ' Packets:' $send_pack_cnt
done
您可能感興趣的文章:
- linux 雙網(wǎng)卡綁定網(wǎng)絡(luò)設(shè)置方法
- Linux網(wǎng)絡(luò)編程之基于UDP實現(xiàn)可靠的文件傳輸示例
- Linux下用netstat查看網(wǎng)絡(luò)狀態(tài)、端口狀態(tài)
- linux命令學(xué)習(xí)之10個網(wǎng)絡(luò)命令和監(jiān)控命令
- 檢查linux網(wǎng)絡(luò)狀態(tài)的兩個腳本
- Linux基本網(wǎng)絡(luò)配置方法介紹
- Linux 檢測服務(wù)器是否連接著網(wǎng)絡(luò)
- Linux的常用網(wǎng)絡(luò)命令
- Linux下的網(wǎng)絡(luò)監(jiān)聽技術(shù)之一
- Linux模擬網(wǎng)絡(luò)丟包與延遲的方法
相關(guān)文章
一文學(xué)會使用Linux內(nèi)核模塊&proc實例統(tǒng)計所有進程信息
這篇文章主要介紹了使用Linux內(nèi)核模塊&proc實例統(tǒng)計所有進程信息詳解,2023-05-05
淺析Shell腳本中的條件判斷[?]與[[?]]的區(qū)別
在編寫?Shell?腳本時,條件判斷是控制腳本流程的重要手段,Shell?提供了多種方式來進行條件判斷,其中[?]和[[?]]是兩種常見的條件測試表達式,下面我們就來看看兩者的區(qū)別吧2024-12-12
Shell中實現(xiàn)整數(shù)自增的幾種方法示例
Linux Shell中寫循環(huán)時,常常要用到變量的自增,下面這篇文章主要給大家分享了關(guān)于Shell中實現(xiàn)整數(shù)變量自增的幾種方法,包括declare -i來聲明、使用let命令、使用(())以及使用expr命令等等方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08

