linux网速监控脚本

niaiai 2020-05-22 AM 54℃ 0条

保存到net.sh文件,添加可执行权限,使用方法net.sh 网卡名称

#!/bin/bash

ethn=$1

if [ $# != 1 ];then
        echo "USEAGE: $0 enthn"
        echo "e.g.: $0 ens33"
        exit 1;
fi

while true
do
    RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
    TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
    sleep 1
    RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
    TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')

    clear
    echo -e "\t RX `date +%k:%M:%S` TX"

    RX=$((${RX_next}-${RX_pre}))
    TX=$((${TX_next}-${TX_pre}))

    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 "$ethn \t $RX $TX "
done
标签: dev, 文件, 权限, date, awk, print, echo, ethn, then

非特殊说明,本博所有文章均为博主原创。

评论啦~