点击登录
  • 欢迎访问无限星辰技术博客,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏无限星辰吧
  • 无限星辰的淘宝店铺已经开张了哦,传送门:http://

Linux 监控网卡流量脚本

LINUX服务器 crx349 2039次浏览 0个评论 扫描二维码

jkll.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /bin/bash 
#Author: xmspace 
#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

脚本二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
#edit by xmspace.net
usage() {
  echo "Useage : $0"
  echo "eg. sh $0 eth0 2"
  exit 1
}
 
if [ $# -lt 2 ]
then
   usage
fi
 
eth=$1
timer=$2
 
in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $1 }')
out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $9 }')
 
while true
do
   sleep ${timer}
   in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $1 }')
   out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{print $9 }')
   dif_in=$(((in-in_old)/timer))
   dif_in=$((dif_in/1024))
   dif_out=$(((out-out_old)/timer))
   dif_out=$((dif_out/1024))
   ct=$(date +"%F %H:%M:%S")
   echo "${ct} -- IN: ${dif_in} KByte/s     OUT: ${dif_out} KByte/s"
   in_old=${in}
   out_old=${out}
done
exit 0

无限星辰 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Linux 监控网卡流量脚本
喜欢 (0)
[]
分享 (0)

您必须 登录 才能发表评论!