I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}
Related
I have a a shell script (myscript.sh), which contains many bash commands. I want to run these command from a php file (index.php).
I used exec to execute the commands as following:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
exec($contents, $output, $return_var);
print_r($contents);
print_r($return_var);
print_r($output);
Here is the content of myscript.sh
VAR1=("somePath/allDirs")
ls
cd ..
ls
for DIR in "${VAR1[#]}"; do
echo "Name is $DIR"
done
pwd
....... other 5 lines of commands
All commands before the loop is working i.e: ls and cd .. and I got the output. However, the for loop is not working and it stop executing the rest of the commands after it.
My question is: how to execute this for loop that is inside shell script file from php file?
note: the for loop contains more command here I just put the echo to make the code easy to read and clear.
Are you mutating the contents of the script at all? Why not just execute the script file rather than passing in the contents of the script? Regardless, if you must pass the contents directly to exec, pass it to bash -c and escape the command contents:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
$contents = escapeshellarg($contents);
exec("bash -c $contents", $output, $return_var);
I have a python script that i want to run from index.php and they both are in the same directory.
The problem is that there's no output shown on the page(index.php).
I am running the page on a CPanel shared hosting from GoDaddy.
I have the permissions -rwx for both the files.
There's no error in running the python3 script file from the terminal.
The same files run smoothly on my computer's localhost but not on the CPanel.
Am able to run bash files using the same php code.
I have tried using system() in php to call the python3 script file but this doesn't seems to work.
index.php:
<?php
$comm = "python3 test.py";
echo $comm;
$output = system($comm);
echo "\n";
print($output);
?>
test.py:
print ("Hello")
Current Output:
python3 test.py
I expect the output to be:
python3 test.py Hello
I think you will need to use shell_exec to run your python code like that
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
and the first line in the python file should be
#!/usr/bin/env python
Don't forget to give your python file the privileges
chmod +x test.py
I tried many solutions but nothing it works :
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
I launched indivdually them but in all cases, Firebug returned : '<pre>'
So I tried this code founded on Stack Overflow :
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
But firebug returned nothing.
My python file begin with #!/usr/bin/env python and if I launch it on server that works !
Do you knwo how can I launch my python file from PHP file ?
chmod will return 0 on success and > 0 on error.
Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.
You can test this by running:
$ /usr/bin/env python /path/to/your/file.py
So check your file permissions to check if the file is executable by the user that runs the php script.
Just to test, you can just print a few lines in your python file
#!/usr/bin/env python
print "test line 1"
print "test line 2"
Then if you have verified permissions and the correct use of python, you can do this in your php.
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
At first, Do you have enabled shell_exec/system/passthru commands in php.ini?
shell_exec("python /home/folder/python/mapfile_compress.py");
I think, it could be problem with your $PATH. Try something like: (use full path to python)
shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
In my case that's work if I write this code :
$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);
I have a command to be run like this
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
I have echoed the $command variable and I get the output as
java -jar X:\wamp\www\moodle\gmksplit.jar X:\wamp\www\moodle/upload/maze_4.gmk X:\wamp\www\moodle/outputs/maze_4;
which is exactly I want to run..
I am trying to run it as
echo $exec($command);
it is not running. I have tried all the functions like shell_exec() and system()
It gives the output as
Java Version: 10700 (1.7.0_01)
when i run the same line in command prompt I get the output as
Java Version: 10700 (1.7.0_01)
Loading lib files in X:\wamp\www\moodle\gmksplit.jar
01_move.lgl 02_main1.lgl 03_main2.lgl 04_control.lgl
05_score.lgl 06_extra.lgl 07_draw.lgl
time taken to load file: 254 ms
so, as you see my php code is giving only the first line as output. The command is not running properly and I am not getting the intended output.
please help me
I am using the wampp server
Can you try this:
<?php
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
$out = array();
exec = ($command, $out);
print_r($out);
?>
I have a .jar file which has a command line interface. I want to call the jar file through command prompt and capture the output of the Jar file.
I have tried with the exec() command.
The command I have used is:
<?php
exec('java -jar D:\\Development\\Filehandler\\dist\\Filehandler.jar \ getConfigLang', $result);
echo $result;
echo $count = count($result);
for($i=0; $i<$count;$i++){
print($result[$i]);
}
?>
The output for this was just '0 0'
Should something else be done before executing this command? like adding path etc???
I am using WAMP server.
Please help me...
Well, you can try two approaches:
1) change current directory in PHP via function http://php.net/manual/en/function.chdir.php
<?php
chdir('D:\Development\Filehandler\dist');
exec('java -jar ./Filehandler.jar \ getConfigLang', $result);
...
?>
2) change .jar file: I don't know if it is possible but try to add these additional libraries with absolute paths.