简体   繁体   中英

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

TL;DR:

I have a PHP page which executes a shell script containing impdp which imports dump to a new schema.
PHP file:

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

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).

Full

I have a PHP page which creates a shell script file and overwrites its contents as the following:

$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

#!/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. so what is wrong with this, thanks in advance. UPDATE
When I execute .sh file itself everything executes well (user is added and granted).
UPDATE 2
I tried doing mentioned above using php oci and it ran successfully.
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.
My new .sh file

#!/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 ? also what permissions that could be missing in the process ?

Update 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'.
So the problem is with How Can I execute any non-os related commands (anything but echo, ls .. etc) from my PHP page ?

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.
Ref : How to call shell script from php that requires SUDO?

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.

The PHP code is probably hiding the error messages related with this.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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