【问题标题】:Run script for send in-game Terraria server commands运行脚本以发送游戏内泰拉瑞亚服务器命令
【发布时间】:2020-08-03 14:02:10
【问题描述】:

在过去的一周里,我在 Ubuntu v18.04 操作系统中安装了 Terraria 1.3.5.3 服务器,用于和朋友在线玩。此服务器应 24/7 开机,没有任何 GUI,只能通过内部 LAN 上的 SSH 访问。

我的朋友问我是否有办法让他们控制服务器,例如通过内部游戏内聊天发送消息,所以我想在所需命令(例如'$say something'或'$save')和读取终端通过管道输出,解释命令并用 bash 命令发回。

我按照以下说明安装服务器:

https://www.linode.com/docs/game-servers/host-a-terraria-server-on-your-linode

并配置我的路由器以将专用端口转发到泰拉瑞亚服务器。

一切正常,但我真的很难让 python 通过上面链接中描述的“terrariad”bash 脚本发送命令。

这是一个用于发送命令的代码,在 python 中:

import subprocess

subprocess.Popen("terrariad save", shell=True)

这很好用,但如果我尝试输入带空格的字符串:

import subprocess

subprocess.Popen("terrariad \"say something\"", shell=True)

它在空格字符中停止命令,在终端上输出:

: say

而不是想要的:

: say something
<Server>something

我能做些什么来解决这个问题? 我尝试了很多东西,但我得到了相同的结果。

附:如果我在 ssh putty 终端中手动发送命令,它可以工作!

编辑 1:

我放弃了 python 解决方案,现在我将尝试使用 bash 代替,这样做似乎更符合逻辑。

编辑 2:

我发现“terrariad”脚本只需要一个参数,但无论我使用哪种方法,Popen 都会将我的参数分成两个,因为我的输入字符串中间有一个空格字符。像这样:

预期:

terrariad "say\ something"

$1 = "say something"

但我得到了 python Popen 的这个:

subprocess.Popen("terrariad \"say something\"", shell=True)

$1 = "say
$2 = something"

不管我尝试列出它:

subprocess.Popen(["terrariad", "say something"])

$1 = "say
$2 = something"

或者在空格字符前使用\引号,如果到达空格字符,它总是拆分变量。

编辑 3:

查看 bash 脚本,我可以理解发送命令时发生了什么...基本上它使用屏幕程序中的命令“stuff”将字符发送到 terraria 屏幕会话:

screen -S terraria -X stuff $send

$send 是一个 printf 命令:

send="`printf \"$*\r\"`"

在我看来,如果我从 Python 运行 bash 文件,它的结果与从命令行运行的结果不同。这怎么可能?这是函数的错误还是错误的实现?

谢谢!

【问题讨论】:

标签: python bash ubuntu


【解决方案1】:

我终于有了一个解决方案,使用管道而不是 Popen 解决方案。

在我看来,Popen 不是运行 bash 脚本的最佳解决方案,如 How to do multiple arguments with Python Popen? 中所述,SiHa 在 cmets 中发送的链接(谢谢!):

“但是,使用 Python 作为许多系统命令的包装器并不是一个好主意。至少,您应该将命令分解为单独的 Popen,以便可以充分处理非零退出。在实际上,这个脚本似乎更适合作为 shell 脚本。”。

所以我提出了解决方案,使用了一个 fifo 文件:

首先,在所需目录(例如,/samba/terraria/config)中创建一个用作管道的 fifo:

mkfifo cmdOutput

*/samba/terraria - 这是我创建的目录,以便使用另一台计算机轻松编辑脚本、保存和加载地图到服务器,与 samba 共享 (https://linuxize.com/post/how-to-install-and-configure-samba-on-ubuntu-18-04/)

然后我创建一个 python 脚本来读取屏幕输出,然后写入管道文件(我知道,可能还有其他方法):

import shlex, os

outputFile = os.open("/samba/terraria/config/cmdOutput", os.O_WRONLY )


print("python script has started!")
while 1:
    line = input()
    print(line)
    cmdPosition = line.find("&")
    if( cmdPosition != -1 ):
        cmd = slice(cmdPosition+1,len(line))
        cmdText = line[cmd]
        os.write(outputFile, bytes( cmdText + "\r\r", 'utf-8'))
        os.write(outputFile, bytes("say Command executed!!!\r\r", 'utf-8'))

然后我编辑 terraria.service 文件以调用此脚本,从 terrariaServer 传送,并将错误重定向到另一个文件:

ExecStart=/usr/bin/screen -dmS terraria /bin/bash -c "/opt/terraria/TerrariaServer.bin.x86_64 -config /samba/terraria/config/serverconfig.txt < /samba/terraria/config/cmdOutput 2>/samba/terraria/config/errorLog.txt | python3 /samba/terraria/scripts/allowCommands.py"

*/samba/terraria/scripts/allowCommands.py - 我的脚本在哪里。

**/samba/terraria/config/errorLog.txt - 将错误日志保存在文件中。

现在我可以发送命令,例如“中午”或“黎明”,这样我就可以更改游戏中的时间、保存世界并在 Boss 战之前用 samba 服务器备份它,如果我有时间可以做其他事情 XD,并且让终端显示服务器正在发生的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多