check_ip.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if [ "${travistest}" != "1" ]; then
  9. if [ ! -f "/bin/ip" ]; then
  10. ipcommand="/sbin/ip"
  11. else
  12. ipcommand="ip"
  13. fi
  14. getip=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|sort -u|grep -v 127.0.0)
  15. getipwc=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|sort -u|grep -vc 127.0.0)
  16. info_config.sh
  17. info_parms.sh
  18. # IP is not set to specific IP.
  19. if [ "${ip}" == "0.0.0.0" ]||[ "${ip}" == "" ]; then
  20. fn_print_dots "Check IP"
  21. # Multiple interfaces.
  22. if [ "${getipwc}" -ge "2" ]; then
  23. if [ "${commandname}" == "details" ]; then
  24. fn_print_warn "Check IP: Multiple IP addresses found."
  25. else
  26. fn_print_fail "Check IP: Multiple IP addresses found."
  27. fi
  28. echo -en "\n"
  29. # IP is set within game config.
  30. if [ "${ipsetinconfig}" == "1" ]; then
  31. fn_print_information "Specify the IP you want to bind within ${servercfg}.\n"
  32. echo -en " * location: ${servercfgfullpath}\n"
  33. echo -en "\n"
  34. echo -en "Set ${ipinconfigvar} to one of the following:\n"
  35. fn_script_log_fatal "Multiple IP addresses found."
  36. fn_script_log_fatal "Specify the IP you want to bind within: ${servercfgfullpath}."
  37. # IP is set within LinuxGSM config.
  38. else
  39. fn_print_information_nl "Specify the IP you want to bind within a LinuxGSM config file.\n"
  40. echo -en " * location: ${configdirserver}\n"
  41. echo -en "\n"
  42. echo -en "Set ip=\"0.0.0.0\" to one of the following:\n"
  43. fn_script_log_fatal "Multiple IP addresses found."
  44. fn_script_log_fatal "Specify the IP you want to bind within: ${configdirserver}."
  45. fi
  46. fi
  47. echo -en "${getip}\n"
  48. echo -en "\n"
  49. echo -en "https://linuxgsm.com/network-interfaces\n"
  50. echo -en ""
  51. # Do not exit for details and postdetails commands.
  52. if [ "${commandname}" != "DETAILS" ]||[ "${commandname}" != "POST-DETAILS" ]; then
  53. fn_script_log_fatal "https://linuxgsm.com/network-interfaces\n"
  54. core_exit.sh
  55. else
  56. ip="NOT SET"
  57. fi
  58. # Single interface.
  59. elif [ "${ipsetinconfig}" == "1" ]; then
  60. fn_print_fail "Check IP: IP address not set in game config."
  61. echo -en "\n"
  62. fn_print_information "Specify the IP you want to bind within ${servercfg}.\n"
  63. echo -en " * location: ${servercfgfullpath}\n"
  64. echo -en "\n"
  65. echo -en "Set ${ipinconfigvar} to the following:\n"
  66. echo -en "${getip}\n"
  67. echo -en "\n"
  68. echo -en "https://linuxgsm.com/network-interfaces\n"
  69. echo -en ""
  70. fn_script_log_fatal "IP address not set in game config."
  71. fn_script_log_fatal "Specify the IP you want to bind within: ${servercfgfullpath}."
  72. fn_script_log_fatal "https://linuxgsm.com/network-interfaces\n"
  73. if [ "${commandname}" != "DETAILS" ]; then
  74. core_exit.sh
  75. fi
  76. else
  77. fn_print_info_nl "Check IP: ${getip}"
  78. fn_script_log_info "IP automatically set as: ${getip}"
  79. ip="${getip}"
  80. fi
  81. fi
  82. fi