tml3nr

pingcheck v2

Jun 28th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. root@OpenWrt:~# cat /etc/servers
  2. sg491.nordvpn.com
  3. sg493.nordvpn.com
  4. sg494.nordvpn.com
  5. sg455.nordvpn.com
  6. sg508.nordvpn.com
  7. sg456.nordvpn.com
  8. sg507.nordvpn.com
  9. sg490.nordvpn.com
  10. sg459.nordvpn.com
  11. sg460.nordvpn.com
  12. sg457.nordvpn.com
  13. sg461.nordvpn.com
  14. sg462.nordvpn.com
  15. sg463.nordvpn.com
  16. sg458.nordvpn.com
  17. root@OpenWrt:~#
  18.  
  19. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  20.  
  21. root@OpenWrt:~# cat ./pingv2.sh
  22. #!/bin/sh
  23.  
  24. check_ping () {
  25. server="$1"
  26. interface="$2"
  27. ping_result=$(ping -c 1 -I "$interface" -W 10 "$server" | grep 'time=' | awk -F 'time=' '{print $2}' | awk '{print $1}')
  28. if [ -z "$ping_result" ]; then
  29. # Error handling on ping failed
  30. echo "Error: ping to $server failed or timed out"
  31. exit 1
  32. fi
  33. }
  34.  
  35. find_fastest_server () {
  36. server_list="/etc/servers"
  37. fastest_server=""
  38. # Threshold for fastest_ping
  39. fastest_ping=999999
  40. # Replace with the name of your WAN interface
  41. interface="br-lan"
  42.  
  43. while read -r server; do
  44. ping_result=$(check_ping "$server" "$interface")
  45. if [ -n "$ping_result" ] && [ "$ping_result" -lt "$fastest_ping" ]; then
  46. fastest_server="$server"
  47. fastest_ping="$ping_result"
  48. fi
  49. done < "$server_list"
  50. }
  51.  
  52. interface="nordlynx"
  53. if [ "$(check_ping "google.com" "$interface")" -lt 50 ]; then
  54. exit 0
  55. else
  56. fastest_server=""
  57. find_fastest_server
  58. uci set network.${interface}.endpoint_host="$fastest_server"
  59. if ! uci commit network; then
  60. # Error handling on commit network
  61. echo "Error: Failed to commit network changes"
  62. exit 1
  63. fi
  64. if ! /etc/init.d/network restart; then
  65. # Error handling on commit network
  66. echo "Error: Failed to restart the network"
  67. exit 1
  68. fi
  69. fi
  70.  
  71. exit 0
  72.  
  73. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  74.  
  75. root@OpenWrt:~# ./pingv2.sh
  76. sh: out of range
  77. uci: Invalid argument
  78. 'radio0' is disabled
  79. root@OpenWrt:~#
  80.  
Advertisement
Add Comment
Please, Sign In to add comment