-
Notifications
You must be signed in to change notification settings - Fork 0
/
enshrd_query
executable file
·199 lines (177 loc) · 6.66 KB
/
enshrd_query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
# Get Enshrouded Server infos (via Valve ServerQueries) and Currently connected User list (via ssh)
# Require 'python-a2s' library and 'ssh' access to the server
#
### v1.1 - Moved to 'python-a2s' library to get ping result and Game server Version (instead of log:SVN)
### v1.0 - Greatly reduced script exectime by doing only one ssh call
### VARS (Important: has to match your install)
# Server infos:
_GAMESRV="your.dedicated-server.net" # Server hostname or IP address
_QUERYPORT="15637" # Query port defined on your server
_SSHALIAS="$_GAMESRV" # SSH server alias (set if different from server name)
# tmp file location
_TMPQUERYFILE="/tmp/.a2squery.log"
# Enshrouded Server location info
_GAMEUSERDIR="/home/enshrouded" # Enshrouded server User homedir
_WKGDIR="${_GAMEUSERDIR}/enshrd-monitor" # Enshrouded Server directory
# Set _SERVERSIDE to "true" to run script locally
_SERVERSIDE="" # Set to true to avoid using '-l' locally (used for scripting purpose)
# PREREQ
if ! pip show python-a2s >/dev/null 2>&1; then echo "# 'python-a2s' is NOT installed.. [pip install python-a2s]"; exit 1; fi
# Colorize
_REZ="\e[0m"
_RDX="\e[1;31m"
_BLX="\e[1;34m"
_GRX="\e[1;32m"
_MVX="\e[1;95m"
_BLU () { echo -e "${_BLX}${@}${_REZ}" ; }
_RED () { echo -e "${_RDX}${@}${_REZ}" ; }
_GRN () { echo -e "${_GRX}${@}${_REZ}" ; }
_MAV () { echo -e "${_MVX}${@}${_REZ}" ; }
_OK () { echo -e "[${_GRX}OK${_REZ}${@}]" ; }
_KO () { echo -e "[${_RDX}KO${_REZ}${@}]" ; }
_MYECHO(){
_DOTNUM=35
if [ -z "$1" ]; then echo "<!> need argument"; return 1; fi
_CHAINL=$(echo $@ | wc -c)
_DOTL=$((_DOTNUM - _CHAINL))
i=0
echo -e "${_BLX}#${_REZ} $@\c"
while [ "$i" -lt "$_DOTL" ]; do echo -e " \c"; i=$((i+1)); done
echo -e "= \c"
return 0
}
# Usage
_SCRIPTUSAGE(){
_GRN "Connection:"
echo " -s|--server <Ip-Hostname>"
echo -e " -p|--port <query_port>\n"
_BLU " exemple: \"$(basename $0) <server>:<port>\""
_BLU " or: \"$(basename $0) -s <server> -p <port>\""
echo -e " *** Server infos can be set in script variable to avoid input\n"
_GRN "Modes:"
echo " No Argument [default]"
_BLU " # Query Steam Server Informations"
echo -e "\n -u|--user"
_BLU " # Query currently connected user(s)"
echo -e "\n -n|--number"
_BLU " # Output only the number [0-9] of connected users (used locally for server restart/update)"
echo -e "\n -f|--full"
_BLU " # Output all 'a2s' informations available"
echo -e "\n -l|--local"
_BLU " # Used for server-side local run"
}
# Args parse
while (( "$#" )); do
case "$1" in
-s|--server) _GAMESRV="${2}"; shift 2;;
-p|--port) _QUERYPORT="${2}"; shift 2;;
-h|--help) _SCRIPTUSAGE; exit 0;;
-u|--users) _USERCHECK="true"; shift 1;;
-n|--number) _NUMCHECK="true"; shift 1;;
-l|--local) _SERVERSIDE="true"; shift 1;;
-f|--full) _SHOWALL="true"; shift 1;;
-c|--colorfree) _SHOWCOLOR="false"; shift 1;;
*) if grep -q ':' <<<$1; then
_GAMESRV=$(awk -F':' '{print $1}' <<<$1)
_QUERYPORT=$(awk -F':' '{print $NF}' <<<$1)
else _RED "Argument not 'game_server:port', exit.." && exit 1
fi
shift 1
;;
-*|--*) _RED "Flag not recognised.." >&2; _SCRIPTUSAGE; exit 1 ;;
esac
done
if [ "$_SHOWCOLOR" = "false" ]; then
_REZ=""
_RDX=""
_BLX=""
_GRX=""
_MVX=""
fi
# test gameserver and port variables
[ -z "$_GAMESRV" -a -z "$_QUERYPORT" ] && _RED "Missing Server:Port args.. exit" && exit 1
# Title
if [ -z "$_NUMCHECK" ]; then
_BLU "################### Enshrouded Server Query ###################"
_MYECHO "query type"
[ "$_SERVERSIDE" = "true" ] && echo -e "[${_GRX}local${_REZ}]" || echo -e "[${_GRX}remote${_REZ}]"
fi
# Auto set server to localhost if '-l'
[ "$_SERVERSIDE" = "true" ] && _GAMESRV="localhost" || _GETSSHINFO=$(echo -e "Users: \c"; ssh -t $_SSHALIAS "cat ${_WKGDIR}/connected-users |tr '\n' ' '" 2>/dev/null &)
# Get connected user
if [ -z "$_NUMCHECK" ]; then
if [ "$_SERVERSIDE" = "true" ]; then
_MYECHO "local files"
[ -f "${_WKGDIR}/connected-users" ] && { _GETACTIVEUSERS=$(cat ${_WKGDIR}/connected-users |tr "\n" " "); _OK; } || { _GETACTIVEUSERS="not found"; _KO; }
else
_MYECHO "ssh infos"
[ ! -z "$_GETSSHINFO" ] && { _GETACTIVEUSERS=$(awk -F': ' '{print $NF}' <<<$_GETSSHINFO); _OK; } || { _GETACTIVEUSERS="not found"; _KO; }
fi
fi
# Get server status with python-a2s lib
[ "$_NUMCHECK" = "true" ] || _MYECHO "online state"
a2squery.py $_GAMESRV $_QUERYPORT |tr ",|(|)" "\n" |grep -v '^$' >${_TMPQUERYFILE}
# stop if error found
if ! grep -q "^error:" ${_TMPQUERYFILE} && [ -s "${_TMPQUERYFILE}" ]; then
[ "$_NUMCHECK" = "true" ] || _OK
else
[ "$_NUMCHECK" = "true" ] || _KO
exit 1
fi
# parse server version from "keywords"
_CURRENTVERSION=$(awk -F';' '/keywords/ {print $(NF-2)}' ${_TMPQUERYFILE} |tr -d "'")
# Number only check
[ "$_NUMCHECK" = "true" ] && awk -F'=' '/player_count/ {print $NF}' ${_TMPQUERYFILE} && exit 0
# Users only check
if [ "$_USERCHECK" = "true" ]; then
_SRVNAME=$(awk -F'=' '/server_name/ {print $NF}' ${_TMPQUERYFILE})
_USERS=$(awk -F'=' '/player_count/ {print $NF}' ${_TMPQUERYFILE})
_MYECHO "server name" && echo "$_SRVNAME"
_MYECHO "server version" && echo "$_CURRENTVERSION"
_MYECHO "connected users" && echo "$_USERS"
if [ "$_USERS" -gt 0 ]; then
_MYECHO "username(s)"
echo "$_GETACTIVEUSERS"
echo
fi
exit 0
fi
# Parse all Server Infos and do not display those in _UNUSED
[ "$_SHOWALL" = true ] && _UNUSED=() || _UNUSED=(SourceInfo map_name steam_id folder app_id version edf stv_port stv_name keywords)
while read QUERYLINE; do
_NAME=$(awk -F'=' '{print $1}' <<<$QUERYLINE)
_VALUE=$(awk -F'=' '{print $2}' <<<$QUERYLINE)
# remove unused
grep -q $_NAME <<<${_UNUSED[@]} && continue
# set blanck value to 'default'
[ -z "$_VALUE" -o "$_VALUE" = "''" ] && _VALUE="default"
# parse server_type and platform to show full value
case "$_NAME" in
server_type)
if [ "$_VALUE" = "'d'" ]; then _VALUE="Dedicated"
elif [ "$_VALUE" = "'l'" ]; then _VALUE="Non-Dedicated"
elif [ "$_VALUE" = "'s'" ]; then _VALUE="SourceTV"
else _VALUE="NotFound"
fi
;;
platform)
if [ "$_VALUE" = "'l'" ]; then _VALUE="Linux"
elif [ "$_VALUE" = "'w'" ]; then _VALUE="Windows"
elif [ "$_VALUE" = "'m'" ]; then _VALUE="Mac"
else _VALUE="NotFound"
fi
;;
ping)
_MYECHO "$_NAME" && echo "$_VALUE" |awk '{printf "%.2f", $1*1000}' ; echo ms
continue
;;
esac
_MYECHO "$_NAME" |tr "_" " " && echo "$_VALUE" |tr -d "'"
if [ "$_NAME" = "player_count" ]; then
[ "$_VALUE" -gt 0 ] && _SHOWPLAYERS="true"
fi
done <${_TMPQUERYFILE}
_MYECHO "server version" && echo "$_CURRENTVERSION"
[ "$_SHOWPLAYERS" = true ] && { _MYECHO "username(s)"; echo "$_GETACTIVEUSERS"; }
echo