简体   繁体   English

无法在PHP页面创建的Shell脚本中执行sqlplus命令

[英]Can't execute sqlplus commands in shell script created by PHP page

TL;DR: TL; DR:

I have a PHP page which executes a shell script containing impdp which imports dump to a new schema. 我有一个PHP页面,该页面执行包含impdp的shell脚本,该脚本将dump导入新的架构。
PHP file: PHP文件:

echo shell_exec("./DumpCreator.sh 22");

DumpCreator.sh DumpCreator.sh

#!/bin/bash
echo $1
impdp U_$1/Pass DIRECTORY=dmpdir DUMPFILE=MYDMP.DMP remap_schema=PARENT:U_$1

It echos 22 but impdp doesn't execute although all permissions are given to a single user (admin). 它回显22,但impdp不会执行,尽管所有权限都授予单个用户(admin)。

Full 充分

I have a PHP page which creates a shell script file and overwrites its contents as the following: 我有一个PHP页面,该页面创建一个shell脚本文件并覆盖其内容,如下所示:

$shellFile = fopen("myfile.sh" , "w");
$field = "1";
$command = "#!/bin/bash\n"
    ."echo $field\n"
    ."sqlplus system/pass as sysdba << SQLEND\n"
    ."create user U_$field identified by newpass;\n"
    ."grant dba to U_$field;\n"
    ."exit;\n"
    ."SQLEND\n";
fwrite($shellFile, $command);
$output = shell_exec("bash myfile.sh");
echo $output;
fclose($shellFile);

contents of .sh file .sh文件的内容

#!/bin/bash
echo 1
sqlplus system/pass sysdba << SQLEND
create user U_1 identified by pass;
grant dba to U_1;
exit;
SQLEND

My problem is the part of sqlplus isn't executing. 我的问题是sqlplus的一部分未执行。 so what is wrong with this, thanks in advance. 所以这有什么问题,请先谢谢。 UPDATE UPDATE
When I execute .sh file itself everything executes well (user is added and granted). 当我执行.sh文件本身时,一切运行良好(已添加并授予用户)。
UPDATE 2 更新2
I tried doing mentioned above using php oci and it ran successfully. 我尝试使用php oci进行上述操作,并且运行成功。
Now the problem is with when user is granted permission I need to copy some dump to it using a script which I will be needing to execute using PHP. 现在的问题是,当授予用户权限时,我需要使用将需要使用PHP执行的脚本将一些转储复制到它。
My new .sh file 我的新.sh文件

#!/bin/bash
echo $1
impdp U_$1/pass DIRECTORY=DATA_PUMP_DIR DUMPFILE=something.DMP remap_schema=something:U_$1

Even if I removed $1, it doesn't execute this part and I think it doesn't require sudo or to su to root, so what am I doing wrong ? 即使我删除了$ 1,它也不会执行此部分,并且我认为它不需要sudosu才能成为root用户,那我在做什么错呢? also what permissions that could be missing in the process ? 在此过程中还可能缺少哪些权限?

Update 3 更新3
Executing the script directly from terminal using 'admin' account which is the one Oracle is installed on, also getting the current user in PHP shows that it's 'admin'. 使用安装了Oracle的'admin'帐户直接从终端执行脚本,并且在PHP中获取当前用户也表明它是'admin'。
So the problem is with How Can I execute any non-os related commands (anything but echo, ls .. etc) from my PHP page ? 所以问题出在我的PHP页面上如何执行任何与操作系统无关的命令(除了echo,ls等)。

So after searching about permissions, I found that it's possible to execute anything (root or non-root commands) by editing sudoers file which will allow any php to execute any command and that's as far as I can tell is a very poor solution. 因此,在搜索了权限之后,我发现可以通过编辑sudoers文件来执行任何命令(root或非root用户命令),该文件将允许任何php执行任何命令,据我所知这是一个非常糟糕的解决方案。
Ref : How to call shell script from php that requires SUDO? 参考: 如何从需要SUDO的php调用shell脚本?

Make sure you have the required environment variables set. 确保设置了必需的环境变量。

In particular you'll probably have to set LD_LIBRARY_PATH to the location of the shared libraries that come with your Oracle installation. 特别是,您可能必须将LD_LIBRARY_PATH设置为Oracle安装随附的共享库的位置。

The PHP code is probably hiding the error messages related with this. PHP代码可能隐藏与此相关的错误消息。

Compare your environment where you normally run SQL*Plus or IMP before and after running oraenv , you will need to set at least a few of those (and probably most if not all). 比较您在运行oraenv之前和之后通常在其中运行SQL * Plus或IMP的环境,您将需要至少设置其中一些(可能是大多数(如果不是全部))。

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

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