03.Linux网速脚本

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash

ethn=$1

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

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 "* * * * * * * * * * * * *"
echo -e "*\t\t\t*"
echo -e "*\t当前时间\t*"
echo -e "*\t`date +%k:%M:%S`\t*"
echo -e "*\t\t\t*"
echo "* * * * * * * * * * * * *"
echo ""
echo "网卡:"
echo "$ethn"
echo ""
echo -e "下行(RX)"
echo -e "$RX"
echo ""
echo -e "上行(TX)"
echo -e "$TX"
echo ""

done

使用方法:

1
./netspeed.sh eth0
  • Copyrights © 2023-2024 kukusun