Advertisement
tml3nr

pingcheck v5 ok

Jun 30th, 2023
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. root@OpenWrt:~# cat /etc/config/network | grep endpoint_host
  2. option endpoint_host 'sg480.nordvpn.com'
  3. ###################
  4. root@OpenWrt:~# ping -I nordlynx 8.8.8.8
  5. PING 8.8.8.8 (8.8.8.8): 56 data bytes
  6. 64 bytes from 8.8.8.8: seq=0 ttl=120 time=62.774 ms
  7. 64 bytes from 8.8.8.8: seq=1 ttl=120 time=62.782 ms
  8. 64 bytes from 8.8.8.8: seq=2 ttl=120 time=62.639 ms
  9. ^C
  10. --- 8.8.8.8 ping statistics ---
  11. 3 packets transmitted, 3 packets received, 0% packet loss
  12. round-trip min/avg/max = 62.639/62.731/62.782 ms
  13. ###################
  14. root@OpenWrt:~# cat ./pingv5.sh
  15. #!/bin/sh
  16.  
  17. check_ping () {
  18. server="$1"
  19. interface="$2"
  20. count=0
  21. while [ "$count" -lt 5 ]; do
  22. ping_output=$(ping "$server" -c 1 -I "$interface" -W 5)
  23. ping_time=$(printf "%.0f" "$(echo "$ping_output" | grep 'time=' | awk -F 'time=' '{print $2}' | awk '{print $1}')")
  24. if [ -n "$ping_time" ] && [ "$ping_time" -gt 0 ]; then
  25. break
  26. fi
  27. count=$((count+1))
  28. sleep 1
  29. done
  30. }
  31.  
  32. find_fastest_server () {
  33. server_list="/etc/servers"
  34. fastest_ping=100
  35. fastest_server=
  36. interface="br-lan"
  37. while IFS= read -r server; do
  38. echo "Checking server: $server"
  39. check_ping "$server" "$interface" &>/dev/null
  40. if [ "$ping_time" -lt "$fastest_ping" ]; then
  41. fastest_server="$server"
  42. fastest_ping="$ping_time"
  43. fi
  44. done < "$server_list"
  45. }
  46.  
  47. ifname="nordlynx"
  48. echo "Pinging..."
  49. check_ping "8.8.8.8" "$ifname" &>/dev/null
  50. echo "Current ping: $ping_time ms"
  51.  
  52. # Check if VPN ping is lower than 50ms
  53. if [ ! "$ping_time" -eq 0 ] && [ "$ping_time" -lt 50 ]; then
  54. exit 0
  55. else
  56. find_fastest_server
  57. echo "Fastest server: $fastest_server"
  58. echo "Fastest ping: $fastest_ping ms"
  59. if [ -n "$fastest_server" ] && [ ! "$fastest_ping" -eq 0 ]; then
  60. uci set network.@wireguard_${ifname}[-1].endpoint_host="$fastest_server"
  61. uci commit network
  62. /etc/init.d/network restart
  63. else
  64. exit 0
  65. fi
  66. fi
  67. exit 0
  68. ###################
  69. root@OpenWrt:~# ./pingv5.sh
  70. Pinging...
  71. Current ping: 63 ms
  72. Checking server: sg455.nordvpn.com
  73. Checking server: sg456.nordvpn.com
  74. Checking server: sg457.nordvpn.com
  75. Checking server: sg458.nordvpn.com
  76. Checking server: sg459.nordvpn.com
  77. Checking server: sg460.nordvpn.com
  78. Checking server: sg461.nordvpn.com
  79. Checking server: sg462.nordvpn.com
  80. Checking server: sg463.nordvpn.com
  81. Checking server: sg490.nordvpn.com
  82. Checking server: sg491.nordvpn.com
  83. Checking server: sg493.nordvpn.com
  84. Checking server: sg494.nordvpn.com
  85. Checking server: sg507.nordvpn.com
  86. Checking server: sg508.nordvpn.com
  87. Fastest server: sg457.nordvpn.com
  88. Fastest ping: 29 ms
  89. 'radio0' is disabled
  90. ###################
  91. root@OpenWrt:~# cat /etc/config/network | grep endpoint_host
  92. option endpoint_host 'sg457.nordvpn.com'
  93. ###################
  94. root@OpenWrt:~# ping -I nordlynx 8.8.8.8
  95. PING 8.8.8.8 (8.8.8.8): 56 data bytes
  96. 64 bytes from 8.8.8.8: seq=0 ttl=117 time=35.840 ms
  97. 64 bytes from 8.8.8.8: seq=1 ttl=117 time=35.564 ms
  98. 64 bytes from 8.8.8.8: seq=2 ttl=117 time=35.586 ms
  99. 64 bytes from 8.8.8.8: seq=3 ttl=117 time=35.443 ms
  100. ^C
  101. --- 8.8.8.8 ping statistics ---
  102. 4 packets transmitted, 4 packets received, 0% packet loss
  103. round-trip min/avg/max = 35.443/35.608/35.840 ms
  104. root@OpenWrt:~#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement