tml3nr

pingcheck v4

Jun 29th, 2023
137
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 'sg494.nordvpn.com'
  3. @@@@@@@@@@@@@@@@@@@@@@
  4. root@OpenWrt:~# ping -I nordlynx google.com
  5. PING google.com (172.217.31.14): 56 data bytes
  6. 64 bytes from 172.217.31.14: seq=0 ttl=114 time=74.798 ms
  7. 64 bytes from 172.217.31.14: seq=1 ttl=114 time=74.787 ms
  8. 64 bytes from 172.217.31.14: seq=2 ttl=114 time=74.573 ms
  9. ^C
  10. --- google.com ping statistics ---
  11. 3 packets transmitted, 3 packets received, 0% packet loss
  12. round-trip min/avg/max = 74.573/74.719/74.798 ms
  13. @@@@@@@@@@@@@@@@@@@@@@
  14. root@OpenWrt:~# ping -I br-lan google.com
  15. PING google.com (172.217.31.14): 56 data bytes
  16. 64 bytes from 172.217.31.14: seq=0 ttl=117 time=27.894 ms
  17. 64 bytes from 172.217.31.14: seq=1 ttl=117 time=27.514 ms
  18. 64 bytes from 172.217.31.14: seq=2 ttl=117 time=27.737 ms
  19. ^C
  20. --- google.com ping statistics ---
  21. 3 packets transmitted, 3 packets received, 0% packet loss
  22. round-trip min/avg/max = 27.514/27.715/27.894 ms
  23. @@@@@@@@@@@@@@@@@@@@@@
  24. root@OpenWrt:~# cat ./pingv4.sh
  25. #!/bin/sh
  26.  
  27. check_ping () {
  28. server="$1"
  29. interface="$2"
  30. count=0
  31. while [ "$count" -lt 5 ]; do
  32. ping_output=$(ping "$server" -c 1 -I "$interface" -W 5)
  33. ping_time=$(printf "%.0f" "$(echo "$ping_output" | grep 'time=' | awk -F 'time=' '{print $2}' | awk '{print $1}')")
  34. if [ -n "$ping_time" ] && [ "$ping_time" -gt 0 ]; then
  35. break
  36. fi
  37. count=$((count+1))
  38. sleep 1
  39. done
  40. }
  41.  
  42. find_fastest_server () {
  43. server_list="/etc/servers"
  44. fastest_ping=100
  45. fastest_server=
  46. interface="br-lan"
  47. while IFS= read -r server; do
  48. echo "Checking server: $server"
  49. check_ping "$server" "$interface" &>/dev/null
  50. if [ "$ping_time" -lt "$fastest_ping" ]; then
  51. fastest_server="$server"
  52. fastest_ping="$ping_time"
  53. fi
  54. done < "$server_list"
  55. }
  56.  
  57. ifname="nordlynx"
  58. echo "Pinging..."
  59. check_ping "8.8.8.8" "$ifname" &>/dev/null
  60. echo "Current ping: $ping_time"
  61.  
  62. # Check if VPN ping is lower than 50ms
  63. if [ "$ping_time" -eq 0 ] || [ "$ping_time" -lt 10 ]; then
  64. exit 0
  65. else
  66. find_fastest_server
  67. echo "Fastest server: $fastest_server"
  68. echo "Fastest ping: $fastest_ping ms"
  69. # If there is no error left, remove this line and all # at the beginning of the 3 following lines
  70. #uci set network.@wireguard_${ifname}[-1].endpoint_host="$fastest_server"
  71. #uci commit network
  72. #/etc/init.d/network restart
  73. fi
  74.  
  75. exit 0
  76. @@@@@@@@@@@@@@@@@@@@@@
  77. root@OpenWrt:~# ./pingv4.sh
  78. Pinging...
  79. Current ping: 39
  80. Checking server: sg491.nordvpn.com
  81. Checking server: sg493.nordvpn.com
  82. Checking server: sg494.nordvpn.com
  83. Checking server: sg455.nordvpn.com
  84. Checking server: sg508.nordvpn.com
  85. Checking server: sg456.nordvpn.com
  86. Checking server: sg507.nordvpn.com
  87. Checking server: sg490.nordvpn.com
  88. Checking server: sg459.nordvpn.com
  89. Checking server: sg460.nordvpn.com
  90. Checking server: sg457.nordvpn.com
  91. Checking server: sg461.nordvpn.com
  92. Checking server: sg462.nordvpn.com
  93. Checking server: sg463.nordvpn.com
  94. Checking server: sg458.nordvpn.com
  95. Fastest server: sg491.nordvpn.com
  96. Fastest ping: 29 ms
  97. @@@@@@@@@@@@@@@@@@@@@@
  98. root@OpenWrt:~# cat /etc/config/network | grep endpoint_host
  99. option endpoint_host 'sg494.nordvpn.com'
  100. root@OpenWrt:~#
Advertisement
Add Comment
Please, Sign In to add comment