Browse Source

refactor: detection script

master
dnomd343 3 years ago
parent
commit
2350581406
  1. 1
      .gitignore
  2. 179
      bench.sh
  3. 223
      cn.sh

1
.gitignore

@ -0,0 +1 @@
/.idea/

179
bench.sh

@ -0,0 +1,179 @@
#!/usr/bin/env bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE="\033[0;35m"
CYAN='\033[0;36m'
SUFFIX='\033[0m'
check_system() {
if [ -f /etc/redhat-release ]; then
PACKAGE_TYPE="RPM"
elif grep -qi 'alpine' /etc/issue; then
PACKAGE_TYPE="APK"
elif grep -qi 'debian\|ubuntu' /etc/issue; then
PACKAGE_TYPE="DEB"
elif grep -qi 'centos\|red hat\|redhat' /etc/issue; then
PACKAGE_TYPE="RPM"
elif grep -qi 'alpine' /proc/version; then
PACKAGE_TYPE="APK"
elif grep -qi 'debian\|ubuntu' /proc/version; then
PACKAGE_TYPE="DEB"
elif grep -qi 'centos\|red hat\|redhat' /proc/version; then
PACKAGE_TYPE="RPM"
else
echo -e "${RED}Warning: Unknown package management${SUFFIX}"
fi
}
curl_setup() {
if type curl > /dev/null 2>&1; then return; fi
echo -n "Installing curl..."
if [ $EUID -ne 0 ]; then
echo -e "\n${RED}Error: You must be root user${SUFFIX}"
exit 1
fi
if [ "${PACKAGE_TYPE}" == "RPM" ]; then
yum update > /dev/null 2>&1 && yum -y install curl > /dev/null 2>&1
elif [ "${PACKAGE_TYPE}" == "DEB" ]; then
apt update > /dev/null 2>&1 && apt -y install curl > /dev/null 2>&1
elif [ "${PACKAGE_TYPE}" == "APK" ]; then
apk update > /dev/null 2>&1 && apk add curl > /dev/null 2>&1
else
echo -e "\n${RED}Error: Unknown package management${SUFFIX}"
echo -e "Please install ${YELLOW}curl${SUFFIX} manual"
exit 1
fi
if type curl > /dev/null 2>&1; then
echo -e "${GREEN}OK${SUFFIX}"
else
echo -e "${RED}ERROR${SUFFIX}"
exit 1
fi
}
speedtest_setup() {
[[ -e './st-temp/speedtest' ]] && return
echo -n "Getting speedtest-cli..."
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54"
html=$(curl -sL "https://www.speedtest.net/apps/cli" --user-agent "${user_agent}")
version=$(echo "$html" | grep -oP 'ookla-speedtest-.*?-' | head -n 1 | cut -b 17- | rev | cut -c 2- | rev)
url="https://install.speedtest.net/app/cli/ookla-speedtest-${version}-linux-$(uname -m).tgz"
mkdir ./st-temp/ && cd ./st-temp || exit 1
curl "${url}" -o ./speedtest.tgz > /dev/null 2>&1
tar xf ./speedtest.tgz speedtest && rm -f ./speedtest.tgz
cd ../
if [ -e './st-temp/speedtest' ]; then
echo -e "${GREEN}OK${SUFFIX}"
else
echo -e "${RED}ERROR${SUFFIX}"
exit 1
fi
}
server_list() {
echo -n "Getting server list..."
rm -f ./st-temp/*.list
curl -sL "https://st.343.re/cn/ALL.dat" > ./st-temp/ALL.list
curl -sL "https://st.343.re/cn/DX.dat" > ./st-temp/DX.list
curl -sL "https://st.343.re/cn/LT.dat" > ./st-temp/LT.list
curl -sL "https://st.343.re/cn/YD.dat" > ./st-temp/YD.list
[[ ! -e './st-temp/ALL.list' ]] && echo -e "${RED}ERROR${SUFFIX}" && clear_env && exit 2
[[ ! -e './st-temp/DX.list' ]] && echo -e "${RED}ERROR${SUFFIX}" && clear_env && exit 2
[[ ! -e './st-temp/LT.list' ]] && echo -e "${RED}ERROR${SUFFIX}" && clear_env && exit 2
[[ ! -e './st-temp/YD.list' ]] && echo -e "${RED}ERROR${SUFFIX}" && clear_env && exit 2
echo -e "${GREEN}OK${SUFFIX}"
}
load_servers() {
local dat
while read -r line
do
dat=(${line//,/ })
id_arr[${#id_arr[@]}+1]=${dat[0]}
addr_arr[${#addr_arr[@]}+1]=${dat[1]}
isp_arr[${#isp_arr[@]}+1]=${dat[2]}
done <<< "$(cat "$1")"
server_num=${#id_arr[@]}
}
select_isp() {
echo -e "${GREEN}1.${SUFFIX} 三网测速"
echo -e "${GREEN}2.${SUFFIX} 电信节点"
echo -e "${GREEN}3.${SUFFIX} 联通节点"
echo -e "${GREEN}4.${SUFFIX} 移动节点"
echo -e "${GREEN}5.${SUFFIX} 取消测速\c"
while :; do echo
read -rp "请选择: " selection
if [[ ! $selection =~ ^[1-5]$ ]]; then
echo -e "${RED}输入无效${SUFFIX}\c"
else
break
fi
done
[[ ${selection} == 5 ]] && clear_env && exit 1
[[ ${selection} == 1 ]] && load_servers './st-temp/ALL.list'
[[ ${selection} == 2 ]] && load_servers './st-temp/DX.list'
[[ ${selection} == 3 ]] && load_servers './st-temp/LT.list'
[[ ${selection} == 4 ]] && load_servers './st-temp/YD.list'
}
speedtest() {
speed_log="./st-temp/speedtest.log"
LANG=C
echo -e "\r\c"
touch $speed_log
node_loc="$2      "
./st-temp/speedtest -p no -s "$1" --accept-license > $speed_log 2>&1
if grep 'Upload' "$speed_log" > /dev/null 2>&1; then
latency=$(awk -F " " '/Latency/{print $2}' "$speed_log")
download=$(awk -F " " '/Download/{print $3}' "$speed_log")
upload=$(awk -F " " '/Upload/{print $3}' "$speed_log")
temp=$(echo "${download}" | 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${SUFFIX}\n" \
"$1" "$3" "|" "${node_loc:0:24}" "↑ " "${upload}" "↓ " "${download}" "${latency}"
fi
else
printf "${RED}%-6s${YELLOW}%s%s${GREEN}%-24s${RED}%s${SUFFIX}\n" "$1" "$3" "|" "${node_loc:0:24}" "ERROR"
fi
}
start_test() {
echo -e "\033[?25l\c"
echo "——————————————————————————————————————————————————————————"
curl "ip.343.re/info"
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)
for ((i=1; i<=server_num; i++)) do
echo -e "正在测试 ${YELLOW}${isp_arr[i]}|${SUFFIX}${GREEN}${addr_arr[i]}${SUFFIX} ...\c"
speedtest "${id_arr[i]}" "${addr_arr[i]}" "${isp_arr[i]}"
done
end=$(date +%s)
echo "——————————————————————————————————————————————————————————"
time=$((end - start))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -e "测试完成, 共耗时${CYAN} ${min}${sec}${SUFFIX}"
else
echo -e "测试完成, 共耗时${CYAN} ${time}${SUFFIX}"
fi
echo -e "\033[?25h"
}
clear_env() {
rm -rf ./st-temp
}
check_system
curl_setup
speedtest_setup
server_list
select_isp
clear
start_test
clear_env

223
cn.sh

@ -1,223 +0,0 @@
#!/usr/bin/env bash
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'
check_root(){
[[ $EUID -ne 0 ]] && echo -e "${RED}请切换为root用户执行${PLAIN}" && exit 1
}
check_system() {
if [ -f /etc/redhat-release ]; then
package_type="RPM"
elif cat /etc/issue | grep -Eqi "debian"; then
package_type="DEB"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
package_type="DEB"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
package_type="RPM"
elif cat /proc/version | grep -Eqi "debian"; then
package_type="DEB"
elif cat /proc/version | grep -Eqi "ubuntu"; then
package_type="DEB"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
package_type="RPM"
fi
}
python_setup() {
[[ -e '/usr/bin/python' ]] && return
echo -e "正在安装Python...\c"
if [ "${package_type}" == "RPM" ]; 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
if [ -e '/usr/bin/python' ]; then
echo -e "${GREEN}OK${PLAIN}"
else
echo -e "${RED}ERROR${PLAIN}"
exit 1
fi
}
curl_setup() {
[[ -e '/usr/bin/curl' ]] && return
echo -e "正在安装Curl...\c"
if [ "${package_type}" == "RPM" ]; 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
if [ -e '/usr/bin/curl' ]; then
echo -e "${GREEN}OK${PLAIN}"
else
echo -e "${RED}ERROR${PLAIN}"
exit 1
fi
}
wget_setup() {
[[ -e '/usr/bin/wget' ]] && return
echo -e "正在安装Wget...\c"
if [ "${package_type}" == "RPM" ]; 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
if [ -e '/usr/bin/wget' ]; then
echo -e "${GREEN}OK${PLAIN}"
else
echo -e "${RED}ERROR${PLAIN}"
exit 1
fi
}
speedtest_setup() {
[[ -e './st-temp/speedtest' ]] && return
echo -e "正在获取Speedtest-cli...\c"
mkdir -p ./st-temp/speedtest-cli
wget --no-check-certificate -qO ./st-temp/speedtest.tgz https://install.speedtest.net/app/cli/ookla-speedtest-1.0.0-$(uname -m)-linux.tgz > /dev/null 2>&1
tar zxvf ./st-temp/speedtest.tgz -C ./st-temp/speedtest-cli/ > /dev/null 2>&1
mv ./st-temp/speedtest-cli/speedtest ./st-temp/speedtest-cli/speedtest.5 ./st-temp/
chmod a+rx ./st-temp/speedtest
rm -rf ./st-temp/speedtest.tgz ./st-temp/speedtest-cli
if [ -e './st-temp/speedtest' ]; then
echo -e "${GREEN}OK${PLAIN}"
else
echo -e "${RED}ERROR${PLAIN}"
clear_env
exit 1
fi
}
get_server() {
echo -e "正在获取服务器列表...\c"
rm -f ./st-temp/ALL.dat
rm -f ./st-temp/DX.dat
rm -f ./st-temp/LT.dat
rm -f ./st-temp/YD.dat
wget -P ./st-temp/ https://st.343.re/cn/ALL.dat > /dev/null 2>&1
wget -P ./st-temp/ https://st.343.re/cn/DX.dat > /dev/null 2>&1
wget -P ./st-temp/ https://st.343.re/cn/LT.dat > /dev/null 2>&1
wget -P ./st-temp/ https://st.343.re/cn/YD.dat > /dev/null 2>&1
[[ ! -e './st-temp/ALL.dat' ]] && echo -e "${RED}ERROR${PLAIN}" && clear_env && exit 1
[[ ! -e './st-temp/DX.dat' ]] && echo -e "${RED}ERROR${PLAIN}" && clear_env && exit 1
[[ ! -e './st-temp/LT.dat' ]] && echo -e "${RED}ERROR${PLAIN}" && clear_env && exit 1
[[ ! -e './st-temp/YD.dat' ]] && echo -e "${RED}ERROR${PLAIN}" && clear_env && exit 1
echo -e "${GREEN}OK${PLAIN}"
}
load_server() {
local dat
while read temp_line
do
dat=(${temp_line//,/ })
id_arr[${#id_arr[@]}+1]=${dat[0]}
addr_arr[${#addr_arr[@]}+1]=${dat[1]}
isp_arr[${#isp_arr[@]}+1]=${dat[2]}
done <<< "$(cat $1)"
server_num=${#id_arr[@]}
}
select_isp() {
echo -e "${GREEN}1.${PLAIN} 三网测速"
echo -e "${GREEN}2.${PLAIN} 电信节点"
echo -e "${GREEN}3.${PLAIN} 联通节点"
echo -e "${GREEN}4.${PLAIN} 移动节点"
echo -e "${GREEN}5.${PLAIN} 取消测速\c"
while :; do echo
read -p "请选择: " selection
if [[ ! $selection =~ ^[1-5]$ ]]; then
echo -e "${RED}输入无效${PLAIN}\c"
else
break
fi
done
[[ ${selection} == 5 ]] && clear_env && exit 1
[[ ${selection} == 1 ]] && load_server './st-temp/ALL.dat'
[[ ${selection} == 2 ]] && load_server './st-temp/DX.dat'
[[ ${selection} == 3 ]] && load_server './st-temp/LT.dat'
[[ ${selection} == 4 ]] && load_server './st-temp/YD.dat'
}
speedtest() {
speedLog="./st-temp/speedtest.log"
touch $speedLog
./st-temp/speedtest -p no -s $1 --accept-license > $speedLog 2>&1
echo -en "\r"
echo -en " "
echo -en "\r"
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
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
}
runtest() {
echo -en "\033[?25l"
echo "——————————————————————————————————————————————————————————"
curl "ip.343.re/info"
echo "——————————————————————————————————————————————————————————"
echo "ID 测速服务器信息 上传/Mbps 下载/Mbps 延迟/ms"
start=$(date +%s)
for ((i=1;i<=$server_num;i++)) do
echo -e "正在测试 ${YELLOW}${isp_arr[i]}|${PLAIN}${GREEN}${addr_arr[i]}${PLAIN} ...\c"
speedtest ${id_arr[i]} ${addr_arr[i]} ${isp_arr[i]}
done
end=$(date +%s)
echo "——————————————————————————————————————————————————————————"
time=$(( $end - $start ))
if [[ $time -gt 60 ]]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
echo -e "测试完成, 共耗时${CYAN} ${min}${sec}${PLAIN}"
else
echo -e "测试完成, 共耗时${CYAN} ${time}${PLAIN}"
fi
echo -e "\033[?25h"
}
clear_env() {
rm -rf ./st-temp
}
init() {
check_root
check_system
python_setup
curl_setup
wget_setup
speedtest_setup
get_server
}
init
select_isp
clear
runtest
clear_env
Loading…
Cancel
Save