Linux网卡流量监控脚本
作用: 实时监控网卡 进/出 流量
语法: bash netmon.sh 网卡名称 [可选参数]
[root@haimati ~]# cat netmon.sh
#!/bin/bash
<< Comment
作者: 秦子腾
邮箱: qinziteng05@163.com
日期: 2022-5-15 23:02
Comment
Format=$(printf "\033[33m\t 监控 ${1} 设备信息\033[0m\n")
KB(){
for ((;;));do
InOld=$(cat /proc/net/dev| awk /${1}/'{print $2}')
OutOld=$(cat /proc/net/dev| awk /${1}/'{print $10}')
sleep 1
InNew=$(cat /proc/net/dev| awk /${1}/'{print $2}')
OutNew=$(cat /proc/net/dev| awk /${1}/'{print $10}')
printf "\033[32m IN流量: %.1f %s\t Out流量: %.1f %s\033[0m\n" \
"$(((${InNew}-${InOld})/1024))" "KB/s" \
"$(((${OutNew}-${OutOld})/1024))" "KB/s"
done
}
MB(){
for ((;;));do
InOld=$(cat /proc/net/dev| awk /${1}/'{print $2}')
OutOld=$(cat /proc/net/dev| awk /${1}/'{print $10}')
sleep 1
InNew=$(cat /proc/net/dev| awk /${1}/'{print $2}')
OutNew=$(cat /proc/net/dev| awk /${1}/'{print $10}')
printf "\033[32m IN流量: %.1f %s\t Out流量: %.1f %s\033[0m\n" \
"$(((${InNew}-${InOld})/1024/1024))" "MB/s" \
"$(((${OutNew}-${OutOld})/1024/1024))" "MB/s"
done
}
if [ -z ${2} ];then
printf "${Format}\n"
KB ${1}
fi
case "${2}" in
"-k")
printf "${Format}\n"
KB ${1}
;;
"-m")
printf "${Format}\n"
MB ${1}
;;
*)
cat <<EOF
语法: bash ${0} 网卡名称 [参数]
-k: KB/s为单位输出(默认)
-m: MB/S为单位输出
EOF
esac
执行脚本:
chmod +x netmon.sh
./netmon.sh eth0
效果预览:
我这台云服务最高带宽是2M 所以测试最高也不会超过2MB/s