Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- root@OpenWrt:~# cat /etc/config/network | grep endpoint_host
- option endpoint_host 'sg480.nordvpn.com'
- ###################
- root@OpenWrt:~# ping -I nordlynx 8.8.8.8
- PING 8.8.8.8 (8.8.8.8): 56 data bytes
- 64 bytes from 8.8.8.8: seq=0 ttl=120 time=62.774 ms
- 64 bytes from 8.8.8.8: seq=1 ttl=120 time=62.782 ms
- 64 bytes from 8.8.8.8: seq=2 ttl=120 time=62.639 ms
- ^C
- --- 8.8.8.8 ping statistics ---
- 3 packets transmitted, 3 packets received, 0% packet loss
- round-trip min/avg/max = 62.639/62.731/62.782 ms
- ###################
- root@OpenWrt:~# cat ./pingv5.sh
- #!/bin/sh
- check_ping () {
- server="$1"
- interface="$2"
- count=0
- while [ "$count" -lt 5 ]; do
- ping_output=$(ping "$server" -c 1 -I "$interface" -W 5)
- ping_time=$(printf "%.0f" "$(echo "$ping_output" | grep 'time=' | awk -F 'time=' '{print $2}' | awk '{print $1}')")
- if [ -n "$ping_time" ] && [ "$ping_time" -gt 0 ]; then
- break
- fi
- count=$((count+1))
- sleep 1
- done
- }
- find_fastest_server () {
- server_list="/etc/servers"
- fastest_ping=100
- fastest_server=
- interface="br-lan"
- while IFS= read -r server; do
- echo "Checking server: $server"
- check_ping "$server" "$interface" &>/dev/null
- if [ "$ping_time" -lt "$fastest_ping" ]; then
- fastest_server="$server"
- fastest_ping="$ping_time"
- fi
- done < "$server_list"
- }
- ifname="nordlynx"
- echo "Pinging..."
- check_ping "8.8.8.8" "$ifname" &>/dev/null
- echo "Current ping: $ping_time ms"
- # Check if VPN ping is lower than 50ms
- if [ ! "$ping_time" -eq 0 ] && [ "$ping_time" -lt 50 ]; then
- exit 0
- else
- find_fastest_server
- echo "Fastest server: $fastest_server"
- echo "Fastest ping: $fastest_ping ms"
- if [ -n "$fastest_server" ] && [ ! "$fastest_ping" -eq 0 ]; then
- uci set network.@wireguard_${ifname}[-1].endpoint_host="$fastest_server"
- uci commit network
- /etc/init.d/network restart
- else
- exit 0
- fi
- fi
- exit 0
- ###################
- root@OpenWrt:~# ./pingv5.sh
- Pinging...
- Current ping: 63 ms
- Checking server: sg455.nordvpn.com
- Checking server: sg456.nordvpn.com
- Checking server: sg457.nordvpn.com
- Checking server: sg458.nordvpn.com
- Checking server: sg459.nordvpn.com
- Checking server: sg460.nordvpn.com
- Checking server: sg461.nordvpn.com
- Checking server: sg462.nordvpn.com
- Checking server: sg463.nordvpn.com
- Checking server: sg490.nordvpn.com
- Checking server: sg491.nordvpn.com
- Checking server: sg493.nordvpn.com
- Checking server: sg494.nordvpn.com
- Checking server: sg507.nordvpn.com
- Checking server: sg508.nordvpn.com
- Fastest server: sg457.nordvpn.com
- Fastest ping: 29 ms
- 'radio0' is disabled
- ###################
- root@OpenWrt:~# cat /etc/config/network | grep endpoint_host
- option endpoint_host 'sg457.nordvpn.com'
- ###################
- root@OpenWrt:~# ping -I nordlynx 8.8.8.8
- PING 8.8.8.8 (8.8.8.8): 56 data bytes
- 64 bytes from 8.8.8.8: seq=0 ttl=117 time=35.840 ms
- 64 bytes from 8.8.8.8: seq=1 ttl=117 time=35.564 ms
- 64 bytes from 8.8.8.8: seq=2 ttl=117 time=35.586 ms
- 64 bytes from 8.8.8.8: seq=3 ttl=117 time=35.443 ms
- ^C
- --- 8.8.8.8 ping statistics ---
- 4 packets transmitted, 4 packets received, 0% packet loss
- round-trip min/avg/max = 35.443/35.608/35.840 ms
- root@OpenWrt:~#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement