测速脚本

#!/usr/bin/env bash

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE="\033[0;35m"
CYAN='\033[0;36m'
PLAIN='\033[0m'

checkroot(){
[[ $EUID -ne 0 ]] && echo -e "${RED}请使用 root 用户运行本脚本!${PLAIN}" && exit 1
}

checksystem() {
if [ -f /etc/redhat-release ]; then
release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -Eqi "debian"; then
release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
release="centos"
fi
}

checkpython() {
if [ ! -e '/usr/bin/python' ]; then
echo "正在安装 Python"
if [ "${release}" == "centos" ]; then
yum update > /dev/null 2>&1
yum -y install python > /dev/null 2>&1
else
apt-get update > /dev/null 2>&1
apt-get -y install python > /dev/null 2>&1
fi

fi
}

checkcurl() {
if [ ! -e '/usr/bin/curl' ]; then
echo "正在安装 Curl"
if [ "${release}" == "centos" ]; then
yum update > /dev/null 2>&1
yum -y install curl > /dev/null 2>&1
else
apt-get update > /dev/null 2>&1
apt-get -y install curl > /dev/null 2>&1
fi
fi
}

checkwget() {
if [ ! -e '/usr/bin/wget' ]; then
echo "正在安装 Wget"
if [ "${release}" == "centos" ]; then
yum update > /dev/null 2>&1
yum -y install wget > /dev/null 2>&1
else
apt-get update > /dev/null 2>&1
apt-get -y install wget > /dev/null 2>&1
fi
fi
}

checkspeedtest() {
if [ ! -e './speedtest-cli/speedtest' ]; then
echo "正在安装 Speedtest-cli"
arch=$(uname -m)
if [ "${arch}" == "i686" ]; then
arch="i386"
fi
wget --no-check-certificate -qO speedtest.tgz https://cdn.jsdelivr.net/gh/oooldking/script@1.1.7/speedtest_cli/ookla-speedtest-1.0.0-${arch}-linux.tgz > /dev/null 2>&1
# wget --no-check-certificate -qO speedtest.tgz https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-${arch}-linux.tgz > /dev/null 2>&1
fi
mkdir -p speedtest-cli && tar zxvf speedtest.tgz -C ./speedtest-cli/ > /dev/null 2>&1 && chmod a+rx ./speedtest-cli/speedtest
}

speed_test(){
speedLog="./speedtest.log"
true > $speedLog
speedtest-cli/speedtest -p no -s $1 --accept-license > $speedLog 2>&1
is_upload=$(cat $speedLog | grep 'Upload')
if [[ ${is_upload} ]]; then
local REDownload=$(cat $speedLog | awk -F ' ' '/Download/{print $3}')
local reupload=$(cat $speedLog | awk -F ' ' '/Upload/{print $3}')
local relatency=$(cat $speedLog | awk -F ' ' '/Latency/{print $2}')

local nodeID=$1
local nodeLocation=$2
local nodeISP=$3

strnodeLocation="${nodeLocation}      "
LANG=C
#echo $LANG

temp=$(echo "${REDownload}" | awk -F ' ' '{print $1}')
if [[ $(awk -v num1=${temp} -v num2=0 'BEGIN{print(num1>num2)?"1":"0"}') -eq 1 ]]; then
printf "${RED}%-6s${YELLOW}%s%s${GREEN}%-24s${CYAN}%s%-10s${BLUE}%s%-10s${PURPLE}%-8s${PLAIN}\n" "${nodeID}" "${nodeISP}" "|" "${strnodeLocation:0:24}" "↑ " "${reupload}" "↓ " "${REDownload}" "${relatency}" | tee -a $log
fi
else
local cerror="ERROR"
fi
}

preinfo() {
echo "———————————————————SuperSpeed 全面测速版——————————————————"
echo " bash <(curl -Lso- https://git.io/superspeed.sh)"
echo " 全部节点列表: https://git.io/superspeedList"
echo " 节点更新: 2020/04/09 | 脚本更新: 2021/07/30"
echo "——————————————————————————————————————————————————————————"
}

selecttest() {
echo -e " 测速类型: ${GREEN}1.${PLAIN} 三网测速 ${GREEN}2.${PLAIN} 取消测速"
echo -e " ${GREEN}3.${PLAIN} 电信节点 ${GREEN}4.${PLAIN} 联通节点 ${GREEN}5.${PLAIN} 移动节点"
echo -ne " ${GREEN}6.${PLAIN} 详细三网测速"
while :; do echo
read -p " 请输入数字选择测速类型: " selection
if [[ ! $selection =~ ^[1-6]$ ]]; then
echo -ne " ${RED}输入错误${PLAIN}, 请输入正确的数字!"
else
break
fi
done
}

runtest() {
[[ ${selection} == 2 ]] && exit 1

if [[ ${selection} == 1 ]]; then
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)

speed_test '3633' '上海' '电信'
speed_test '24012' '内蒙古呼和浩特' '电信'
speed_test '27377' '北京5G' '电信'
speed_test '29026' '四川成都' '电信'
# speed_test '29071' '四川成都' '电信'
speed_test '17145' '安徽合肥5G' '电信'
speed_test '27594' '广东广州5G' '电信'
# speed_test '27810' '广西南宁' '电信'
speed_test '27575' '新疆乌鲁木齐' '电信'
# speed_test '26352' '江苏南京5G' '电信'
speed_test '5396' '江苏苏州5G' '电信'
# speed_test '5317' '江苏连云港5G' '电信'
# speed_test '7509' '浙江杭州' '电信'
# speed_test '23844' '湖北武汉' '电信'
speed_test '29353' '湖北武汉5G' '电信'
speed_test '28225' '湖南长沙5G' '电信'
speed_test '3973' '甘肃兰州' '电信'
# speed_test '19076' '重庆' '电信'
#***
# speed_test '21005' '上海' '联通'
speed_test '24447' '上海5G' '联通'
# speed_test '5103' '云南昆明' '联通'
# speed_test '5145' '北京' '联通'
# speed_test '5505' '北京' '联通'
# speed_test '9484' '吉林长春' '联通'
speed_test '2461' '四川成都' '联通'
speed_test '27154' '天津5G' '联通'
# speed_test '5509' '宁夏银川' '联通'
# speed_test '5724' '安徽合肥' '联通'
speed_test '5039' '山东济南' '联通'
speed_test '26180' '山东济南5G' '联通'
speed_test '26678' '广东广州5G' '联通'
speed_test '16192' '广东深圳' '联通'
# speed_test '6144' '新疆乌鲁木齐' '联通'
# speed_test '13704' '江苏南京' '联通'
speed_test '5485' '湖北武汉' '联通'
# speed_test '26677' '湖南株洲' '联通'
speed_test '4870' '湖南长沙' '联通'
# speed_test '4690' '甘肃兰州' '联通'
# speed_test '4884' '福建福州' '联通'
# speed_test '31985' '重庆' '联通'
speed_test '4863' '陕西西安' '联通'
#***
speed_test '30154' '上海' '移动'
# speed_test '25637' '上海5G' '移动'
# speed_test '26728' '云南昆明' '移动'
speed_test '27019' '内蒙古呼和浩特' '移动'
# speed_test '30232' '内蒙呼和浩特5G' '移动'
# speed_test '30293' '内蒙古通辽5G' '移动'
#speed_test '25858' '北京' '移动'
#speed_test '16375' '吉林长春' '移动'
speed_test '24337' '四川成都' '移动'
speed_test '17184' '天津5G' '移动'
speed_test '26940' '宁夏银川' '移动'
# speed_test '31815' '宁夏银川' '移动'
# speed_test '26404' '安徽合肥5G' '移动'
#speed_test '27151' '山东临沂5G' '移动'
# speed_test '25881' '山东济南5G' '移动'
# speed_test '27100' '山东青岛5G' '移动'
# speed_test '26501' '山西太原5G' '移动'
# speed_test '31520' '广东中山' '移动'
# speed_test '6611' '广东广州' '移动'
# speed_test '4515' '广东深圳' '移动'
speed_test '15863' '广西南宁' '移动'
# speed_test '16858' '新疆乌鲁木齐' '移动'
#speed_test '26938' '新疆乌鲁木齐5G' '移动'
# speed_test '17227' '新疆和田' '移动'
# speed_test '17245' '新疆喀什' '移动'
# speed_test '17222' '新疆阿勒泰' '移动'
speed_test '27249' '江苏南京5G' '移动'
speed_test '21845' '江苏常州5G' '移动'
# speed_test '26850' '江苏无锡5G' '移动'
# speed_test '17320' '江苏镇江5G' '移动'
#speed_test '25883' '江西南昌5G' '移动'
speed_test '17223' '河北石家庄' '移动'
speed_test '26331' '河南郑州5G' '移动'
# speed_test '6715' '浙江宁波5G' '移动'
# speed_test '4647' '浙江杭州' '移动'
# speed_test '16503' '海南海口' '移动'
# speed_test '28491' '湖南长沙5G' '移动'
# speed_test '16145' '甘肃兰州' '移动'
#speed_test '16171' '福建福州' '移动'
speed_test '18444' '西藏拉萨' '移动'
speed_test '16398' '贵州贵阳' '移动'
#speed_test '25728' '辽宁大连' '移动'
# speed_test '16167' '辽宁沈阳' '移动'
# speed_test '17584' '重庆' '移动'
speed_test '26380' '陕西西安' '移动'
# speed_test '29105' '陕西西安5G' '移动'
speed_test '29083' '青海西宁5G' '移动'
speed_test '26656' '黑龙江哈尔滨' '移动'

