Created
March 28, 2026 02:41
-
-
Save crischutu07/0e3eaa946f58515ee677426b8e55eb3b to your computer and use it in GitHub Desktop.
Bash script that gives minecraft server info including color formatting for MOTD using mcstatus api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| verbose=0 | |
| motd=1 | |
| ver=1 | |
| mods=0 | |
| noaddr=0 | |
| plugins=1 | |
| hide_players=1 | |
| log (){ | |
| [ "$verbose" -eq 1 ] && printf "[DEBUG] %s\n" "$@" | |
| } | |
| declare -A COLOR_MAP=( | |
| # heck i accidentally wrote all of this.. | |
| [0]="\e[38;2;0;0;0m" | |
| [1]="\e[0;34m" | |
| [2]="\e[0;32m" | |
| [3]="\e[0;36m" | |
| [4]="\e[0;31m" | |
| [5]="\e[0;35m" | |
| [6]="\e[0;33m" | |
| [7]="\e[38;2;42;42;42m" # java color | |
| [8]="\e[38;2;21;21;21m" | |
| [9]="\e[0;94m" | |
| [a]="\e[0;92m" | |
| [b]="\e[0;96m" | |
| [c]="\e[0;91m" | |
| [d]="\e[0;95m" | |
| [e]="\e[0;93m" | |
| [f]="\e[0;97m" | |
| [l]="\e[1m" # bold | |
| [m]="\e[9m" # strikethrough | |
| [n]="\e[4m" # underlined | |
| [o]="\e[3m" # italic | |
| [r]="\e[0m" # reset | |
| ) | |
| printf "\n" | |
| help (){ | |
| if [[ "$1" == "short" ]]; then | |
| printf "Usage: %s [-abhvV] <address>\n" "$(basename "$0")" | |
| exit 1 | |
| fi | |
| printf "Usage: %s [-abhvV] <address>\n" "$(basename "$0")" | |
| printf """ | |
| -a, --no-address | |
| Don't show the IP Address. | |
| -h, --help | |
| Display this help. | |
| -m, --show-mods | |
| Display list of mods. | |
| -p, --hide-plugins | |
| Hide plugins list. | |
| -v, --hide-versions | |
| Don't show server version. | |
| -V, --verbose | |
| Turn on verbose mode (debug mode). | |
| More information about this command or filing a bugs, visit this repository: <https://github.com/crischutu07/mcstatus>\n""" | |
| exit 1 | |
| } | |
| OPTIONS=$(getopt -o ahmpPvV --long no-address,help,show-mods,hide-plugins,hide-players,hide-versions,verbose -- "$@") | |
| # shellcheck disable=SC2181 | |
| if [ $? -ne 0 ]; then | |
| help short | |
| fi | |
| eval set -- "$OPTIONS" | |
| while true; do | |
| case "$1" in | |
| -a | --no-address) noaddr=1; shift;; | |
| -h | --help) help;; | |
| -m | --show-mods) mods=1; shift;; | |
| -p | --hide-plugins) plugins=0; shift;; | |
| -P | --hide-players) hide_players=0; shift;; | |
| -v) ver=0; shift;; | |
| -V|--verbose) verbose=1; shift;; | |
| --) shift; break;; | |
| *) help "short"; break;; | |
| esac | |
| done | |
| for i in "$@"; do | |
| normal_argv+=("$i") | |
| done | |
| address="${normal_argv[0]}" | |
| log "OPTIONS: $OPTIONS" | |
| log "ADDR: $address (${normal_argv[1]})" | |
| shift | |
| if [[ -z "$address" ]]; then | |
| help | |
| fi | |
| CONTENTS="$(curl -s "https://api.mcstatus.io/v2/status/java/$address")" | |
| [[ "$CONTENTS" == "Invalid address value" ]] && { | |
| echo "$CONTENTS"; exit 1 | |
| } | |
| [[ "$(jq -r '.online' <<< "$CONTENTS")" != "true" ]] && { | |
| log "API Returned Server is Offline."; online='!' | |
| motd=0; ver=0; mods=0; plugins=0 | |
| } || { | |
| log "API Returned Server is Online"; online='✓' | |
| } | |
| addr=$(jq -r '.ip_address' <<< "$CONTENTS") | |
| log "Address: $addr" | |
| if [[ "$addr" == "null" ]]; then | |
| log "IP Address from domain not found: $addr"; addr="Not Found" | |
| fi | |
| if [[ "$noaddr" == 1 ]]; then | |
| echo "($online) Address: $address" | |
| else | |
| echo "($online) Address: $address ($addr)" | |
| fi | |
| ((motd)) && { | |
| motd="$(jq -r '.motd.raw' <<< "$CONTENTS")" | |
| # used for saving color formatting state | |
| PARSE_COLOR=0 | |
| for ((i=0; i<${#motd}; i++)); do | |
| # get only 1st char | |
| CHAR="${motd:$i:1}" | |
| if [ "$PARSE_COLOR" -eq 1 ]; then | |
| printf "${COLOR_MAP[$CHAR]}" | |
| PARSE_COLOR=0 | |
| else | |
| if [[ "$CHAR" == "§" ]]; then | |
| PARSE_COLOR=1 | |
| else | |
| printf "$CHAR" | |
| fi | |
| fi | |
| done | |
| # reset terminal color | |
| printf "${COLOR_MAP[r]}\n" | |
| } | |
| ((ver)) && { | |
| echo "Version: $(jq -r '.version.name_clean' <<< "$CONTENTS")" | |
| } | |
| ((plugins)) && { | |
| plugins_count=$(jq -r '.plugins | length' <<< "$CONTENTS") | |
| if [[ $plugins_count != 0 ]]; then | |
| echo "╭ Plugins ($plugins_count)" | |
| for ((i=0; i<plugins_count; i++)); do | |
| plugin_name=$(jq -r ".plugins[$i].name" <<< "$CONTENTS") | |
| plugin_version=$(jq -r ".plugins[$i].version" <<< "$CONTENTS") | |
| if [[ "$i" -eq "$((plugins_count-1))" ]]; then | |
| printf "└── %s\n" "$plugin_name ($plugin_version)" | |
| else | |
| printf "├── %s\n" "$plugin_name ($plugin_version)" | |
| fi | |
| done | |
| fi | |
| } | |
| ((mods)) && { | |
| mods_count=$(jq -r '.mods | length' <<< "$CONTENTS") | |
| if [[ $mods_count != 0 ]]; then | |
| echo "╭ Mods ($mods_count)" | |
| for ((i=0; i<mods_count; i++)); do | |
| mod_name=$(jq -r ".mods[$i].name" <<< "$CONTENTS") | |
| mod_version=$(jq -r ".mods[$i].version" <<< "$CONTENTS") | |
| if [[ "$i" -eq "$((mods_count-1))" ]]; then | |
| printf "└── %s\n" "$mod_name ($mod_version)" | |
| else | |
| printf "├── %s\n" "$mod_name ($mod_version)" | |
| fi | |
| done | |
| fi | |
| } | |
| ((hide_players)) && { | |
| players=$(jq -r '.players' <<< "$CONTENTS") | |
| if [[ "$players" != "null" ]]; then | |
| player_count=$(jq -r '.players.list | length' <<< "$CONTENTS") | |
| player_online=$(jq -r '.players.online' <<< "$CONTENTS") | |
| max_players=$(jq -r '.players.max' <<< "$CONTENTS") | |
| if [[ "$player_count" -gt 0 ]]; then | |
| echo "╭ Players ($player_online/$max_players)" | |
| for ((i=0; i<player_count; i++)); do | |
| player_name=$(jq -r ".players.list[$i].name_clean" <<< "$CONTENTS") | |
| if [[ "$i" -eq "$((player_count-1))" ]]; then | |
| printf "└── %s\n" "$player_name" | |
| else | |
| printf "├── %s\n" "$player_name" | |
| fi | |
| done | |
| else | |
| echo "Players ($player_online/$max_players)" | |
| fi | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment