PHP shell tar problem, return code 2 - php

Want to archive a folder using tar from PHP:
$result = shell_exec("tar cf $sourceFile $sourceFolder -C $source > /dev/null; echo $?");
var_dump($result);
Output:
string(2) "2 "
the > /dev/null; echo $? thing is for outputing the result code of a script under linux;
the -C $source - changes to the right folder before doing anything
This is really strange because when i run this from linux console, it works just fine - creates the archive, so it's not a permission issue.
Other sripts like "whoami" or "ls" work fine.
Any ideas what does it mean?

Maybe: shell_exec("/bin/bash tar ....")

Just for debugging purposes redirect stderr to stdout and use passthru() to display the complete output (possibly including error messages).
$cmd = sprintf("tar cf %s %s -C %s 2>&1",
escapeshellarg($sourceFile),
escapeshellarg($sourceFolder),
escapeshellarg($source)
);
echo '<pre>', htmlspecialchars($cmd), ": \n";
flush();
passthru($cmd, $code);
var_dump($code);
echo "</pre>";

Related

Launch python file from PHP on LINUX server

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

shell script with SFTP from PHP

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);
}

Bash output in web browser

I made a script in bash for getting pings into a file.
#!/bin/sh
echo "Starting script"
echo "Working.."
while true; do
DATE=$(date)
PING=$(ping -c 1 google.pl | tail -1| awk '{print $4}' | cut -d '/' -f 2)
echo "$DATE Ping: $PING" >> logs/ping.txt
sleep 5000
done
But due to lack of free space i changed echo "$DATE Ping: $PING" >> logs/ping.txt to just echo "$DATE Ping: $PING" to recive every line in cmd, and it worked
But still the main idea is to run the scipt through the web browser and display its output. (i can run it tho but i have no idea how to show echo output in a browser)
You can call the bash script from php using:
exec('myscript.sh');
And then open the ping.txt using:
$myFile = "ping.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
Without text file:
$ping = shell_exec('myscript.sh');
echo "<pre>$ping</pre>";
With a bit of ajax and using Net_Ping you could have a page that updates in near-realtime.
Alternatively use shell_exec to run ping from inside your php and echo the output returned from it.
If you need to execute your bash script from PHP and display the output in a browser, then just use the shell_exec() PHP function
Example from php.net:
<?php
$output = shell_exec('/path/script-name-here');
echo "<pre>$output</pre>";
?>

tar utility, PHP and exec

Consider the following code
echo "tar -cvf $outpath -C $path $file<br>";
$arr = array();
$val = exec("tar -cvf $outpath -C $path $file", $arr);
var_dump($arr);
echo outputs tar -cvf ../folder/subfolder1/1234.tar -C ../folder/ subfolder2 and this command really create archive if I call it from command line from htdocs folder. However exec do nothing. var_dump($arr) prints empty array. So here are the questions
How can I make exec create archive?
Why empty array is outputed?
UPD. please note, that I'm running windows.
For those who'll read this question: adding 2>&1 to command helped. And I used shell_exec instead of sheel
$val = shell_exec("tar -cvf $outpath -C $path $file 2>&1");
echo "$val"; // prints output from tar

PHP ffmpeg exec returns null

I'm trying to run ffmpeg through a PHP exec call, I've been debugging for a while and looked at lot of responses on here, but still not found any answers...
My simplified call is:
$cmd = 'ffmpeg 2>&1';
exec(escapeshellcmd($cmd), $stdout, $stderr);
var_dump($stderr);
var_dump($stdout);
var_dump($cmd);
exit;
My output is $stderr = int(1) and $stdout = array(0) { }
Also I tried shell_exec($cmd) which returns NULL.
cmd.exe has permissions set for the IUSR account - e.g. I can run $cmd = 'dir' and see a directory listing output.
PHP is not running in safe mode.
The ffmpeg.exe is in the same directory as my php file, but I have the same response giving an absolute path to the ffmpeg.exe file in $cmd.
ffmpeg is executing fine from the command line.
I'm running Windows XP, IIS and PHP 5.3.
EDIT:
If I run 'ffmpeg -h' I get the help commands, which must indicated that ffmpeg is recognised
I've increased the PHP memory limit to 1024 - no luck.
I've now got this working - I think there may have been several issues:
It turns out that $cmd = 'ffmpeg' returns null, so it's not a good test!
Also running escape shell command on '2>&1' echoes 2^>^&1" - I think this was my original problem.
I've now managed to rea test file using: 'ffmpeg -i SAMPLE.AVI 2>&1'.
Working code:
$cmd = 'ffmpeg -i SAMPLE.AVI 2>&1';
exec($cmd, $output, $value);
var_dump($output);
var_dump($value);
var_dump($cmd);
exit;
As noted above ffmpeg is a bit of a memory hog, so it's worth checking memory too.

Categories