Advertisement
tml3nr

pingcheck v2 - ping 350

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