end=$(date +%s)
rm -rf speedtest*
echo "——————————————————————————————————————————————————————————"
time=$(( $end - $start ))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -ne " 测试完成, 本次测速耗时: ${min}${sec} 秒"
else
echo -ne " 测试完成, 本次测速耗时: ${time} 秒"
fi
echo -ne "\n 当前时间: "
echo $(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)
echo -e " ${GREEN}# 三网测速中为避免节点数不均及测试过久,每部分未使用所${PLAIN}"
echo -e " ${GREEN}# 有节点,如果需要使用全部节点,可分别选择三网节点检测${PLAIN}"
fi

if [[ ${selection} == 3 ]]; then
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)

speed_test '3633' '上海' '电信'
speed_test '24012' '内蒙古呼和浩特' '电信'
speed_test '27377' '北京5G' '电信'
speed_test '29026' '四川成都' '电信'
speed_test '29071' '四川成都' '电信'
speed_test '17145' '安徽合肥5G' '电信'
speed_test '27594' '广东广州5G' '电信'
speed_test '27810' '广西南宁' '电信'
speed_test '27575' '新疆乌鲁木齐' '电信'
speed_test '26352' '江苏南京5G' '电信'
speed_test '5396' '江苏苏州5G' '电信'
speed_test '5317' '江苏连云港5G' '电信'
speed_test '7509' '浙江杭州' '电信'
speed_test '23844' '湖北武汉' '电信'
speed_test '29353' '湖北武汉5G' '电信'
speed_test '28225' '湖南长沙5G' '电信'
speed_test '3973' '甘肃兰州' '电信'
speed_test '19076' '重庆' '电信'

end=$(date +%s)
rm -rf speedtest*
echo "——————————————————————————————————————————————————————————"
time=$(( $end - $start ))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -ne " 测试完成, 本次测速耗时: ${min}${sec} 秒"
else
echo -ne " 测试完成, 本次测速耗时: ${time} 秒"
fi
echo -ne "\n 当前时间: "
echo $(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)
fi

if [[ ${selection} == 4 ]]; then
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)

speed_test '21005' '上海' '联通'
speed_test '24447' '上海5G' '联通'
speed_test '5103' '云南昆明' '联通'
speed_test '5145' '北京' '联通'
speed_test '5505' '北京' '联通'
speed_test '9484' '吉林长春' '联通'
speed_test '2461' '四川成都' '联通'
speed_test '27154' '天津5G' '联通'
speed_test '5509' '宁夏银川' '联通'
speed_test '5724' '安徽合肥' '联通'
speed_test '5039' '山东济南' '联通'
speed_test '26180' '山东济南5G' '联通'
speed_test '26678' '广东广州5G' '联通'
speed_test '16192' '广东深圳' '联通'
speed_test '6144' '新疆乌鲁木齐' '联通'
speed_test '13704' '江苏南京' '联通'
speed_test '5485' '湖北武汉' '联通'
speed_test '26677' '湖南株洲' '联通'
speed_test '4870' '湖南长沙' '联通'
speed_test '4690' '甘肃兰州' '联通'
speed_test '4884' '福建福州' '联通'
speed_test '31985' '重庆' '联通'
speed_test '4863' '陕西西安' '联通'

