简体   繁体   English

通过PHP在远程服务器上运行脚本

[英]Run script on remote server via PHP

I have a game server company and on my control panel I currently have a page where a user can click a button and it installs a world on their game server. 我有一家游戏服务器公司,并且在我的控制面板上,我现在有一个页面,用户可以单击一个按钮,该页面会在他们的游戏服务器上安装一个世界。 How this is done is via a script on the node which extracts the world onto their game server. 这是通过节点上的脚本完成的,该脚本将世界提取到其游戏服务器上。

I have a few nodes and the panel is hosted on my web server. 我有几个节点,该面板托管在我的Web服务器上。 How would I do it so when they click the button on the website, it not only posts the value of the form to the script but also runs the script? 当他们单击网站上的按钮时,我将如何处理,它不仅将表单的值发布到脚本中,而且还运行脚本?

If you need to know, the script is as follows: 如果需要知道,脚本如下:

#!/bin/bash

id=$SERVERID

if [ "$WORLD" = "testworld" ]; then
     cd "/home/minecraft/multicraft/servers/server$SERVERID"
     unzip -nu "$JAR_DIR/maps/testworld.zip"
fi

exit 0

The variable $WORLD is what needs to be sent to the script as a POST from the form on the control panel. 变量$ WORLD是需要从控制面板上的表单作为POST发送到脚本的内容。

The variable $SERVERID is defined at the top of the script and needs to also be sent from the form on the control panel. 变量$ SERVERID在脚本顶部定义,还需要从控制面板上的表单发送。 The control panel has already defined this variable, it just needs to be sent to the script as part of the form. 控制面板已经定义了此变量,只需将其作为表单的一部分发送到脚本。

I am quite sure how to echo the $SERVERID variable on the line: 我很确定如何在行中回显$ SERVERID变量:

cd "/home/minecraft/multicraft/servers/server$SERVERID"

So help with that is also appreciated. 因此,对此表示感谢。

Any ideas? 有任何想法吗?

Thanks 谢谢

You can save the script on your server as an Shell script, which accepts $WORLD and $SERVERID as command line arguments. 您可以将脚本作为Shell脚本保存在服务器上,该脚本接受$ WORLD和$ SERVERID作为命令行参数。 After that run that script from PHP using exec or system command. 之后,使用execsystem命令从PHP运行该脚本。

Now, your Shell script looks like this. 现在,您的Shell脚本如下所示。

#!/bin/bash

id=$SERVERID

if [ "$1" = "testworld" ]; then
 cd "/home/minecraft/multicraft/servers/server${2}"
 unzip -nu "$JAR_DIR/maps/testworld.zip"
fi

exit 0

Save it as, let say script.sh . 另存为,例如script.sh Now, call it from PHP. 现在,从PHP调用它。

exec("script.sh " . $WORLD . " " . $SERVERID);

I hope I got your question correctly, and It helps. 希望我能正确回答您的问题,对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM