-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkSpeedTest.sh
More file actions
executable file
·106 lines (97 loc) · 2.44 KB
/
NetworkSpeedTest.sh
File metadata and controls
executable file
·106 lines (97 loc) · 2.44 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /bin/bash
username="" # 用户名
ip="" # IP地址
port="22" # 端口号
n=5 # times
b=20 # b M size
my_times=1; # 循环条件
seconds=0; # 总时间
speed=0; # 上传速度
#获取参数
while getopts ":m:i:p:n:b:" opt
do
case $opt in
m)
username="$OPTARG"
;;
i)
echo $OPTARG|grep "^[0-2]\?[0-9]\?[0-9]\{1\}\.[0-2]\?[0-9]\?[0-9]\{1\}\.[0-2]\?[0-9]\?[0-9]\{1\}\.[0-2]\?[0-9]\?[0-9]\{1\}$" > /dev/null
if [ $? -eq 1 ];then
echo error ip,please enter correct
exit 1
else
ip="$OPTARG"
fi
;;
p)
echo $OPTARG|grep "^[0-9]\{1,5\}$" > /dev/null
if [ $? -eq 1 ];then
echo error port,please enter correct
exit 1
elif [ $OPTARG -gt 65535 ];then
echo error port,please enter correct
exit 1
else
port="$OPTARG"
fi
;;
n)
echo $OPTARG|grep "^[0-9]\+$" > /dev/null
if [ $? -eq 1 ];then
echo error times,please enter correct
exit 1
else
n="$OPTARG"
fi
;;
b)
echo $OPTARG|grep "^[0-9]\+$" > /dev/null
if [ $? -eq 1 ];then
echo error size,please enter correct
exit 1
else
b="$OPTARG"
fi
;;
?)
echo "参数错误 请参照以下显示操作"
printf;
echo "-m 输入用户名(默认为deploy)"
echo "-i 输入IP地址(默认为127.0.0.1)"
echo "-p 输入端口号(默认为22)"
echo "-n 输入发送次数(默认为5)"
echo "-b 输入发送文件大小(默认为20M)"
exit 1;;
esac
done
if [ ${#username} -lt 1 ];then
echo "用户名不能为空"
exit 1
elif [ ${#ip} -lt 1 ];then
echo "IP不能为空"
exit
fi
#创建需要跨域复制的测试文件
`dd if=/dev/zero of=/tmp/zerofile bs=1M count="$b"`
#循环复制 并进行时间计算
while (( $my_times <= $n ))
do
echo "第${my_times}次上传测试中..."
(time scp -P "$port" /tmp/zerofile "$username"@"$ip":/tmp/) 2> /tmp/a.txt
second=$(cat /tmp/a.txt | grep "real"|tail -n 1|awk '{print $2}' | awk 'BEGIN{FS="."}{print $1}' | awk 'BEGIN{FS="m"}{print $1*60+$2}')
sleep 1
let "seconds+=second"
let "my_times++"
done
#echo $b $n $seconds
#计算上传速度
if [ $seconds -eq 0 ];then
echo "上传速度过快,所用时间为小于1ms 无法计算"
exit 1
else
speed=$(printf "%.2f" `echo "scale=2; $b*$n/$seconds" | bc`)
#speed=$(echo $b $n $seconds | awk '{ printf "%0.2f\n" ,$1*$2/$3}')
fi
echo Spped is $speed"MB/s"
#删除本地创建的测试文件
`rm /tmp/zerofile`