end=$(date +%s)
rm -rf speedtest*
echo "——————————————————————————————————————————————————————————"
time=$(( $end - $start ))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -ne " 测试完成, 本次测速耗时: ${min}${sec} 秒"
else
echo -ne " 测试完成, 本次测速耗时: ${time} 秒"
fi
echo -ne "\n 当前时间: "
echo $(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)
fi

if [[ ${selection} == 5 ]]; then
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)

speed_test '30154' '上海' '移动'
speed_test '25637' '上海5G' '移动'
speed_test '26728' '云南昆明' '移动'
speed_test '27019' '内蒙古呼和浩特' '移动'
speed_test '30232' '内蒙呼和浩特5G' '移动'
speed_test '30293' '内蒙古通辽5G' '移动'
speed_test '25858' '北京' '移动'
speed_test '16375' '吉林长春' '移动'
speed_test '24337' '四川成都' '移动'
speed_test '17184' '天津5G' '移动'
speed_test '26940' '宁夏银川' '移动'
speed_test '31815' '宁夏银川' '移动'
speed_test '26404' '安徽合肥5G' '移动'
speed_test '27151' '山东临沂5G' '移动'
speed_test '25881' '山东济南5G' '移动'
speed_test '27100' '山东青岛5G' '移动'
speed_test '26501' '山西太原5G' '移动'
speed_test '31520' '广东中山' '移动'
speed_test '6611' '广东广州' '移动'
speed_test '4515' '广东深圳' '移动'
speed_test '15863' '广西南宁' '移动'
speed_test '16858' '新疆乌鲁木齐' '移动'
speed_test '26938' '新疆乌鲁木齐5G' '移动'
speed_test '17227' '新疆和田' '移动'
speed_test '17245' '新疆喀什' '移动'
speed_test '17222' '新疆阿勒泰' '移动'
speed_test '27249' '江苏南京5G' '移动'
speed_test '21845' '江苏常州5G' '移动'
speed_test '26850' '江苏无锡5G' '移动'
speed_test '17320' '江苏镇江5G' '移动'
speed_test '25883' '江西南昌5G' '移动'
speed_test '17223' '河北石家庄' '移动'
speed_test '26331' '河南郑州5G' '移动'
speed_test '6715' '浙江宁波5G' '移动'
speed_test '4647' '浙江杭州' '移动'
speed_test '16503' '海南海口' '移动'
speed_test '28491' '湖南长沙5G' '移动'
speed_test '16145' '甘肃兰州' '移动'
speed_test '16171' '福建福州' '移动'
speed_test '18444' '西藏拉萨' '移动'
speed_test '16398' '贵州贵阳' '移动'
speed_test '25728' '辽宁大连' '移动'
speed_test '16167' '辽宁沈阳' '移动'
speed_test '17584' '重庆' '移动'
speed_test '26380' '陕西西安' '移动'
speed_test '29105' '陕西西安5G' '移动'
speed_test '29083' '青海西宁5G' '移动'
speed_test '26656' '黑龙江哈尔滨' '移动'

end=$(date +%s)
rm -rf speedtest*
echo "——————————————————————————————————————————————————————————"
time=$(( $end - $start ))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -ne " 测试完成, 本次测速耗时: ${min}${sec} 秒"
else
echo -ne " 测试完成, 本次测速耗时: ${time} 秒"
fi
echo -ne "\n 当前时间: "
echo $(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)
fi

if [[ ${selection} == 6 ]]; then
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)

