query_gamedig.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. # LinuxGSM query_gamedig.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Querys a gameserver using node-gamedig.
  7. # https://github.com/sonicsnes/node-gamedig
  8. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. # Check if gamedig and jq are installed.
  10. if [ "$(command -v gamedig 2>/dev/null)" ]&&[ "$(command -v jq 2>/dev/null)" ]; then
  11. # will bypass query if server offline.
  12. check_status.sh
  13. if [ "${status}" != "0" ]; then
  14. # checks if query is working null = pass.
  15. gamedigcmd=$(echo -e "gamedig --type \"${querytype}\" --host \"${queryip}\" --query_port \"${queryport}\"|jq")
  16. gamedigraw=$(gamedig --type "${querytype}" --host "${queryip}" --query_port "${queryport}")
  17. querystatus=$(echo "${gamedigraw}" | jq '.error|length')
  18. if [ "${querystatus}" != "null" ]; then
  19. gamedigcmd=$(echo -e "gamedig --type \"${querytype}\" --host \"${queryip}\" --port \"${queryport}\"|jq")
  20. gamedigraw=$(gamedig --type "${querytype}" --host "${queryip}" --port "${queryport}")
  21. querystatus=$(echo "${gamedigraw}" | jq '.error|length')
  22. fi
  23. # server name.
  24. gdname=$(echo "${gamedigraw}" | jq -re '.name')
  25. if [ "${gdname}" == "null" ]; then
  26. unset gdname
  27. fi
  28. # numplayers.
  29. if [ "${querytype}" == "minecraft" ]; then
  30. gdplayers=$(echo "${gamedigraw}" | jq -re '.players | length-1')
  31. else
  32. gdplayers=$(echo "${gamedigraw}" | jq -re '.players | length')
  33. fi
  34. if [ "${gdplayers}" == "null" ]; then
  35. unset gdplayers
  36. elif [ "${gdplayers}" == "[]" ]||[ "${gdplayers}" == "-1" ]; then
  37. gdplayers=0
  38. fi
  39. # maxplayers.
  40. gdmaxplayers=$(echo "${gamedigraw}" | jq -re '.maxplayers')
  41. if [ "${gdmaxplayers}" == "null" ]; then
  42. unset maxplayers
  43. elif [ "${gdmaxplayers}" == "[]" ]; then
  44. gdmaxplayers=0
  45. fi
  46. # current map.
  47. gdmap=$(echo "${gamedigraw}" | jq -re '.map')
  48. if [ "${gdmap}" == "null" ]; then
  49. unset gdmap
  50. fi
  51. # current gamemode.
  52. gdgamemode=$(echo "${gamedigraw}" | jq -re '.raw.rules.GameMode_s')
  53. if [ "${gdgamemode}" == "null" ]; then
  54. unset gdgamemode
  55. fi
  56. # numbots.
  57. gdbots=$(echo "${gamedigraw}" | jq -re '.bots | length')
  58. if [ "${gdbots}" == "null" ]||[ "${gdbots}" == "0" ]; then
  59. unset gdbots
  60. fi
  61. fi
  62. fi