从 php 运行 shell 脚本 [英] run shell script from php

查看:47
本文介绍了从 php 运行 shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 php 脚本,该脚本可以通过 ssh 连接到我的 Qnap TS219 服务器并在其上运行命令.

I am attempting to create a php script that can connect thru ssh to my Qnap TS219 server and run a command on it.

到目前为止,我的脚本可以很好地连接到服务器,但是当我运行命令时,我收到一条错误消息,我无法弄清楚.

My script so far connects fine to the server but when I run the command I get an error message and I can't figure it out.

exec.sh

#!/bin/bash
cp /share/MD0_DATA/Qdownload/rapidshare/admin/script.txt /share/MD0_DATA/Qdownload/rapidshare/admin/script.sh
chmod 755 /share/MD0_DATA/Qdownload/rapidshare/admin/script.sh
nohup sh /share/MD0_DATA/Qdownload/rapidshare/admin/script.sh &
exit 0

脚本.sh

#!/bin/bash
/opt/bin/plowdown -o /share/MD0_DATA/Qdownload/rapidshare /share/MD0_DATA/Qdownload/rapidshare/admin/down.txt 2>/share/MD0_DATA/Qdownload/rapidshare/admin/output.txt

提交表单后我当前通过 ssh 运行的命令:

the command that I am currently running thru ssh after I submit the form:

echo $ssh->exec('sh /share/MD0_DATA/Qdownload/rapidshare/admin/exec.sh');

现在生成下面的代码,但只有在我杀死 2 个 bash 进程之后(如果我不杀死 2 个 bash 进程,页面会持续加载并且处理器活动为 100%):

Right now generates the code below but only after I kill 2 bash processes (the page keeps loading indefinetly and the processor activity is at 100% if I don't kill the 2 bash processes):

/share/MD0_DATA/.qpkg/Optware/share/plowshare/lib.sh: line 261: getopt: command not found start download (rapidshare): http://rapidshare.com/files/312885386/Free_Stuff-Your_Internet_eBay_Business_Free_Startup_Resources.rar /share/MD0_DATA/.qpkg/Optware/share/plowshare/lib.sh: line 261: getopt: command not found /share/MD0_DATA/.qpkg/Optware/share/plowshare/lib.sh: line 46: --insecure: command not found Error: failed inside rapidshare_download() 

这个脚本将在我的本地网络中使用,无法从外部访问,所以我不担心安全性,我知道代码看起来很基本,原始但我没有使用php,shell脚本的经验,所以如果有人可以对此有任何意义并帮助我将不胜感激.

This script will be used in my local network, no access from outside, so I am not worry about security, I know the code looks very basic, primitive but I have no experience with php, shell script, so if someone can make any sense on this and help me out will be greatly appreciated.

编辑1.我也尝试了 shell_exec 命令仍然没有乐趣,如果我通过腻子运行脚本,效果很好.

Edit1. I also tried the shell_exec command still no joy and if I run the script thru putty works beautifully.

编辑2.我想我们正在做某事.

Edit2. I think we are on to something.

我添加了您建议的代码,并收到以下消息.

I added the code you suggested and I got the following message.

sh: /share/MD0_DATA/.qpkg/Optware/share/plowshare: is a directory /usr/bin:/bin:/usr/sbin:/sbin 

我认为目前 PATH 是 usr/bin:/bin:usr/sbin:/sbin 并且我认为它应该是/opt/bin/opt/sbin 因为有可执行文件".有什么想法吗?

I think at the moment the PATH is usr/bin:/bin:usr/sbin:/sbin and I think it should be /opt/bin /opt/sbin because there are the "executables". Any ideeas?

谢谢,克里斯.

推荐答案

运行这个

echo $ssh->exec('pwd');

它是否正确列出了您的路径?如果是这样,那么您的问题不是 PHP,如果它没有列出或仍然给出错误,那么 PHP 是您的问题,我们可以从那里继续.

Does it list your path correctly? If so then your problem is NOT PHP, if it doesn't list or still gives an error then PHP is your problem and we can continue from there.

根据您列出的错误,我的第一个猜测是 PATH 未设置,因此 lib.sh 找不到它要查找的内容.

From the error you've listed, my first guess would be that PATH isn't set, so lib.sh can't find what it's looking for.

请记住,您正在使用自定义 shell (PHP ssh) 登录,很多时候事情没有按照应有的方式设置,因此您的脚本可能找不到路径和变量等要求.

Remember you're logging in with a custom shell (PHP ssh), quite often things aren't set as they should be, so your scripts might not find requirements like paths and variables.

既然它给了/root,我们至少知道它正在通过,为什么不也设置 PATH 等...

Since it's giving /root, we at least know it's going through, why not also set the PATH etc...

echo $ssh->exec('PATH=$PATH;/share/MD0_DATA/.qpkg/Optware/share/plowshare; sh /share/MD0_DATA/Qdownload/rapidshare/admin/exec.sh');

请记住,您也可以使用它来查看正在设置和未设置的内容.

Remember you can also use this to see what is and isn't being set.

echo $ssh->exec('ECHO $PATH');

这篇关于从 php 运行 shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