How to avoid "ls: write error: Broken pipe" with php exec - php

In PHP I am running this command:
exec ("ls -U ".$folder." |head -1000", $ls_u);
This command is inside a PHP file, run directly by PHP on the console and throws this error:
ls: write error: Broken pipe
What causes this error? Or is using pipes in PHP exec not a good idea?

In Linux with PHP on the terminal, you can reproduce this error with this code:
main.php:
<?php
$process = popen("ls", "r");
pclose($process);
?>
Then running it like this on the terminal:
eric#dev ~ $ php main.php
ls: write error: Broken pipe
The error happens because you are running a command which returns data and you are not catching it, so it complains about a broken pipe.
I was able to fix it like this:
<?php
$handle = popen('ls', 'r');
$read = fread($handle, 2096);
pclose($handle);
echo $read;
?>
Then it works, printing:
eric#dev ~ $ php main.php
main.php
anotherfile.txt
Note, you'll have to stick the fread(...) in a loop if you want to get all the output from ls.

I believe there's a syntax error in the console command, and suggest escaping the arguments with escapeshellarg().
exec('ls -U '.escapeshellarg($folder).' | head -1000', $ls_u);

Related

Execute shell command and get live output

I'm trying to execute the following command from PHP, the problem is that I tried "PING", "netstat -a" and "ls", all of them worked fine and I got the output.
Wpscan is a Wordpress bug scanner, it's written in ruby, I installed it in the server and used this command to make it executable from anywhere
sudo ln -s /home/ubuntu/wpscan/wpscan.rb /usr/local/bin/wpscan
As you can see I tried the same command on the shell and it is working fine. I don't know what's the problem with the PHP code.
wpscan command
$cmd = 'wpscan -u www.google.com';
while (# ob_end_flush()); // end all output buffers if any
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
# flush();
}
echo '</pre>';
?>
Update:
I just put the following "2>&1" at the end of the command and I got the following error:
/home/ubuntu/wpscan/lib/common/common_helper.rb:6:in home': couldn't find HOME environment -- expanding~' (ArgumentError)
from /home/ubuntu/wpscan/lib/common/common_helper.rb:6:in '
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require'
from /home/ubuntu/wpscan/lib/wpscan/wpscan_helper.rb:3:in'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire'
from /usr/local/bin/wpscan:8:in `
Update2:
I just checked the users of PHP script and shell.
PHP: www-data
Shell: ubuntu

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

Running PhantomJS from PHP with exec()

I've got the following script:
#!/bin/sh
export DISPLAY=:0
phantomjs --version
It try to run it from the following PHP script:
<?php
$result = shell_exec('sh test.sh');
echo $result;
?>
This script return the following error:
[Thu Jun 19 10:31:31 2014] [error] [client] test.sh: line 3: phantomjs: command not found
I tried to run phantomjs -v by hand in a console, and it runs fine. I checked the PATH, and phantomjs is correctly defined and found.
The execution environment is a virtual Server with LiveConfig.
Can someone help me understand what I'm doing wrong ?
It could be an issue with shell_exec() and line breaks,
try adding "2>&1" to the string you are passing:
$result = shell_exec('sh test.sh 2>&1');
this worked for me, found it in the top comment here, naturally ;)
Your PATH probably lacks the location for the phantomjs executable. PhantomJS is probably installed in /usr/local/bin so you need to add this to your PATH variable:
#!/bin/sh
export DISPLAY=:0
PATH=$PATH:/usr/local/bin
phantomjs --version
To check what the current PATH is, you could begin the shell script with:
#!/bin/sh
echo $PATH
<?php
exec('/usr/local/bin/phantomjs path/somescript.js');
?>
Yes. Sometimes phantomjs don't need full path in some environment without generate any error. However, sometimes it does.
Always use the full path for all argument in the php command.
Did you use the fullpath for hello.js?
Do not use exec(). Never. It's a bad way.
Use the php-phantomjs and PhantomJS Runner instead.

Giving php permission to execute java commands

I am tryng to execute java command from php, but the output array keeps returning empty. Any help would be appreciated.
Below is the code that i have tried
<?php
exec( "/usr/bin/java -version",$output);
print_r($output);
?>
Also i checked the error log and found the following
sh: /usr/bin/java: Permission denied
and when i try the code
<?php
exec( "java -version",$output);
print_r($output);
?>
i get
sh: java: command not found
what should i do to allow my php file to execute java commands?
Use cron to execute the java program.
1)write argument name to a file from your php program.
$Handle = fopen("myfile", 'w');
fwrite($Handle, "argument");
fclose($Handle);
2) Create a bash script to read that argument name and run java program.
#!/bin/sh
arg=$(head -n 1 myfile)
/usr/bin/java argument 2>&1
>myfile
save it as mycron.sh
chmod +x mycron.sh
3) Configure the script in crontab
crontab -e and paste this
* * * * * mycron.sh

running a jar file in PHP

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

Categories