check_ip.sh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if [ "${legacymode}" == "1" ]; then
  45. fn_script_log_fatal "Specify the IP you want to bind within the ${selfname} script."
  46. else
  47. fn_script_log_fatal "Specify the IP you want to bind within: ${configdirserver}."
  48. fi
  49. fi
  50. echo -en "${getip}\n"
  51. echo -en "\n"
  52. echo -en "https://linuxgsm.com/network-interfaces\n"
  53. echo -en ""
  54. # Do not exit for details and postdetails commands.
  55. if [ "${commandname}" != "DETAILS" ]||[ "${commandname}" != "POST-DETAILS" ]; then
  56. fn_script_log_fatal "https://linuxgsm.com/network-interfaces\n"
  57. core_exit.sh
  58. else
  59. ip="NOT SET"
  60. fi
  61. # Single interface.
  62. elif [ "${ipsetinconfig}" == "1" ]; then
  63. fn_print_fail "Check IP: IP address not set in game config."
  64. echo -en "\n"
  65. fn_print_information "Specify the IP you want to bind within ${servercfg}.\n"
  66. echo -en " * location: ${servercfgfullpath}\n"
  67. echo -en "\n"
  68. echo -en "Set ${ipinconfigvar} to the following:\n"
  69. echo -en "${getip}\n"
  70. echo -en "\n"
  71. echo -en "https://linuxgsm.com/network-interfaces\n"
  72. echo -en ""
  73. fn_script_log_fatal "IP address not set in game config."
  74. fn_script_log_fatal "Specify the IP you want to bind within: ${servercfgfullpath}."
  75. fn_script_log_fatal "https://linuxgsm.com/network-interfaces\n"
  76. if [ "${commandname}" != "DETAILS" ]; then
  77. core_exit.sh
  78. fi
  79. else
  80. fn_print_info_nl "Check IP: ${getip}"
  81. fn_script_log_info "IP automatically set as: ${getip}"
  82. ip="${getip}"
  83. fi
  84. fi
  85. fi