speed_test '3633' '上海' '电信'
speed_test '24012' '内蒙古呼和浩特' '电信'
speed_test '27377' '北京5G' '电信'
speed_test '29026' '四川成都' '电信'
speed_test '29071' '四川成都' '电信'
speed_test '17145' '安徽合肥5G' '电信'
speed_test '27594' '广东广州5G' '电信'
speed_test '27810' '广西南宁' '电信'
speed_test '27575' '新疆乌鲁木齐' '电信'
speed_test '26352' '江苏南京5G' '电信'
speed_test '5396' '江苏苏州5G' '电信'
speed_test '5317' '江苏连云港5G' '电信'
speed_test '7509' '浙江杭州' '电信'
speed_test '23844' '湖北武汉' '电信'
speed_test '29353' '湖北武汉5G' '电信'
speed_test '28225' '湖南长沙5G' '电信'
speed_test '3973' '甘肃兰州' '电信'
speed_test '19076' '重庆' '电信'
#***
speed_test '21005' '上海' '联通'
speed_test '24447' '上海5G' '联通'
speed_test '5103' '云南昆明' '联通'
speed_test '5145' '北京' '联通'
speed_test '5505' '北京' '联通'
speed_test '9484' '吉林长春' '联通'
speed_test '2461' '四川成都' '联通'
speed_test '27154' '天津5G' '联通'
speed_test '5509' '宁夏银川' '联通'
speed_test '5724' '安徽合肥' '联通'
speed_test '5039' '山东济南' '联通'
speed_test '26180' '山东济南5G' '联通'
speed_test '26678' '广东广州5G' '联通'
speed_test '16192' '广东深圳' '联通'
speed_test '6144' '新疆乌鲁木齐' '联通'
speed_test '13704' '江苏南京' '联通'
speed_test '5485' '湖北武汉' '联通'
speed_test '26677' '湖南株洲' '联通'
speed_test '4870' '湖南长沙' '联通'
speed_test '4690' '甘肃兰州' '联通'
speed_test '4884' '福建福州' '联通'
speed_test '31985' '重庆' '联通'
speed_test '4863' '陕西西安' '联通'
#***
speed_test '30154' '上海' '移动'
speed_test '25637' '上海5G' '移动'
speed_test '26728' '云南昆明' '移动'
speed_test '27019' '内蒙古呼和浩特' '移动'
speed_test '30232' '内蒙呼和浩特5G' '移动'
speed_test '30293' '内蒙古通辽5G' '移动'
speed_test '25858' '北京' '移动'
speed_test '16375' '吉林长春' '移动'
speed_test '24337' '四川成都' '移动'
speed_test '17184' '天津5G' '移动'
speed_test '26940' '宁夏银川' '移动'
speed_test '31815' '宁夏银川' '移动'
speed_test '26404' '安徽合肥5G' '移动'
speed_test '27151' '山东临沂5G' '移动'
speed_test '25881' '山东济南5G' '移动'
speed_test '27100' '山东青岛5G' '移动'
speed_test '26501' '山西太原5G' '移动'
speed_test '31520' '广东中山' '移动'
speed_test '6611' '广东广州' '移动'
speed_test '4515' '广东深圳' '移动'
speed_test '15863' '广西南宁' '移动'
speed_test '16858' '新疆乌鲁木齐' '移动'
speed_test '26938' '新疆乌鲁木齐5G' '移动'
speed_test '17227' '新疆和田' '移动'
speed_test '17245' '新疆喀什' '移动'
speed_test '17222' '新疆阿勒泰' '移动'
speed_test '27249' '江苏南京5G' '移动'
speed_test '21845' '江苏常州5G' '移动'
speed_test '26850' '江苏无锡5G' '移动'
speed_test '17320' '江苏镇江5G' '移动'
speed_test '25883' '江西南昌5G' '移动'
speed_test '17223' '河北石家庄' '移动'
speed_test '26331' '河南郑州5G' '移动'
speed_test '6715' '浙江宁波5G' '移动'
speed_test '4647' '浙江杭州' '移动'
speed_test '16503' '海南海口' '移动'
speed_test '28491' '湖南长沙5G' '移动'
speed_test '16145' '甘肃兰州' '移动'
speed_test '16171' '福建福州' '移动'
speed_test '18444' '西藏拉萨' '移动'
speed_test '16398' '贵州贵阳' '移动'
speed_test '25728' '辽宁大连' '移动'
speed_test '16167' '辽宁沈阳' '移动'
speed_test '17584' '重庆' '移动'
speed_test '26380' '陕西西安' '移动'
speed_test '29105' '陕西西安5G' '移动'
speed_test '29083' '青海西宁5G' '移动'
speed_test '26656' '黑龙江哈尔滨' '移动'

end=$(date +%s)
rm -rf speedtest*
echo "——————————————————————————————————————————————————————————"
time=$(( $end - $start ))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -ne " 测试完成, 本次测速耗时: ${min}${sec} 秒"
else
echo -ne " 测试完成, 本次测速耗时: ${time} 秒"
fi
echo -ne "\n 当前时间: "
echo $(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)
echo -e " ${GREEN}# 三网测速中为避免节点数不均及测试过久,每部分未使用所${PLAIN}"
echo -e " ${GREEN}# 有节点,如果需要使用全部节点,可分别选择三网节点检测${PLAIN}"
fi
}

