check_ip.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. # LinuxGSM check_ip.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Automatically identifies the server interface IP.
  7. # If multiple interfaces are detected the user will need to manually set using ip="0.0.0.0".
  8. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. info_game.sh
  10. ip_commands_array=("ip" "/bin/ip" "/usr/sbin/ip")
  11. for ip_command in "${ip_commands_array[@]}"; do
  12. if [ "$(command -v "${ip_command}" 2> /dev/null)" ]; then
  13. ipcommand="${ip_command}"
  14. break
  15. fi
  16. done
  17. ethtool_commands_array=("ethtool" "/bin/ethtool" "/usr/sbin/ethtool")
  18. for ethtool_command in "${ethtool_commands_array[@]}"; do
  19. if [ "$(command -v "${ethtool_command}" 2> /dev/null)" ]; then
  20. ethtoolcommand="${ethtool_command}"
  21. break
  22. fi
  23. done
  24. mapfile -t current_ips < <(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
  25. function fn_is_valid_ip() {
  26. local ip="${1}"
  27. # excluding 0.* ips also
  28. grep -qEe '^[1-9]+[0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$' <<< "${ip}"
  29. }
  30. # Check if server has multiple IP addresses.
  31. # If the IP variable has been set by user.
  32. if fn_is_valid_ip "${ip}"; then
  33. queryips=("${ip}" "${publicip}")
  34. httpip=("${ip}")
  35. telnetip=("${ip}")
  36. # If the game config has an IP set.
  37. elif fn_is_valid_ip "${configip}"; then
  38. queryips=("${configip}" "${publicip}")
  39. ip="${configip}"
  40. httpip=("${configip}")
  41. telnetip=("${configip}")
  42. # If there is only 1 server IP address.
  43. # Some IP details can automatically use the one IP.
  44. elif [ "${#current_ips[@]}" == "1" ]; then
  45. queryips=("127.0.0.1" "${current_ips[@]}" "${publicip}")
  46. ip="0.0.0.0"
  47. httpip=("${current_ips[@]}")
  48. telnetip=("${current_ips[@]}")
  49. # If no ip is set by the user and server has more than one IP.
  50. else
  51. queryips=("127.0.0.1" "${current_ips[@]}" "${publicip}")
  52. ip="0.0.0.0"
  53. httpip=("${ip}")
  54. telnetip=("${ip}")
  55. fi