Linux公网测速脚本,可以用于路由器或者服务器

测度测试

这个脚本是我以前做路由器开发时,用来给路由器测试上传以及下载速度的,当然也可以用于服务器的公网测试。是用shell写的,测试还是比较准确的。

#!/bin/sh
speed_result=/tmp/testspeed.txt

/usr/bin/curl --connect-timeout 2 -o /tmp/1.txt -s https://nodes.speedtest.cn/?https=1&browser=1&page=1&lat=22.71986&lon=114.24771&q=
sleep 1 
fasted_site=$(cat /tmp/1.txt  | sed 's/https:/\n/g' | sed 's/Url/\n/g' | grep "/hello" | awk -F '","' '{print $1}' | sed 's/\\\//\//g' | awk -F "/" '{print "http://"$3"/hello"}' | xargs curl --connect-timeout 2 -r 0-1048576 -L -w "%{speed_download}--%{http_code}--%{url_effective}--" -s | sort -n | grep "\-\-200--" | grep "\-\-Client"  -v | head -1 | awk -F "--" '{print $3}' | awk -F "/" '{print $3}')

echo $fasted_site

download_speed=$(curl --connect-timeout 10 -m 40 -L  -o/dev/null  http://$fasted_site/download?size=1000000000 >/tmp/downinfo.txt  2>&1 ; highest_m=`sed "s/\r/\n/g" /tmp/downinfo.txt | awk '{print $12}' | grep "M" | sed 's/M//g' | awk '{sum += $1} END {printf "NR = %d,Average = %3.3f\n",NR,sum/NR}' | rev | awk '{print "M"$1}' | rev` && highest_k=`sed "s/\r/\n/g" /tmp/downinfo.txt | awk '{print $12}' | grep "k" | sed 's/k//g' | awk '{sum += $1} END {printf "NR = %d,Average = %3.3f\n",NR,sum/NR}' | rev | awk '{print "k"$1}' | rev` && test -n "$highest_m" && echo $highest_m || echo $highest_k)


rm -f /tmp/test.jpg
upload_speed=$(dd if=/dev/zero of=/tmp/test.jpg bs=1k count=1000000 >/dev/null 2>&1 ; (time curl --connect-timeout 10 -m 30 -X POST -F'image=@/tmp/test.jpg' http://$fasted_site/upload?r=0.6127615031910414) >/tmp/uploadinfo.txt  2>&1 ; highest_m=`sed "s/\r/\n/g" /tmp/uploadinfo.txt | awk '{print $12}' | grep "M" | sed 's/M//g' | awk '{sum += $1} END {printf "NR = %d,Average = %3.3f\n",NR,sum/NR}' | rev | awk '{print "M"$1}' | rev` && highest_k=`sed "s/\r/\n/g" /tmp/uploadinfo.txt | awk '{print $12}' | grep "k" | sed 's/k//g' | awk '{sum += $1} END {printf "NR = %d,Average = %3.3f\n",NR,sum/NR}' | rev | awk '{print "k"$1}' | rev` && test -n "$highest_m" && echo $highest_m || echo $highest_k)
echo "download_speed:"$download_speed >> $speed_result
echo "upload_speed:"$upload_speed >> $speed_result

Linux公网测速脚本,可以用于路由器或者服务器_第1张图片

如上图所示,运行后选择的是5g.shunicomtest.com.prod.hosts.ooklaserver.net:8080这个测速站点,fasted_site选择的是一个最慢站点,如果要选最快的,可以把这一行代码的sort -n 改成sort -nr 。测速结果保存在/tmp/testspeed.txt这个文件,打开这个文件后,就可以看到速度是15.160M的下行,15.926的上行。单位是B,不是b。如果要转换成b,直接乘以8就可以了。

你可能感兴趣的:(linux系统运维,服务器,linux,运维)