runall() {
checkroot;
checksystem;
checkpython;
checkcurl;
checkwget;
checkspeedtest;
clear
speed_test;
preinfo;
selecttest;
runtest;
rm -rf speedtest*
}

runall

不能运行先运行下面

#!/usr/bin/env bash
#
# Description: A Bench Script by Teddysun
#
# Copyright (C) 2015 - 2022 Teddysun <i@teddysun.com>
# Thanks: LookBack <admin@dwhd.org>
# URL: https://teddysun.com/444.html
# https://github.com/teddysun/across/blob/master/bench.sh
#
trap _exit INT QUIT TERM

_red() {
printf '\033[0;31;31m%b\033[0m' "$1"
}

_green() {
printf '\033[0;31;32m%b\033[0m' "$1"
}

_yellow() {
printf '\033[0;31;33m%b\033[0m' "$1"
}

_blue() {
printf '\033[0;31;36m%b\033[0m' "$1"
}

_exists() {
local cmd="$1"
if eval type type > /dev/null 2>&1; then
eval type "$cmd" > /dev/null 2>&1
elif command > /dev/null 2>&1; then
command -v "$cmd" > /dev/null 2>&1
else
which "$cmd" > /dev/null 2>&1
fi
local rt=$?
return ${rt}
}

_exit() {
_red "\nThe script has been terminated.\n"
# clean up
rm -fr speedtest.tgz speedtest-cli benchtest_*
exit 1
}

