最近因为业务需要,准备着手写个多ADSL端口的shell测速脚本。在客户端测速完成以后,将测速结果上传至服务端,测速结果同时在测速客户端和WEB服务器上同步显示,测速服务器可以查看测速历史记录。考虑了通过如下方式来实现:
1、speedtest,基于speedtest的官方测速节点进行测速。目前的脚本不支持自选节点,默认使用命令自己查到的最近的节点进行测速。但是我们这边测速的节点在兰州电信。多次测速发现测速并不能跑满
#!/bin/bash
# 检测系统和版本
get_os_version() {
if [[ -e /etc/os-release ]]; then
source /etc/os-release
OS=$NAME
VERSION=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si)
VERSION=$(lsb_release -sr)
elif [[ -e /etc/lsb-release ]]; then
source /etc/lsb-release
OS=$DISTRIB_ID
VERSION=$DISTRIB_RELEASE
elif [[ -e /etc/debian_version ]]; then
OS="Debian"
VERSION=$(cat /etc/debian_version)
elif [[ -e /etc/centos-release ]]; then
OS="CentOS"
VERSION=$(awk '{print $(NF-1)}' /etc/centos-release)
else
OS=$(uname -s)
VERSION=$(uname -r)
fi
echo "$OS $VERSION"
}
# 检测并安装 speedtest-cli
install_speedtest_cli() {
if ! command -v speedtest-cli &>/dev/null; then
echo "Installing speedtest-cli..."
if [[ $OS == "CentOS" ]]; then
sudo curl -o speedtest-cli https://ghproxy.com/https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
sudo mv speedtest-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/speedtest-cli
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]]; then
sudo curl -o speedtest-cli https://ghproxy.com/https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
sudo mv speedtest-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/speedtest-cli
fi
fi
}
# 检测并安装 jq
install_jq() {
if ! command -v jq &>/dev/null; then
echo "Installing jq..."
if [[ $OS == "CentOS" ]]; then
if [[ $VERSION =~ ^7 ]] || [[ $VERSION =~ ^8 ]]; then
sudo yum -y install epel-release
sudo yum -y install jq
fi
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]]; then
sudo apt-get update
sudo apt-get -y install jq
fi
fi
}
# 检查并安装 pystun Python 库
install_pystun() {
if ! command -v pystun &>/dev/null; then
# 安装 pystun 库
python -m pip install --upgrade --force pip==20.2.4 -i https://pypi.douban.com/simple
python -m pip install --upgrade --force pip -i https://pypi.douban.com/simple
pip install pystun
ln -s /usr/bin/pystun /usr/local/bin/pystun
echo "pystun library has been installed."
fi
}
# 获取系统和版本
OS_VERSION=$(get_os_version)
echo "ppp speedtest script By:Fenei"
echo "http://blog.fenei.net"
echo "Operating System and Version: $OS_VERSION"
# 安装 speedtest-cli 和 jq
install_speedtest_cli
install_jq
install_pystun
# 获取当前系统的主机名
hostname=$(hostname)
# 获取当前日期时间
datetime=$(date +"%Y%m%d%H%M%S")
# 设置结果文件名
filename="${hostname}_${datetime}"
# 获取当前系统中的 PPP 端口
ppp_ports=($(ls -d /sys/class/net/ppp* | awk -F/ '{print $NF}'))
# 创建结果目录
mkdir -p testlog
# 清空结果文件
> "testlog/${filename}.csv"
# 输出 CSV 头部
echo "Account,Password,Port,VLAN,IP Addr,Nat_type,ExternalIP,Up Speed(Mbps),Down Speed(Mbps),Test Time" >> "testlog/${filename}.csv"
# 初始化总计变量
total_upload=0
total_download=0
# 遍历每个 PPP 端口
for port in "${ppp_ports[@]}"
do
(
# 获取拨号账号
account=$(grep "USER=" /etc/sysconfig/network-scripts/ifcfg-"$port" | cut -d= -f2)
# 获取拨号密码
password=$(grep "\"$account\"" /etc/ppp/pap-secrets | awk -F '"' '{print $4}')
# 获取拨号网卡的 IP 地址
ip=$(ip addr show dev "$port" | awk '/inet / {print $2}' | cut -d'/' -f1)
# 获取 VLAN 编号
vlan=$(grep "ETH=" /etc/sysconfig/network-scripts/ifcfg-"$port" | awk -F'.' '{if (NF > 1) print $NF; else print 1}')
# 测试 NAT 类型
#nat_type=$(pystun --json -i "$ip" | jq -r '.nat_type')
nat_type=$(pystun -i "$ip" | grep "NAT Type:" | awk -F':' '{print $2}' | awk '{$1=$1};1')
ExternalIP=$(pystun -i "$ip" | grep "External IP:" | awk '{print $NF}')
# 运行测速命令并获取上传和下载速度
result=$(speedtest-cli --source "$ip" --json)
upload=$(echo "$result" | jq -r '.upload')
download=$(echo "$result" | jq -r '.download')
# 获取测速时间(精确到秒)
speedtest_time=$(date +"%Y-%m-%d %H:%M:%S")
# 将速度转换为兆比特每秒(Mbps)
upload=$(awk "BEGIN{printf \"%.2f\", $upload / 1024 / 1024}")
download=$(awk "BEGIN{printf \"%.2f\", $download / 1024 / 1024}")
# 输出每个端口的测速结果
#echo -e "$account\t$password\t$port\t$vlan\t$ip\t$nat_type\t$upload\t$download\t$speedtest_time"
# 输出到结果文件
echo "$account,$password,$port,$vlan,$ip,$nat_type,$ExternalIP,$upload,$download,\"$speedtest_time\"" >> "testlog/${filename}.csv"
) &
done
# 等待所有线程完成
wait
# 统计上传和下载速度的总和
total_upload=$(awk -F',' '{sum += $8} END {printf "%.2f", sum}' "testlog/${filename}.csv")
total_download=$(awk -F',' '{sum += $9} END {printf "%.2f", sum}' "testlog/${filename}.csv")
# 输出总计行
#echo -e "Total\t\t\t\t\t$total_upload\t$total_download"
# 将总计数据追加到结果文件
echo "Total,,,,,,,$total_upload,$total_download" >> "testlog/${filename}.csv"
# 对齐输出结果
column -s, -t "testlog/${filename}.csv"
# 输出测速结果文件路径
echo "Speed test results saved in the server file: http://spt.fenei.net/2.html?file=${filename}.csv"
# 上传结果文件到服务器
curl -F "file=@testlog/${filename}.csv" http://spt.fenei.net/upload.phpspeedtest-go的代码
#!/bin/bash
# 检测系统和版本
get_os_version() {
if [[ -e /etc/os-release ]]; then
source /etc/os-release
OS=$NAME
VERSION=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si)
VERSION=$(lsb_release -sr)
elif [[ -e /etc/lsb-release ]]; then
source /etc/lsb-release
OS=$DISTRIB_ID
VERSION=$DISTRIB_RELEASE
elif [[ -e /etc/debian_version ]]; then
OS="Debian"
VERSION=$(cat /etc/debian_version)
elif [[ -e /etc/centos-release ]]; then
OS="CentOS"
VERSION=$(awk '{print $(NF-1)}' /etc/centos-release)
else
OS=$(uname -s)
VERSION=$(uname -r)
fi
echo "$OS $VERSION"
}
# 检测并安装 speedtest-cli
install_speedtest_cli() {
if ! command -v speedtest-cli &>/dev/null; then
echo "Installing speedtest-cli..."
if [[ $OS == "CentOS" ]]; then
sudo curl -o speedtest-cli https://ghproxy.com/https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
sudo mv speedtest-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/speedtest-cli
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]]; then
sudo curl -o speedtest-cli https://ghproxy.com/https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
sudo mv speedtest-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/speedtest-cli
fi
fi
}
# 检测并安装 speedtest-cli
install_speedtest_go() {
if ! command -v speedtest-go &>/dev/null; then
echo "Installing speedtest-go..."
curl -o speedtest-go.tar.gz https://ghproxy.com/https://github.com/showwin/speedtest-go/releases/download/v1.6.3/speedtest-go_1.6.3_Linux_x86_64.tar.gz
tar xf speedtest-go.tar.gz
mv speedtest-go /usr/local/bin/
chmod +x /usr/local/bin/speedtest-go
rm speedtest-go.tar.gz
echo "speedtest-go 安装完成!"
else
echo "speedtest-go 已经安装过了。"
fi
}
# 检测并安装 jq
install_jq() {
if ! command -v jq &>/dev/null; then
echo "Installing jq..."
if [[ $OS == "CentOS" ]]; then
if [[ $VERSION =~ ^7 ]] || [[ $VERSION =~ ^8 ]]; then
sudo yum -y install epel-release
sudo yum -y install jq
fi
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]]; then
sudo apt-get update
sudo apt-get -y install jq
fi
fi
}
# 检查并安装 pystun Python 库
install_pystun() {
if ! command -v pystun &>/dev/null; then
# 安装 pystun 库
python -m pip install --upgrade --force pip==20.2.4 -i https://pypi.douban.com/simple
python -m pip install --upgrade --force pip -i https://pypi.douban.com/simple
pip install pystun
ln -s /usr/bin/pystun /usr/local/bin/pystun
echo "pystun library has been installed."
fi
}
# 获取系统和版本
OS_VERSION=$(get_os_version)
echo "ppp speedtest script By:Fenei"
echo "http://blog.fenei.net"
echo "Operating System and Version: $OS_VERSION"
# 安装 speedtest-cli 和 jq
install_speedtest_cli
install_speedtest_go
install_jq
install_pystun
# 获取当前系统的主机名
hostname=$(hostname)
# 获取当前日期时间
datetime=$(date +"%Y%m%d%H%M%S")
# 设置结果文件名
filename="${hostname}_${datetime}"
# 获取当前系统中的 PPP 端口
ppp_ports=($(ls -d /sys/class/net/ppp* | awk -F/ '{print $NF}'))
# 创建结果目录
mkdir -p testlog
# 清空结果文件
> "testlog/${filename}.csv"
# 输出 CSV 头部
echo "Account,Password,Port,VLAN,IP Addr,Nat_type,ExternalIP,Up Speed(Mbps),Down Speed(Mbps),Test Time" >> "testlog/${filename}.csv"
# 初始化总计变量
total_upload=0
total_download=0
# 遍历每个 PPP 端口
for port in "${ppp_ports[@]}"
do
(
# 获取拨号账号
account=$(grep "USER=" /etc/sysconfig/network-scripts/ifcfg-"$port" | cut -d= -f2)
# 获取拨号密码
password=$(grep "\"$account\"" /etc/ppp/pap-secrets | awk -F '"' '{print $4}')
# 获取拨号网卡的 IP 地址
ip=$(ip addr show dev "$port" | awk '/inet / {print $2}' | cut -d'/' -f1)
# 获取 VLAN 编号
vlan=$(grep "ETH=" /etc/sysconfig/network-scripts/ifcfg-"$port" | awk -F'.' '{if (NF > 1) print $NF; else print 1}')
# 测试 NAT 类型
#nat_type=$(pystun --json -i "$ip" | jq -r '.nat_type')
nat_type=$(pystun -H stun.miwifi.com -i "$ip" | grep "NAT Type:" | awk -F':' '{print $2}' | awk '{$1=$1};1')
ExternalIP=$(pystun -H stun.miwifi.com -i "$ip" | grep "External IP:" | awk '{print $NF}')
# 运行测速命令并获取上传和下载速度
echo "speedtest-go --custom-url=http://113.143.100.145:8082/backend/upload.php --source=$port --json"
result=$(speedtest-go --custom-url=http://113.143.100.145:8082/backend/upload.php --source=$port --json)
upload=$(echo "$result" | jq -r '.servers[0].ul_speed')
download=$(echo "$result" | jq -r '.servers[0].dl_speed')
# 获取测速时间(精确到秒)
speedtest_time=$(date +"%Y-%m-%d %H:%M:%S")
# 将速度转换为兆比特每秒(Mbps)
#upload=$(awk "BEGIN{printf \"%.2f\", $upload / 1024 / 1024}")
#download=$(awk "BEGIN{printf \"%.2f\", $download / 1024 / 1024}")
# 输出每个端口的测速结果
#echo -e "$account\t$password\t$port\t$vlan\t$ip\t$nat_type\t$upload\t$download\t$speedtest_time"
# 输出到结果文件
echo "$account,$password,$port,$vlan,$ip,$nat_type,$ExternalIP,$upload,$download,\"$speedtest_time\"" >> "testlog/${filename}.csv"
) &
done
# 等待所有线程完成
wait
# 统计上传和下载速度的总和
total_upload=$(awk -F',' '{sum += $8} END {printf "%.2f", sum}' "testlog/${filename}.csv")
total_download=$(awk -F',' '{sum += $9} END {printf "%.2f", sum}' "testlog/${filename}.csv")
# 输出总计行
#echo -e "Total\t\t\t\t\t$total_upload\t$total_download"
# 将总计数据追加到结果文件
echo "Total,,,,,,,$total_upload,$total_download" >> "testlog/${filename}.csv"
# 对齐输出结果
column -s, -t "testlog/${filename}.csv"
# 输出测速结果文件路径
echo "Speed test results saved in the server file: http://spt.fenei.net/2.html?file=${filename}.csv"
# 上传结果文件到服务器
curl -F "file=@testlog/${filename}.csv" http://spt.fenei.net/upload.phpiperf3的代码,服务端docker命令为docker container run --rm --init --net=host --name iperf -e COUNT=2000 cseelye/iperf-multi
#!/bin/bash
# 检查iperf3是否已安装
check_iperf3() {
if ! command -v iperf3 &>/dev/null; then
echo "iperf3 is not installed. Please install iperf3 before running this script."
exit 1
fi
}
# 检查jq是否已安装
check_jq() {
if ! command -v jq &>/dev/null; then
echo "jq is not installed. Please install jq before running this script."
exit 1
fi
}
# 检查pystun是否已安装
check_pystun() {
if ! command -v pystun &>/dev/null; then
echo "pystun is not installed. Please install pystun before running this script."
exit 1
fi
}
# 测试iperf服务器可用性
test_iperf_servers() {
local servers=("iperf1.fenei.net" "iperf2.fenei.net")
local available_servers=()
for server in "${servers[@]}"; do
if iperf3 -c "$server" -p 5201 -P 1 -t 1 -J &>/dev/null; then
available_servers+=("$server")
fi
done
echo "${available_servers[@]}"
}
# 获取NAT类型和外部IP
get_nat_type_and_external_ip() {
local ip=$1
local nat_type
local external_ip
nat_type=$(pystun -i "$ip" | grep "NAT Type:" | awk -F':' '{print $2}' | awk '{$1=$1};1')
external_ip=$(pystun -i "$ip" | grep "External IP:" | awk '{print $NF}')
echo "$nat_type,$external_ip"
}
# 测速函数
run_speed_test() {
local account=$1
local password=$2
local port=$3
local vlan=$4
local ip_addr=$5
local server=$6
local server_port=$7
echo "Running speed test for $account on $port"
# 获取NAT类型和外部IP
local nat_type_and_external_ip=$(get_nat_type_and_external_ip "$ip_addr")
local nat_type=${nat_type_and_external_ip%,*}
local external_ip=${nat_type_and_external_ip#*,}
# 运行iperf3测速
local iperf_result_up=$(iperf3 -c "$server" -p $((5200+(RANDOM%2000))) -B $ip_addr -t 1 -P 5 -f m -J)
local iperf_result_down=$(iperf3 -c "$server" -p $((5200+(RANDOM%2000))) -B $ip_addr -t 1 -P 5 -R -f m -J)
local up_speed=$(echo "$iperf_result_up" | jq -r '.end.sum_sent.bits_per_second' | awk '{printf "%.2f", $1 / 1000000}')
local down_speed=$(echo "$iperf_result_down" | jq -r '.end.sum_received.bits_per_second' | awk '{printf "%.2f", $1 / 1000000}')
echo "iperf3 -c "$server" -p $((5200+(RANDOM%30))) -B $ip_addr -f m -J"
# 将测速结果追加到结果文件
echo "$account,$password,$port,$vlan,$ip_addr,$nat_type,$external_ip,$up_speed,$down_speed,$datetime" >> "testlog/${filename}.csv"
echo "Speed test for $account on $port completed"
}
# 获取当前系统中的PPP端口
get_ppp_ports() {
local ppp_ports=($(ls -d /sys/class/net/ppp* | awk -F/ '{print $NF}'))
echo "${ppp_ports[@]}"
}
# 主函数
main() {
# 检查依赖
check_iperf3
check_jq
check_pystun
# 创建结果文件夹
mkdir -p testlog
# 清空结果文件
> "testlog/${filename}.csv"
# 输出 CSV 头部
echo "Account,Password,Port,VLAN,IP Addr,Nat_type,ExternalIP,Up Speed(Mbps),Down Speed(Mbps),Test Time" >> "testlog/${filename}.csv"
# 获取当前系统的主机名
local hostname=$(hostname)
# 获取当前日期时间
local datetime=$(date +"%Y%m%d%H%M%S")
# 设置结果文件名
local filename="${hostname}_${datetime}"
# 获取可用的iperf服务器
local available_servers=($(test_iperf_servers))
if [ ${#available_servers[@]} -eq 0 ]; then
echo "No available iperf servers found."
exit 1
fi
# 获取PPP端口列表
local ppp_ports=($(get_ppp_ports))
if [ ${#ppp_ports[@]} -eq 0 ]; then
echo "No PPP ports found."
exit 1
fi
local server_index=0
# 遍历PPP端口,运行测速
for port in "${ppp_ports[@]}"; do
# 获取拨号账号
local account=$(grep "USER=" /etc/sysconfig/network-scripts/ifcfg-"$port" | cut -d= -f2)
# 获取拨号密码
local password=$(grep "\"$account\"" /etc/ppp/pap-secrets | awk -F '"' '{print $4}')
# 获取拨号网卡的IP地址
local ip=$(ip addr show dev "$port" | awk '/inet / {print $2}' | cut -d'/' -f1)
# 获取VLAN编号
local vlan=$(grep "ETH=" /etc/sysconfig/network-scripts/ifcfg-"$port" | awk -F'.' '{if (NF > 1) print $NF; else print 1}')
# 获取当前服务器和端口
local server=${available_servers[$server_index]}
local server_port=$((5201 + server_index))
# 多线程运行测速
run_speed_test "$account" "$password" "$port" "$vlan" "$ip" "$server" "$server_port" &
# 更新服务器索引
server_index=$(( (server_index + 1) % ${#available_servers[@]} ))
echo $server_index
done
# 等待所有测速任务完成
wait
# 统计上传和下载速度的总和
total_upload=$(awk -F',' '{sum += $8} END {printf "%.2f", sum}' "testlog/${filename}.csv")
total_download=$(awk -F',' '{sum += $9} END {printf "%.2f", sum}' "testlog/${filename}.csv")
# 输出总计行
#echo -e "Total\t\t\t\t\t$total_upload\t$total_download"
# 将总计数据追加到结果文件
echo "Total,,,,,,,$total_upload,$total_download" >> "testlog/${filename}.csv"
# 对齐输出结果
column -s, -t "testlog/${filename}.csv"
# 输出测速结果文件路径
echo "Speed test results saved in the server file: http://spt.fenei.net/2.html?file=${filename}.csv"
# 上传结果文件到服务器
curl -F "file=@testlog/${filename}.csv" http://spt.fenei.net/upload.php
}
# 运行主函数
main基于librespeed-cli,此测速工具支持自建测速节点。
#!/bin/bash
# 检测系统和版本
get_os_version() {
if [[ -e /etc/os-release ]]; then
source /etc/os-release
OS=$NAME
VERSION=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si)
VERSION=$(lsb_release -sr)
elif [[ -e /etc/lsb-release ]]; then
source /etc/lsb-release
OS=$DISTRIB_ID
VERSION=$DISTRIB_RELEASE
elif [[ -e /etc/debian_version ]]; then
OS="Debian"
VERSION=$(cat /etc/debian_version)
elif [[ -e /etc/centos-release ]]; then
OS="CentOS"
VERSION=$(awk '{print $(NF-1)}' /etc/centos-release)
else
OS=$(uname -s)
VERSION=$(uname -r)
fi
echo "$OS $VERSION"
}
# 检测并安装 speedtest-cli
install_speedtest_cli() {
if ! command -v speedtest-cli &>/dev/null; then
echo "Installing speedtest-cli..."
if [[ $OS == "CentOS" ]]; then
sudo curl -o speedtest-cli https://ghproxy.com/https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
sudo mv speedtest-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/speedtest-cli
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]]; then
sudo curl -o speedtest-cli https://ghproxy.com/https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
sudo mv speedtest-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/speedtest-cli
fi
fi
}
# 检测并安装 librespeed-cli
install_librespeed-cli() {
if ! command -v librespeed-cli &>/dev/null; then
echo "Installing librespeed-cli..."
sudo curl -o librespeed-cli.tar.gz https://ghproxy.com/https://github.com/librespeed/speedtest-cli/releases/download/v1.0.10/librespeed-cli_1.0.10_linux_amd64.tar.gz
sudo tar xf librespeed-cli.tar.gz
sudo mv librespeed-cli /usr/local/bin/
sudo chmod +x /usr/local/bin/librespeed-cli
fi
}
# 检测并安装 jq
install_jq() {
if ! command -v jq &>/dev/null; then
echo "Installing jq..."
if [[ $OS == "CentOS" ]]; then
if [[ $VERSION =~ ^7 ]] || [[ $VERSION =~ ^8 ]]; then
sudo yum -y install epel-release
sudo yum -y install jq
fi
elif [[ $OS == "Ubuntu" ]] || [[ $OS == "Debian" ]]; then
sudo apt-get update
sudo apt-get -y install jq
fi
fi
}
# 检查并安装 pystun Python 库
install_pystun() {
if ! command -v pystun &>/dev/null; then
# 安装 pystun 库
python -m pip install --upgrade --force pip==20.2.4 -i https://pypi.douban.com/simple
python -m pip install --upgrade --force pip -i https://pypi.douban.com/simple
pip install pystun
ln -s /usr/bin/pystun /usr/local/bin/pystun
echo "pystun library has been installed."
fi
}
# 获取系统和版本
OS_VERSION=$(get_os_version)
echo "ppp speedtest script By:Fenei"
echo "http://blog.fenei.net"
echo "Operating System and Version: $OS_VERSION"
# 安装 speedtest-cli 和 jq
install_speedtest_cli
install_librespeed-cli
install_jq
install_pystun
# 获取当前系统的主机名
hostname=$(hostname)
# 获取当前日期时间
datetime=$(date +"%Y%m%d%H%M%S")
# 设置结果文件名
filename="${hostname}_${datetime}"
# 获取当前系统中的 PPP 端口
ppp_ports=($(ls -d /sys/class/net/ppp* | awk -F/ '{print $NF}'))
# 创建结果目录
mkdir -p testlog
# 清空结果文件
> "testlog/${filename}.csv"
# 输出 CSV 头部
echo "Account,Password,Port,VLAN,IP Addr,Nat_type,ExternalIP,Up Speed(Mbps),Down Speed(Mbps),Test Time" >> "testlog/${filename}.csv"
# 初始化总计变量
total_upload=0
total_download=0
# 遍历每个 PPP 端口
for port in "${ppp_ports[@]}"
do
(
# 获取拨号账号
account=$(grep "USER=" /etc/sysconfig/network-scripts/ifcfg-"$port" | cut -d= -f2)
# 获取拨号密码
password=$(grep "\"$account\"" /etc/ppp/pap-secrets | awk -F '"' '{print $4}')
# 获取拨号网卡的 IP 地址
ip=$(ip addr show dev "$port" | awk '/inet / {print $2}' | cut -d'/' -f1)
# 获取 VLAN 编号
vlan=$(grep "ETH=" /etc/sysconfig/network-scripts/ifcfg-"$port" | awk -F'.' '{if (NF > 1) print $NF; else print 1}')
# 测试 NAT 类型
#nat_type=$(pystun --json -i "$ip" | jq -r '.nat_type')
#nat_type=$(pystun -i "$ip" | grep "NAT Type:" | awk -F':' '{print $2}' | awk '{$1=$1};1')
#ExternalIP=$(pystun -i "$ip" | grep "External IP:" | awk '{print $NF}')
# 运行测速命令并获取上传和下载速度
result=$(librespeed-cli -4 --source "$ip" --local-json /root/1.json --json)
upload=$(echo $result | jq -r '.[0].upload')
download=$(echo $result | jq -r '.[0].download')
# 获取测速时间(精确到秒)
speedtest_time=$(date +"%Y-%m-%d %H:%M:%S")
# 将速度转换为兆比特每秒(Mbps)
#upload=$(awk "BEGIN{printf \"%.2f\", $upload / 1024 / 1024}")
#download=$(awk "BEGIN{printf \"%.2f\", $download / 1024 / 1024}")
# 输出每个端口的测速结果
#echo -e "$account\t$password\t$port\t$vlan\t$ip\t$nat_type\t$upload\t$download\t$speedtest_time"
# 输出到结果文件
echo "$account,$password,$port,$vlan,$ip,$nat_type,$ExternalIP,$upload,$download,\"$speedtest_time\"" >> "testlog/${filename}.csv"
) &
done
# 等待所有线程完成
wait
# 统计上传和下载速度的总和
total_upload=$(awk -F',' '{sum += $8} END {printf "%.2f", sum}' "testlog/${filename}.csv")
total_download=$(awk -F',' '{sum += $9} END {printf "%.2f", sum}' "testlog/${filename}.csv")
# 输出总计行
#echo -e "Total\t\t\t\t\t$total_upload\t$total_download"
# 将总计数据追加到结果文件
echo "Total,,,,,,,$total_upload,$total_download" >> "testlog/${filename}.csv"
# 对齐输出结果
column -s, -t "testlog/${filename}.csv"
# 输出测速结果文件路径
echo "Speed test results saved in the server file: http://spt.fenei.net/2.html?file=${filename}.csv"
# 上传结果文件到服务器
curl -F "file=@testlog/${filename}.csv" http://spt.fenei.net/upload.php
对应的librespeed服务端docker脚本--- version: "3" services: librespeed_8080: image: adolfintel/speedtest container_name: librespeed_8080 ports: - 8080:80 environment: - MODE=frontend - TELEMETRY=true - PASSWORD=admin@123 - ENABLE_ID_OBFUSCATION=true volumes: - /data/lrs/frontend/servers.json:/servers.json #- /data/lrs/frontend/servers.json:/var/www/html/servers.json restart: unless-stopped librespeed_8081: image: adolfintel/speedtest container_name: librespeed_8081 ports: - 8081:80 environment: - MODE=backend - TELEMETRY=true - PASSWORD=admin@123 restart: unless-stopped librespeed_8082: image: adolfintel/speedtest container_name: librespeed_8082 ports: - 8082:80 environment: - MODE=backend - TELEMETRY=true - PASSWORD=admin@123 restart: unless-stopped librespeed_8083: image: adolfintel/speedtest container_name: librespeed_8083 ports: - 8083:80 environment: - MODE=backend - TELEMETRY=true - PASSWORD=admin@123 restart: unless-stopped librespeed_8084: image: adolfintel/speedtest container_name: librespeed_8084 ports: - 8084:80 environment: - MODE=backend - TELEMETRY=true - PASSWORD=admin@123 restart: unless-stopped librespeed_8085: image: adolfintel/speedtest container_name: librespeed_8085 ports: - 8085:80 environment: - MODE=backend - TELEMETRY=true - PASSWORD=admin@123 restart: unless-stopped librespeed_8086: image: badapple9/speedtest-x container_name: librespeed_8086 ports: - 8086:80 environment: - MAX_LOG_COUNT=100 - IP_SERVICE=ip.sb - SAME_IP_MULTI_LOGS=true restart: unless-stopped
WEB服务端展现如下,可对主机名测速时间段进行筛选。并查看历史测速详情。



本文链接:https://kinber.cn/post/4348.html 转载需授权!
推荐本站淘宝优惠价购买喜欢的宝贝:

支付宝微信扫一扫,打赏作者吧~
