linux下監(jiān)控網(wǎng)絡(luò)流量的腳本
更新時(shí)間:2013年01月23日 15:11:11 作者:
在我們的日常工作當(dāng)中,監(jiān)控網(wǎng)絡(luò)要么使用iptarf,ifstat這些命令實(shí)現(xiàn)的,但是需要裝額外的RPM包。特別是iptarf裝起來(lái)麻煩
我看了下,linux下的/proc/net/dev記錄了每塊網(wǎng)卡發(fā)送和接受的包和字節(jié)數(shù)。因此萌生想法,寫了一個(gè)。運(yùn)行效果:
[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)容如下,幾乎不需要修改,就可以拿到任何機(jī)器上去使用了。
[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)容如下,幾乎不需要修改,就可以拿到任何機(jī)器上去使用了。
復(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實(shí)現(xiàn)可靠的文件傳輸示例
- Linux下用netstat查看網(wǎng)絡(luò)狀態(tài)、端口狀態(tài)
- linux命令學(xué)習(xí)之10個(gè)網(wǎng)絡(luò)命令和監(jiān)控命令
- 檢查linux網(wǎng)絡(luò)狀態(tài)的兩個(gè)腳本
- Linux基本網(wǎng)絡(luò)配置方法介紹
- Linux 檢測(cè)服務(wù)器是否連接著網(wǎng)絡(luò)
- Linux的常用網(wǎng)絡(luò)命令
- Linux下的網(wǎng)絡(luò)監(jiān)聽技術(shù)之一
- Linux模擬網(wǎng)絡(luò)丟包與延遲的方法
相關(guān)文章
linux生成(加載)動(dòng)態(tài)庫(kù)靜態(tài)庫(kù)和加載示例方法
這篇文章主要介紹了linux生成(加載)動(dòng)態(tài)庫(kù)靜態(tài)庫(kù)示例方法,大家參考使用2013-11-11對(duì)Linux下shell編程之for循環(huán)的實(shí)例講解
今天小編就為大家分享一篇對(duì)Linux下shell編程之for循環(huán)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06一些Linux Shell中的權(quán)限相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了一些Linux Shell中的權(quán)限相關(guān)知識(shí)總結(jié),使Linux入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-07-07