简体   繁体   English

从PHP页面运行Shell脚本

[英]Running Shell Script from PHP Page

What I'd like to do: 我想做的是:

When mydomain.com/index.php is viewed, run a script, that logs in on another machine (different from the one serving the index.php ) in the local net of the server and runs a Python script. 当查看mydomain.com/index.php ,运行一个脚本,该脚本登录到服务器本地网络中的另一台计算机上(不同于为index.php服务的计算机),并运行一个Python脚本。

What I have so far: 到目前为止,我有:

In the index.php I call index.php我称

shell_exec('/var/www/dir/save.sh '.$ARG1.' '.$ARG2);

The script save.sh : 脚本save.sh

#!/usr/bin/expect -f
set cmd "python /path/to/python_script.py"
set file [lindex $argv 0]
set content [lindex $argv 1]

spawn ssh usr@local_ip "$cmd $file $content"
expect "assword:"
send "PWD\r"
interact

I ran sudo chmod www-data+x save.sh to enable the PHP-Site to run the command. 我运行sudo chmod www-data+x save.sh来启用PHP-Site运行命令。 When I run sudo -u www-data ./save.sh arg1 arg2 in the terminal, everything works as expected and I can see the output of python_script.py (that I print for testing). 当我在终端中运行sudo -u www-data ./save.sh arg1 arg2时,一切都按预期工作,并且可以看到python_script.py的输出(我打印以进行测试)。

However, when calling index.php and echoing the output of the shell_exec -Call, I see everything up to user@ip's password: but not the output of python_script.py and I can check, that the script has not been invoked on the other machine. 但是,当调用index.php并回显shell_exec -Call的输出时,我看到的一切都取决于user@ip's password:但是没有 python_script.py的输出,我可以检查一下,该脚本是否在另一个上未被调用机。

Any idea, what I'm missing? 任何想法,我想念的是什么? I'm glad for any help! 我很高兴为您提供任何帮助! :) :)


UPDATE UPDATE

Thanks for the quick responses. 感谢您的快速回复。 Sorry for not making myself clear enough. 对不起,我自己不够清楚。

First of all: This is not used for a public/commercial/elaborate website but rather for private fun and mydomain.com is locked by auth_basic . 首先:这不是用于公共/商业/精心制作的网站,而是用于私人娱乐,并且mydomain.comauth_basic锁定。

My setup: Server1 is hosting mydomain.com . 我的设置: Server1托管mydomain.com Server2 is running some stuff for private entertainment, eg smart home software and Telegram bots. Server2正在运行一些用于私人娱乐的内容,例如智能家居软件和Telegram机器人。

What I want to do: When calling mydomain.com/index.php , I want to save same parameters (actually just True or False ) to Server2 , to use them for some of the scripts running there. 我想做的是:调用mydomain.com/index.php ,我想将相同的参数(实际上只是TrueFalse )保存到Server2 ,以将它们用于在那里运行的某些脚本。 Server1 and Server2 are in the same local network but only Server1 is reachable from the outside, hence the idea to login via SSH and run a Python-Script. Server1Server2在同一个局域网中,但是从外部只能访问Server1 ,因此可以通过SSH登录并运行Python脚本。

I appreciate any advise on how to do this in a better way or to make my inteded solution work. 感谢您提供有关如何更好地执行此操作或使我的嵌入式解决方案工作的建议。 :) :)


UPDATE 2: 更新2:

Thanks symcbean for pointing to errors in the script. 感谢symcbean指出脚本中的错误。 I have to admit, that I copy pasted it from other stackoverflow threads. 我必须承认,我从其他stackoverflow线程复制并粘贴了它。

Thanks also to ADyson for pointing out better ways to handle the issue. 也感谢ADyson指出了解决此问题的更好方法。 I will definetly keep them in mind for bigger projects. 对于大型项目,我一定会牢记在心。

In the meantime I was able to get the script running. 同时,我能够运行脚本。 It is still very basic, does not track excection of the Python script and does not handle errors. 它仍然是非常基本的,不跟踪Python脚本的执行并且不处理错误。 Again, I wan't to point out, that I use this for private usage only and mydomain.com/index.php will only be up for limited time (it will be an andvent calender and will be taken offline after xmas). 再一次,我不想指出,我仅将其用于私人用途,而mydomain.com/index.php将仅在有限的时间内可用(它将是安万德压光机,圣诞节后将脱机)。 But just for completeness sake, here is what I came up with: 但是为了完整起见,这是我想到的:

#!/usr/bin/expect -f
set cmd "python /path/to/python_script.py"
set file [lindex $argv 0]
set content [lindex $argv 1]

spawn ssh usr@local_ip
expect "assword:"
send "PWD\r"
expect "*\$ "
send "$cmd $file $content\r"
expect "*\$*"
send "exit\r"
interact

Leaving aside the peculiar choices before you got to that point, what exactly were you expecting to happen when the expect script gets to 在您到达这一点之前,先撇开特殊的选择,当期望脚本到达

interact

?

There is nothing in your shell script to start the python script, nor to track its execution, nor to exit the shell. 您的shell脚本中没有启动python脚本,跟踪其执行或退出shell的内容。 Meanwhile you PHP code is blocked until the local script exits. 同时,您的PHP代码被阻止,直到本地脚本退出。

While there are lots of ways of fixing this, I am hesitant to suggest any without a better understanding of why you chose such an elaborate mechanism to handle the invocation. 尽管有很多方法可以解决此问题,但我仍然不愿提出任何建议,而又不能更好地理解您为什么选择了这种复杂的机制来处理调用。

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

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