shell脚本实时监控网站流量
内容如下:vim liuliang.sh
[*]#!/bin/bash
[*]
[*]while [ "1" ]
[*]do
[*]eth=eth1
[*]RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
[*]TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
[*]#echo $RXpre
[*]#echo $TXpre
[*]sleep 1
[*]RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
[*]TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
[*]#echo $RXnext
[*]#echo $TXnext
[*]clear
[*]echo -e "\t RX `date +%k:%M:%S` TX"
[*]RX=$((${RXnext}-${RXpre}))
[*]TX=$((${TXnext}-${TXpre}))
[*]if [[ $RX -lt 1024 ]];then
[*]RX="${RX}B/s"
[*]elif [[ $RX -gt 1048576 ]];then
[*]RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
[*]else
[*]RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
[*]fi
[*]if [[ $TX -lt 1024 ]];then
[*]TX="${TX}B/s"
[*]elif [[ $TX -gt 1048576 ]];then
[*]TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
[*]else
[*]TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
[*]fi
[*]
[*]echo -e "$eth \t $RX $TX "
[*]done
[*]
先添加执行权限:
chmod u+x liuliang.sh
在测试:
./liuliang.sh
页:
[1]