check_ip.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. # LinuxGSM check_ip.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://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. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. info_game.sh
  10. ip_commands_array=("/bin/ip" "/usr/sbin/ip" "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=("/bin/ethtool" "/usr/sbin/ethtool" "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. getip=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
  25. getipwc=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -vc 127.0.0)
  26. # Check if server has multiple IP addresses
  27. # If the IP variable has been set by user.
  28. if [ -n "${ip}" ] && [ "${ip}" != "0.0.0.0" ]; then
  29. queryips=("${ip}")
  30. webadminip=("${ip}")
  31. telnetip=("${ip}")
  32. # If game config does have an IP set.
  33. elif [ -n "${configip}" ] && [ "${configip}" != "0.0.0.0" ]; then
  34. queryips=("${configip}")
  35. ip="${configip}"
  36. webadminip=("${configip}")
  37. telnetip=("${configip}")
  38. # If there is only 1 server IP address.
  39. # Some IP details can automaticly use the one IP
  40. elif [ "${getipwc}" == "1" ]; then
  41. queryips=($(echo "${getip}"))
  42. ip="0.0.0.0"
  43. webadminip=("${getip}")
  44. telnetip=("${getip}")
  45. # If no ip is set by the user and server has more than one IP.
  46. else
  47. queryips=($(echo "${getip}"))
  48. ip="0.0.0.0"
  49. webadminip=("${ip}")
  50. telnetip=("${ip}")
  51. fi