get_opsy() {
[ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return
[ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
[ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}

next() {
printf "%-70s\n" "-" | sed 's/\s/-/g'
}

speed_test() {
local nodeName="$2"
[ -z "$1" ] && ./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr > ./speedtest-cli/speedtest.log 2>&1 || \
./speedtest-cli/speedtest --progress=no --server-id=$1 --accept-license --accept-gdpr > ./speedtest-cli/speedtest.log 2>&1
if [ $? -eq 0 ]; then
local dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
local up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
local latency=$(awk '/Latency/{print $2" "$3}' ./speedtest-cli/speedtest.log)
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
printf "\033[0;33m%-18s\033[0;32m%-18s\033[0;31m%-20s\033[0;36m%-12s\033[0m\n" " ${nodeName}" "${up_speed}" "${dl_speed}" "${latency}"
fi
fi
}

speed() {
speed_test '' 'Speedtest.net'
speed_test '21541' 'Los Angeles, US'
speed_test '43860' 'Dallas, US'
speed_test '40879' 'Montreal, CA'
speed_test '24215' 'Paris, FR'
speed_test '28922' 'Amsterdam, NL'
speed_test '24447' 'Shanghai, CN'
speed_test '26352' 'Nanjing, CN'
speed_test '27594' 'Guangzhou, CN'
speed_test '32155' 'Hongkong, CN'
speed_test '6527' 'Seoul, KR'
speed_test '7311' 'Singapore, SG'
speed_test '21569' 'Tokyo, JP'
}

io_test() {
(LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count=$1 conv=fdatasync && rm -f benchtest_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
}

calc_size() {
local raw=$1
local total_size=0
local num=1
local unit="KB"
if ! [[ ${raw} =~ ^[0-9]+$ ]] ; then
echo ""
return
fi
if [ "${raw}" -ge 1073741824 ]; then
num=1073741824
unit="TB"
elif [ "${raw}" -ge 1048576 ]; then
num=1048576
unit="GB"
elif [ "${raw}" -ge 1024 ]; then
num=1024
unit="MB"
elif [ "${raw}" -eq 0 ]; then
echo "${total_size}"
return
fi
total_size=$( awk 'BEGIN{printf "%.1f", '$raw' / '$num'}' )
echo "${total_size} ${unit}"
}

check_virt(){
_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)"
if _exists "dmidecode"; then
sys_manu="$(dmidecode -s system-manufacturer 2>/dev/null)"
sys_product="$(dmidecode -s system-product-name 2>/dev/null)"
sys_ver="$(dmidecode -s system-version 2>/dev/null)"
else
sys_manu=""
sys_product=""
sys_ver=""
fi
if grep -qa docker /proc/1/cgroup; then
virt="Docker"
elif grep -qa lxc /proc/1/cgroup; then
virt="LXC"
elif grep -qa container=lxc /proc/1/environ; then
virt="LXC"
elif [[ -f /proc/user_beancounters ]]; then
virt="OpenVZ"
elif [[ "${virtualx}" == *kvm-clock* ]]; then
virt="KVM"
elif [[ "${sys_product}" == *KVM* ]]; then
virt="KVM"
elif [[ "${cname}" == *KVM* ]]; then
virt="KVM"
elif [[ "${cname}" == *QEMU* ]]; then
virt="KVM"
elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; then
virt="VMware"
elif [[ "${sys_product}" == *"VMware Virtual Platform"* ]]; then
virt="VMware"
elif [[ "${virtualx}" == *"Parallels Software International"* ]]; then
virt="Parallels"
elif [[ "${virtualx}" == *VirtualBox* ]]; then
virt="VirtualBox"
elif [[ -e /proc/xen ]]; then
if grep -q "control_d" "/proc/xen/capabilities" 2>/dev/null; then
virt="Xen-Dom0"
else
virt="Xen-DomU"
fi
elif [ -f "/sys/hypervisor/type" ] && grep -q "xen" "/sys/hypervisor/type"; then
virt="Xen"
elif [[ "${sys_manu}" == *"Microsoft Corporation"* ]]; then
if [[ "${sys_product}" == *"Virtual Machine"* ]]; then
if [[ "${sys_ver}" == *"7.0"* || "${sys_ver}" == *"Hyper-V" ]]; then
virt="Hyper-V"
else
virt="Microsoft Virtual Machine"
fi
fi
else
virt="Dedicated"
fi
}

ipv4_info() {
local org="$(wget -q -T10 -O- ipinfo.io/org)"
local city="$(wget -q -T10 -O- ipinfo.io/city)"
local country="$(wget -q -T10 -O- ipinfo.io/country)"
local region="$(wget -q -T10 -O- ipinfo.io/region)"
if [[ -n "$org" ]]; then
echo " Organization : $(_blue "$org")"
fi
if [[ -n "$city" && -n "country" ]]; then
echo " Location : $(_blue "$city / $country")"
fi
if [[ -n "$region" ]]; then
echo " Region : $(_yellow "$region")"
fi
if [[ -z "$org" ]]; then
echo " Region : $(_red "No ISP detected")"
fi
}

install_speedtest() {
if [ ! -e "./speedtest-cli/speedtest" ]; then
sys_bit=""
local sysarch="$(uname -m)"
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
local sysarch="$(arch)"
fi
if [ "${sysarch}" = "x86_64" ]; then
sys_bit="x86_64"
fi
if [ "${sysarch}" = "i386" ] || [ "${sysarch}" = "i686" ]; then
sys_bit="i386"
fi
if [ "${sysarch}" = "armv8" ] || [ "${sysarch}" = "armv8l" ] || [ "${sysarch}" = "aarch64" ] || [ "${sysarch}" = "arm64" ]; then
sys_bit="aarch64"
fi
if [ "${sysarch}" = "armv7" ] || [ "${sysarch}" = "armv7l" ]; then
sys_bit="armhf"
fi
if [ "${sysarch}" = "armv6" ]; then
sys_bit="armel"
fi
[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.1.1-linux-${sys_bit}.tgz"
url2="https://dl.lamp.sh/files/ookla-speedtest-1.1.1-linux-${sys_bit}.tgz"
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}
if [ $? -ne 0 ]; then
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}
[ $? -ne 0 ] && _red "Error: Failed to download speedtest-cli.\n" && exit 1
fi
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
rm -f speedtest.tgz
fi
printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"
}

print_intro() {
echo "-------------------- A Bench.sh Script By Teddysun -------------------"
echo " Version : $(_green v2022-06-01)"
echo " Usage : $(_red "wget -qO- bench.sh | bash")"
}

# Get System information
get_system_info() {
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
cores=$( awk -F: '/processor/ {core++} END {print core}' /proc/cpuinfo )
freq=$( awk -F'[ :]' '/cpu MHz/ {print $4;exit}' /proc/cpuinfo )
ccache=$( awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
cpu_aes=$( grep -i 'aes' /proc/cpuinfo )
cpu_virt=$( grep -Ei 'vmx|svm' /proc/cpuinfo )
tram=$( LANG=C; free | awk '/Mem/ {print $2}' )
tram=$( calc_size $tram )
uram=$( LANG=C; free | awk '/Mem/ {print $3}' )
uram=$( calc_size $uram )
swap=$( LANG=C; free | awk '/Swap/ {print $2}' )
swap=$( calc_size $swap )
uswap=$( LANG=C; free | awk '/Swap/ {print $3}' )
uswap=$( calc_size $uswap )
up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime )
if _exists "w"; then
load=$( LANG=C; w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
elif _exists "uptime"; then
load=$( LANG=C; uptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
fi
opsy=$( get_opsy )
arch=$( uname -m )
if _exists "getconf"; then
lbit=$( getconf LONG_BIT )
else
echo ${arch} | grep -q "64" && lbit="64" || lbit="32"
fi
kern=$( uname -r )
disk_total_size=$( LANG=C; df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $2 }' )
disk_total_size=$( calc_size $disk_total_size )
disk_used_size=$( LANG=C; df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $3 }' )
disk_used_size=$( calc_size $disk_used_size )
tcpctrl=$( sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}' )
}
# Print System information
print_system_info() {
if [ -n "$cname" ]; then
echo " CPU Model : $(_blue "$cname")"
else
echo " CPU Model : $(_blue "CPU model not detected")"
fi
if [ -n "$freq" ]; then
echo " CPU Cores : $(_blue "$cores @ $freq MHz")"
else
echo " CPU Cores : $(_blue "$cores")"
fi
if [ -n "$ccache" ]; then
echo " CPU Cache : $(_blue "$ccache")"
fi
if [ -n "$cpu_aes" ]; then
echo " AES-NI : $(_green "Enabled")"
else
echo " AES-NI : $(_red "Disabled")"
fi
if [ -n "$cpu_virt" ]; then
echo " VM-x/AMD-V : $(_green "Enabled")"
else
echo " VM-x/AMD-V : $(_red "Disabled")"
fi
echo " Total Disk : $(_yellow "$disk_total_size") $(_blue "($disk_used_size Used)")"
echo " Total Mem : $(_yellow "$tram") $(_blue "($uram Used)")"
if [ "$swap" != "0" ]; then
echo " Total Swap : $(_blue "$swap ($uswap Used)")"
fi
echo " System uptime : $(_blue "$up")"
echo " Load average : $(_blue "$load")"
echo " OS : $(_blue "$opsy")"
echo " Arch : $(_blue "$arch ($lbit Bit)")"
echo " Kernel : $(_blue "$kern")"
echo " TCP CC : $(_yellow "$tcpctrl")"
echo " Virtualization : $(_blue "$virt")"
}

print_io_test() {
freespace=$( df -m . | awk 'NR==2 {print $4}' )
if [ -z "${freespace}" ]; then
freespace=$( df -m . | awk 'NR==3 {print $3}' )
fi
if [ ${freespace} -gt 1024 ]; then
writemb=2048
io1=$( io_test ${writemb} )
echo " I/O Speed(1st run) : $(_yellow "$io1")"
io2=$( io_test ${writemb} )
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
io3=$( io_test ${writemb} )
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
ioraw1=$( echo $io1 | awk 'NR==1 {print $1}' )
[ "`echo $io1 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw1=$( awk 'BEGIN{print '$ioraw1' * 1024}' )
ioraw2=$( echo $io2 | awk 'NR==1 {print $1}' )
[ "`echo $io2 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw2=$( awk 'BEGIN{print '$ioraw2' * 1024}' )
ioraw3=$( echo $io3 | awk 'NR==1 {print $1}' )
[ "`echo $io3 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw3=$( awk 'BEGIN{print '$ioraw3' * 1024}' )
ioall=$( awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}' )
ioavg=$( awk 'BEGIN{printf "%.1f", '$ioall' / 3}' )
echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"
else
echo " $(_red "Not enough space for I/O Speed test!")"
fi
}

print_end_time() {
end_time=$(date +%s)
time=$(( ${end_time} - ${start_time} ))
if [ ${time} -gt 60 ]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo " Finished in : ${min} min ${sec} sec"
else
echo " Finished in : ${time} sec"
fi
date_time=$(date '+%Y-%m-%d %H:%M:%S %Z')
echo " Timestamp : $date_time"
}

! _exists "wget" && _red "Error: wget command not found.\n" && exit 1
! _exists "free" && _red "Error: free command not found.\n" && exit 1
start_time=$(date +%s)
get_system_info
check_virt
clear
print_intro
next
print_system_info
ipv4_info
next
print_io_test
next
install_speedtest && speed && rm -fr speedtest-cli
next
print_end_time
next