1 | #!/bin/bash
|
---|
2 | # Version 1
|
---|
3 |
|
---|
4 | # Provides common functions for 7dtd-scripts. Not intended to be run directly.
|
---|
5 |
|
---|
6 | # Check if the script is run as root (exit otherwise) and load global config
|
---|
7 | checkRootLoadConf() {
|
---|
8 | if [ `id -u` -ne 0 ]; then
|
---|
9 | echo "This script has to be run as root!"
|
---|
10 | exit 10
|
---|
11 | fi
|
---|
12 | . /etc/7dtd.conf
|
---|
13 | }
|
---|
14 |
|
---|
15 | # Get the config path for the given instance
|
---|
16 | # Params:
|
---|
17 | # 1: Instance name
|
---|
18 | # Returns:
|
---|
19 | # Config path for instance
|
---|
20 | getInstancePath() {
|
---|
21 | echo $SDTD_BASE/instances/$1
|
---|
22 | }
|
---|
23 |
|
---|
24 | # Check if the given instance name is an existing instance
|
---|
25 | # On failure exit the script!
|
---|
26 | # Params:
|
---|
27 | # 1: Instance name
|
---|
28 | checkInstance() {
|
---|
29 | if [ -z $1 ]; then
|
---|
30 | echo "No instance given!"
|
---|
31 | exit 2
|
---|
32 | fi
|
---|
33 | if [ ! -d $(getInstancePath $1) ]; then
|
---|
34 | echo "Instance $1 does not exist!"
|
---|
35 | exit 3
|
---|
36 | fi
|
---|
37 | }
|
---|
38 |
|
---|
39 | # Check if the given instance is currently running
|
---|
40 | # Params:
|
---|
41 | # 1: Instance name
|
---|
42 | # Returns:
|
---|
43 | # 0 = not running
|
---|
44 | # 1 = running
|
---|
45 | isRunning() {
|
---|
46 | start-stop-daemon --status --pidfile $(getInstancePath $1)/7dtd.pid
|
---|
47 | if [ $? -eq 0 ]; then
|
---|
48 | echo 1
|
---|
49 | else
|
---|
50 | echo 0
|
---|
51 | fi
|
---|
52 | }
|
---|
53 |
|
---|
54 | # Get a single value from a serverconfig
|
---|
55 | # Params:
|
---|
56 | # 1: Instance name
|
---|
57 | # 2: Property name
|
---|
58 | # Returns:
|
---|
59 | # Property value
|
---|
60 | getConfigValue() {
|
---|
61 | CONF=$(getInstancePath $1)/serverconfig.xml
|
---|
62 | xmllint --xpath "string(/ServerSettings/property[@name='$2']/@value)" $CONF
|
---|
63 | }
|
---|
64 |
|
---|
65 | # Send a single command to the telnet port
|
---|
66 | # Params:
|
---|
67 | # 1: Instance name
|
---|
68 | # 2: Command
|
---|
69 | # Returns:
|
---|
70 | # String of telnet output
|
---|
71 | telnetCommand() {
|
---|
72 | TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
|
---|
73 | TEL_PORT=$(getConfigValue $1 TelnetPort)
|
---|
74 | TEL_PASS=$(getConfigValue $1 TelnetPassword)
|
---|
75 | if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
|
---|
76 | echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
|
---|
77 | else
|
---|
78 | echo "Telnet not enabled or no password set."
|
---|
79 | fi
|
---|
80 | }
|
---|