check_ip.sh 3.1 KB

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