check_ip.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. # LinuxGSM check_ip.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Automatically identifies the server interface IP.
  6. # If multiple interfaces are detected the user will need to manually set using ip="0.0.0.0".
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. info_config.sh
  9. info_parms.sh
  10. if [ ! -f "/bin/ip" ]; then
  11. ipcommand="/sbin/ip"
  12. else
  13. ipcommand="ip"
  14. fi
  15. getip=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
  16. getipwc=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -vc 127.0.0)
  17. # Check if server has multiple IP addresses
  18. # If the IP variable has been set by user.
  19. if [ -n "${ip}" ]&&[ "${ip}" != "0.0.0.0" ]; then
  20. queryips=( "${ip}" )
  21. webadminip=( "${ip}" )
  22. telnetip=( "${ip}" )
  23. # If game config does have an IP set.
  24. elif [ -n "${configip}" ]&&[ "${configip}" != "0.0.0.0" ];then
  25. queryips=( "${configip}" )
  26. ip="${configip}"
  27. webadminip=( "${configip}" )
  28. telnetip=( "${configip}" )
  29. # If there is only 1 server IP address.
  30. # Some IP details can automaticly use the one IP
  31. elif [ "${getipwc}" == "1" ]; then
  32. queryips=( $(echo "${getip}") )
  33. ip="0.0.0.0"
  34. webadminip=( "${getip}" )
  35. telnetip=( "${getip}" )
  36. # If no ip is set by the user and server has more than one IP.
  37. else
  38. queryips=( $(echo "${getip}") )
  39. ip="0.0.0.0"
  40. webadminip=( "${ip}" )
  41. telnetip=( "${ip}" )
  42. fi