Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- RCON_AUTH=3
- RCON_CMD=2
- declare -A decode_msg
- function switchEndian
- {
- printf %s ${1} | grep -o .. | tac | tr -d '\n'
- return 0
- }
- function intToHex
- {
- printf %.8X ${1}
- return 0
- }
- function hexToInt
- {
- echo "ibase=16; "${1} | bc
- return 0
- }
- function intToHex_SE
- {
- echo $(switchEndian $(intToHex ${1}))
- return 0
- }
- function hexToInt_SE
- {
- echo $(hexToInt $(switchEndian ${1}))
- return 0
- }
- function helper
- {
- echo ${1} | sed -e 's/\(..\)/\\x\1/g'
- return 0
- }
- function rcon_build_msg
- {
- # {1} = ident
- # {2} = type
- # {3} = payload
- if [[ "${#@}" -ne 3 ]]
- then
- echo "Error using rcon_build_msg" >&2
- return 1
- fi
- local msg_size=$(helper $(intToHex_SE $(( 10 + ${#3}))))
- local msg_body=$(helper $(intToHex_SE "${1}"))$(helper $(intToHex_SE "${2}"))"${3}""\\00\\00"
- echo "${msg_size}""${msg_body}"
- return 0
- }
- function rcon_sendMsg
- {
- # ${1} = message
- if [[ "${#@}" -ne 1 ]]
- then
- echo "Error rcon_sendMsg" >&2
- return 1
- fi
- echo -ne "${1}" >"${rcon_pIn}"
- return 0
- }
- function rcon_readByte
- {
- dd if=${rcon_pOut} bs=1 count=${1} 2>/dev/null | xxd -ps | tr 'a-z' 'A-Z'
- return 0
- }
- function rcon_readMsg
- {
- local msgSize=$(rcon_readByte "4")
- local msgBody=$(rcon_readByte $(hexToInt_SE ${msgSize}))
- echo ${msgSize}${msgBody}
- return 0
- }
- function rcon_decodeMsg
- {
- if [[ "${#@}" -ne 1 ]]
- then
- return 1
- fi
- decode_msg["size"]=$(hexToInt_SE ${1:0:8})
- decode_msg["ident"]=$(hexToInt_SE ${1:8:8})
- decode_msg["type"]=$(hexToInt_SE ${1:16:8})
- decode_msg["payload"]=$( echo ${1:24:-4} | xxd -p -r )
- return 0
- }
- function rcon_connect
- {
- # {1} = host
- # {2} = port
- # {3} = password
- ident=42
- local msg=""
- local response=""
- if [[ "${#@}" -ne 3 ]]
- then
- echo "Usage: rcon_connect host port password" >&2
- fi
- netcat -nq 1 "${1}" "${2}" <"${rcon_pIn}" >"${rcon_pOut}" &
- local res=${!}
- exec {hold_pIn}>"${rcon_pIn}"
- exec {hold_pOut}<"${rcon_pOut}"
- ps -p "${res}" >/dev/null
- if [[ $? -ne 0 ]]
- then
- echo "Error netcat" >&2
- return 1
- fi
- msg=$(rcon_build_msg "${ident}" "${RCON_AUTH}" "${3}")
- rcon_sendMsg "${msg}"
- response=$(rcon_readMsg)
- rcon_decodeMsg "${response}"
- if [[ ${decode_msg["ident"]} -ne "${ident}" ]]
- then
- echo "Authentification failed" >&2
- return 1
- fi
- return 0
- }
- rcon_pIn=$(mktemp -u)
- mkfifo "${rcon_pIn}"
- rcon_pOut=$(mktemp -u)
- mkfifo "${rcon_pOut}"
- ident=0
- response=""
- rcon_connect "127.0.0.1" "25575" "password"
- if [[ "${?}" -eq 0 && "${ident}" -gt 0 ]]
- then
- msg=$(rcon_build_msg "${ident}" "${RCON_CMD}" "save-off")
- rcon_sendMsg "${msg}"
- response=$(rcon_readMsg)
- rcon_decodeMsg "${response}"
- echo ${decode_msg["payload"]}
- if [[ "${decode_msg["payload"]}" == "Disabled"* ]]
- then
- msg=$(rcon_build_msg "${ident}" "${RCON_CMD}" "save-all")
- rcon_sendMsg "${msg}"
- response=$(rcon_readMsg)
- rcon_decodeMsg "${response}"
- echo "${decode_msg["payload"]}"
- if [[ "${decode_msg["payload"]}" == *"Save complete." ]]
- then
- echo "TODO: BACKUP"
- fi
- msg=$(rcon_build_msg "${ident}" "${RCON_CMD}" "save-on")
- rcon_sendMsg "${msg}"
- response=$(rcon_readMsg)
- rcon_decodeMsg "${response}"
- echo "${decode_msg["payload"]}"
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement