forked from R0GGER/public-iperf3-servers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findtest.sh
99 lines (83 loc) · 3.47 KB
/
findtest.sh
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
#!/bin/bash
# Find and Test
# Unstable!
# curl -s https://raw.githubusercontent.com/R0GGER/public-iperf3-servers/refs/heads/main/findtest.sh | bash
install_dependencies() {
if [[ -f /etc/debian_version ]]; then
# Debian/Ubuntu
echo "Detected Debian/Ubuntu. Installing jq and bc with apt."
sudo apt-get update && sudo apt-get install -y jq bc
elif [[ -f /etc/rocky-release ]] || [[ -f /etc/redhat-release ]]; then
# Rocky Linux/Red Hat/CentOS
echo "Detected Rocky Linux or RHEL-based system. Installing jq and bc with dnf."
sudo dnf install -y jq bc
else
echo "Unsupported operating system. Please install jq and bc manually."
exit 1
fi
}
# Check if jq is installed, if not, install it
if ! command -v jq &> /dev/null || ! command -v bc &> /dev/null; then
echo "jq or bc is not installed. Installing jq and bc..."
install_dependencies
if [[ $? -ne 0 ]]; then
echo "Failed to install jq or bc. Exiting."
exit 1
fi
else
echo "jq and bc are already installed."
fi
# Function to calculate the Haversine distance between two sets of coordinates
haversine() {
lat1=$1
lon1=$2
lat2=$3
lon2=$4
# Convert degrees to radians
dlat=$(echo "$lat2 - $lat1" | bc -l 2>/dev/null)
dlon=$(echo "$lon2 - $lon1" | bc -l 2>/dev/null)
lat1=$(echo "$lat1 * 0.017453292519943295" | bc -l 2>/dev/null)
lat2=$(echo "$lat2 * 0.017453292519943295" | bc -l 2>/dev/null)
dlat=$(echo "$dlat * 0.017453292519943295" | bc -l 2>/dev/null)
dlon=$(echo "$dlon * 0.017453292519943295" | bc -l 2>/dev/null)
a=$(echo "s($dlat/2)^2 + c($lat1) * c($lat2) * s($dlon/2)^2" | bc -l 2>/dev/null)
c=$(echo "2 * a(sqrt($a) / sqrt(1-$a))" | bc -l 2>/dev/null)
# Earth's radius in kilometers (6371)
distance=$(echo "6371 * $c" | bc -l 2>/dev/null)
echo $distance
}
# Step 1: Find the location of the client (your own IP)
CLIENT_INFO=$(curl -s ipinfo.io)
CLIENT_LAT=$(echo $CLIENT_INFO | jq -r '.loc' | cut -d',' -f1 2>/dev/null)
CLIENT_LON=$(echo $CLIENT_INFO | jq -r '.loc' | cut -d',' -f2 2>/dev/null)
echo "Client location: Latitude $CLIENT_LAT, Longitude $CLIENT_LON"
# Step 2: Download the list of iperf3 servers
SERVER_LIST_URL="https://iperf3serverlist.net"
SERVER_LIST_FILE="/tmp/iperf3_servers.html"
curl -s $SERVER_LIST_URL -o $SERVER_LIST_FILE
# Step 3: Find the IP addresses of the servers in the list
SERVER_IPS=$(grep -Eo '(([0-9]{1,3}\.){3}[0-9]{1,3})' $SERVER_LIST_FILE)
# Step 4: Find the location of each server
closest_server=""
closest_distance=""
for ip in $SERVER_IPS; do
# Find the geolocation of the server
SERVER_INFO=$(curl -s ipinfo.io/$ip)
SERVER_LAT=$(echo $SERVER_INFO | jq -r '.loc' | cut -d',' -f1 2>/dev/null)
SERVER_LON=$(echo $SERVER_INFO | jq -r '.loc' | cut -d',' -f2 2>/dev/null)
# Calculate the distance between the client and the server
distance=$(haversine $CLIENT_LAT $CLIENT_LON $SERVER_LAT $SERVER_LON)
# echo "Server $ip is $distance km away"
# Compare distances to find the closest server
if [[ -z "$closest_distance" || $(echo "$distance < $closest_distance" | bc -l 2>/dev/null) -eq 1 ]]; then
closest_distance=$distance
closest_server=$ip
fi
done
# Step 5: Run the iperf3 test with the closest server
if [[ -n "$closest_server" ]]; then
echo "The closest server is $closest_server at a distance of $closest_distance km"
iperf3 -c $closest_server
else
echo "No closest server found."
fi